Skip to content

Commit

Permalink
feat(core): convert register over to toasts (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark authored Dec 12, 2024
1 parent 0e34915 commit 608b886
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 201 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-roses-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

Update the register customer page to use toasts for messaging.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ const RegisterCustomerMutation = graphql(`
type Variables = VariablesOf<typeof RegisterCustomerMutation>;
type RegisterCustomerInput = Variables['input'];

interface RegisterCustomerForm {
formData: FormData;
reCaptchaToken?: string;
}

const isRegisterCustomerInput = (data: unknown): data is RegisterCustomerInput => {
if (typeof data === 'object' && data !== null && 'email' in data) {
return true;
Expand All @@ -50,7 +45,15 @@ const isRegisterCustomerInput = (data: unknown): data is RegisterCustomerInput =
return false;
};

export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCustomerForm) => {
interface RegisterCustomerResponse {
status: 'success' | 'error';
message: string;
}

export const registerCustomer = async (
formData: FormData,
reCaptchaToken?: string,
): Promise<RegisterCustomerResponse> => {
const t = await getTranslations('Register');

formData.delete('customer-confirmPassword');
Expand All @@ -60,7 +63,7 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus
if (!isRegisterCustomerInput(parsedData)) {
return {
status: 'error',
errors: [t('Errors.inputError')],
message: t('Errors.inputError'),
};
}

Expand All @@ -78,28 +81,27 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus

const result = response.data.customer.registerCustomer;

if (result.errors.length === 0) {
return { status: 'success', data: parsedData };
if (result.errors.length > 0) {
result.errors.forEach((error) => {
throw new Error(error.message);
});
}

return {
status: 'error',
errors: result.errors.map((error) => error.message),
};
return { status: 'success', message: t('Form.successMessage') };
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);

if (error instanceof BigCommerceAPIError) {
return {
status: 'error',
errors: [t('Errors.apiError')],
message: t('Errors.apiError'),
};
}

return {
status: 'error',
errors: [t('Errors.error')],
message: t('Errors.error'),
};
}
};
Loading

0 comments on commit 608b886

Please sign in to comment.