Skip to content

Commit

Permalink
refactor submit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-alexsaiannyi committed Dec 5, 2024
1 parent fd1efa8 commit 9237e7f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { parseRegisterCustomerFormData } from '~/components/form-fields/shared/parse-fields';

import { createErrorsList } from '../../../account/(tabs)/_components/utils';

const RegisterCustomerMutation = graphql(`
mutation RegisterCustomer($input: RegisterCustomerInput!, $reCaptchaV2: ReCaptchaV2Input) {
customer {
Expand Down Expand Up @@ -62,7 +60,7 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus
if (!isRegisterCustomerInput(parsedData)) {
return {
status: 'error',
error: t('Errors.inputError'),
errors: [t('Errors.inputError')],
};
}

Expand All @@ -86,7 +84,7 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus

return {
status: 'error',
error: createErrorsList(result.errors),
errors: result.errors.map((error) => error.message),
};
} catch (error) {
// eslint-disable-next-line no-console
Expand All @@ -95,13 +93,13 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus
if (error instanceof BigCommerceAPIError) {
return {
status: 'error',
error: t('Errors.apiError'),
errors: [t('Errors.apiError')],
};
}

return {
status: 'error',
error: t('Errors.error'),
errors: [t('Errors.error')],
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ import { Button } from '~/components/ui/button';
import { Field, Form, FormSubmit } from '~/components/ui/form';
import { Message } from '~/components/ui/message';

import { SubmitErrorsList } from '../../../account/(tabs)/_components/submit-errors-list';
import { login } from '../_actions/login';
import { registerCustomer } from '../_actions/register-customer';
import { getRegisterCustomerQuery } from '../page-data';

interface FormStatus {
status: 'success' | 'error';
message: string;
messages: string[];
}

type CustomerFields = ExistingResultType<typeof getRegisterCustomerQuery>['customerFields'];
Expand Down Expand Up @@ -202,7 +203,7 @@ export const RegisterCustomerForm = ({
if (formData.get('customer-password') !== formData.get('customer-confirmPassword')) {
setFormStatus({
status: 'error',
message: t('confirmPassword'),
messages: [t('confirmPassword')],
});

window.scrollTo({
Expand Down Expand Up @@ -230,7 +231,7 @@ export const RegisterCustomerForm = ({
}

if (submit.status === 'error') {
setFormStatus({ status: 'error', message: submit.error ?? '' });
setFormStatus({ status: 'error', messages: submit.errors ?? [''] });
}

window.scrollTo({
Expand All @@ -242,8 +243,8 @@ export const RegisterCustomerForm = ({
return (
<>
{formStatus && (
<Message className="mb-8 whitespace-pre-line" variant={formStatus.status}>
<p>{formStatus.message}</p>
<Message className="mb-8" variant={formStatus.status}>
<SubmitErrorsList errors={formStatus.messages} />
</Message>
)}
<Form action={onSubmit} onClick={preSubmitFieldsValidation} ref={form}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const SubmitErrorsList = ({ errors }: { errors: string[] }) => (
<ul className={errors.length === 1 ? 'list-none' : 'ms-2 list-disc'}>
{errors.map((error) => (
<li key={error}>{error}</li>
))}
</ul>
);
15 changes: 0 additions & 15 deletions core/app/[locale]/(default)/account/(tabs)/_components/utils.ts

This file was deleted.

0 comments on commit 9237e7f

Please sign in to comment.