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

Update account forms submit errors message #1649

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/afraid-eels-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": patch
---

improve account forms submit errors message
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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 @@ -84,7 +86,7 @@ export const registerCustomer = async ({ formData, reCaptchaToken }: RegisterCus

return {
status: 'error',
error: result.errors.map((error) => error.message).join('\n'),
error: createErrorsList(result.errors),
};
} catch (error) {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const RegisterCustomerForm = ({
return (
<>
{formStatus && (
<Message className="mb-8" variant={formStatus.status}>
<Message className="mb-8 whitespace-pre-line" variant={formStatus.status}>
<p>{formStatus.message}</p>
</Message>
)}
Expand Down
15 changes: 15 additions & 0 deletions core/app/[locale]/(default)/account/(tabs)/_components/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface GenericError {
[p: string]: string | string[];
message: string;
}

export const createErrorsList = (submitErrors: GenericError[]) =>
submitErrors
.map((error) => {
if (submitErrors.length > 1) {
return `\u2022 ${error.message}`;
}

return error.message;
})
.join('\n');
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getSessionCustomerAccessToken } from '~/auth';
import { client } from '~/client';
import { graphql } from '~/client/graphql';

import { createErrorsList } from '../../_components/utils';
import { State } from '../../settings/change-password/_actions/change-password';

const DeleteCustomerAddressMutation = graphql(`
Expand Down Expand Up @@ -58,7 +59,7 @@ export const deleteAddress = async (addressId: number): Promise<State> => {

return {
status: 'error',
message: result.errors.map((error) => error.message).join('\n'),
message: createErrorsList(result.errors),
};
} catch (error: unknown) {
if (error instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { parseAccountFormData } from '~/components/form-fields/shared/parse-fields';

import { createErrorsList } from '../../../_components/utils';

const AddCustomerAddressMutation = graphql(`
mutation AddCustomerAddressMutation(
$input: AddCustomerAddressInput!
Expand Down Expand Up @@ -88,7 +90,7 @@ export const addAddress = async ({

return {
status: 'error',
message: result.errors.map((error) => error.message).join('\n'),
message: createErrorsList(result.errors),
};
} catch (error: unknown) {
if (error instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { parseAccountFormData } from '~/components/form-fields/shared/parse-fields';

import { createErrorsList } from '../../../../_components/utils';

const UpdateCustomerAddressMutation = graphql(`
mutation UpdateCustomerAddressMutation(
$input: UpdateCustomerAddressInput!
Expand All @@ -17,6 +19,9 @@ const UpdateCustomerAddressMutation = graphql(`
updateCustomerAddress(input: $input, reCaptchaV2: $reCaptchaV2) {
errors {
__typename
... on AddressDoesNotExistError {
message
}
... on CustomerAddressUpdateError {
message
}
Expand Down Expand Up @@ -96,15 +101,7 @@ export const updateAddress = async ({

return {
status: 'error',
message: result.errors
.map((error) => {
if (error.__typename === 'AddressDoesNotExistError') {
return t('Errors.notFound');
}

return error.message;
})
.join('\n'),
message: createErrorsList(result.errors),
};
} catch (error: unknown) {
if (error instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { client } from '~/client';
import { graphql, VariablesOf } from '~/client/graphql';
import { parseAccountFormData } from '~/components/form-fields/shared/parse-fields';

import { createErrorsList } from '../../_components/utils';

const UpdateCustomerMutation = graphql(`
mutation UpdateCustomerMutation($input: UpdateCustomerInput!, $reCaptchaV2: ReCaptchaV2Input) {
customer {
Expand Down Expand Up @@ -109,6 +111,6 @@ export const updateCustomer = async ({ formData, reCaptchaToken }: UpdateCustome

return {
status: 'error',
error: result.errors.map((error) => error.message).join('\n'),
error: createErrorsList(result.errors),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getSessionCustomerAccessToken } from '~/auth';
import { client } from '~/client';
import { graphql } from '~/client/graphql';

import { createErrorsList } from '../../../_components/utils';

const ChangePasswordFieldsSchema = z.object({
customerId: z.string(),
customerToken: z.string(),
Expand Down Expand Up @@ -79,7 +81,7 @@ export const changePassword = async (_previousState: unknown, formData: FormData

return {
status: 'error',
message: result.errors.map((error) => error.message).join('\n'),
message: createErrorsList(result.errors),
};
} catch (error: unknown) {
if (error instanceof z.ZodError) {
Expand Down
Loading