Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(account-settings): refactor formValidation, updateAccountSettings & dataProcessing #242

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 55 additions & 47 deletions apps/storefront/src/pages/AccountSetting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,42 @@ function useData() {
const companyId = role === 3 && isAgenting ? Number(salesRepCompanyId) : Number(companyInfoId);
const isBCUser = !isB2BUser || (role === 3 && !isAgenting);

return { isBCUser, companyId, customer };
const validateEmailValue = async (emailValue: string) => {
if (customer.emailAddress === trim(emailValue)) return true;
const payload = {
email: emailValue,
channelId,
};

const { isValid }: { isValid: boolean } = isBCUser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the type definition { isValid: boolean } here because the async functions are giving back any?

If so, I'd be tempted to move it closer to the problem by adding it as a return type definition of their signatures
(rather than make this function make up for their shortcomings)

? await checkUserBCEmail(payload)
: await checkUserEmail(payload);

return isValid;
};

const emailValidation = (data: Partial<ParamProps>) => {
if (data.email !== customer.emailAddress && !data.currentPassword) {
return false;
}

return true;
};

const passwordValidation = (data: Partial<ParamProps>) => {
if (data.password !== data.confirmPassword) {
return false;
}

return true;
};

return { isBCUser, companyId, customer, validateEmailValue, emailValidation, passwordValidation };
}

function AccountSetting() {
const { isBCUser, companyId, customer } = useData();
const { isBCUser, companyId, customer, validateEmailValue, emailValidation, passwordValidation } =
useData();

const {
control,
Expand Down Expand Up @@ -170,51 +201,6 @@ function AccountSetting() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const validateEmailValue = async (emailValue: string) => {
if (customer.emailAddress === trim(emailValue)) return true;
const payload = {
email: emailValue,
channelId,
};

const { isValid }: CustomFieldItems = isBCUser
? await checkUserBCEmail(payload)
: await checkUserEmail(payload);

if (!isValid) {
setError('email', {
type: 'custom',
message: b3Lang('accountSettings.notification.emailExists'),
});
}

return isValid;
};

const passwordValidation = (data: Partial<ParamProps>) => {
if (data.password !== data.confirmPassword) {
setError('confirmPassword', {
type: 'manual',
message: b3Lang('global.registerComplete.passwordMatchPrompt'),
});
setError('password', {
type: 'manual',
message: b3Lang('global.registerComplete.passwordMatchPrompt'),
});
return false;
}

return true;
};

const emailValidation = (data: Partial<ParamProps>) => {
if (data.email !== customer.emailAddress && !data.currentPassword) {
snackbar.error(b3Lang('accountSettings.notification.updateEmailPassword'));
return false;
}
return true;
};

const handleGetUserExtraFields = (data: CustomFieldItems) => {
let userExtraFieldsInfo: CustomFieldItems[] = [];
const userExtraFields = accountInfoFormFields.filter(
Expand All @@ -237,10 +223,32 @@ function AccountSetting() {
try {
const isValid = await validateEmailValue(data.email);

if (!isValid) {
setError('email', {
type: 'custom',
message: b3Lang('accountSettings.notification.emailExists'),
});
}

const emailFlag = emailValidation(data);

if (!emailFlag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these flag names aren't very descriptive, what do you think to turning them into something like

if (isValidEmail(data.email)) { 
    // etc

in another commit/PR/ticket?

snackbar.error(b3Lang('accountSettings.notification.updateEmailPassword'));
}

const passwordFlag = passwordValidation(data);

if (!passwordFlag) {
setError('confirmPassword', {
type: 'manual',
message: b3Lang('global.registerComplete.passwordMatchPrompt'),
});
setError('password', {
type: 'manual',
message: b3Lang('global.registerComplete.passwordMatchPrompt'),
});
}

let userExtraFields: CustomFieldItems[] = [];
if (!isBCUser) {
userExtraFields = handleGetUserExtraFields(data);
Expand Down