Skip to content

Commit

Permalink
chore(core): revert auth page changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Dec 9, 2024
1 parent ab616d1 commit 00c8d07
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,30 @@ export const changePassword = async (_previousState: unknown, formData: FormData
const result = response.data.customer.resetPassword;

if (result.errors.length === 0) {
return { status: 'success', messages: [''] };
return { status: 'success', message: '' };
}

return {
status: 'error',
messages: result.errors.map((error) => error.message),
message: result.errors.map((error) => error.message).join('\n'),
};
} catch (error: unknown) {
if (error instanceof ZodError) {
return {
status: 'error',
messages: error.issues.map(({ path, message }) => `${path.toString()}: ${message}.`),
message: error.issues
.map(({ path, message }) => `${path.toString()}: ${message}.`)
.join('\n'),
};
}

if (error instanceof Error) {
return {
status: 'error',
messages: [error.message],
message: error.message,
};
}

return { status: 'error', messages: [t('Errors.error')] };
return { status: 'error', message: t('Errors.error') };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { Message } from '~/components/ui/message';
import { useRouter } from '~/i18n/routing';

import { useAccountStatusContext } from '../../../account/(tabs)/_components/account-status-provider';
import { SubmitMessagesList } from '../../../account/(tabs)/_components/submit-messages-list';
import { changePassword } from '../_actions/change-password';

interface Props {
Expand Down Expand Up @@ -51,13 +50,19 @@ export const ChangePasswordForm = ({ customerId, customerToken }: Props) => {
const router = useRouter();
const [state, formAction] = useActionState(changePassword, {
status: 'idle',
messages: [''],
message: '',
});

const [newPassword, setNewPasssword] = useState('');
const [isConfirmPasswordValid, setIsConfirmPasswordValid] = useState(true);
const { setAccountState } = useAccountStatusContext();

let messageText = '';

if (state.status === 'error') {
messageText = state.message;
}

const handleNewPasswordChange = (e: ChangeEvent<HTMLInputElement>) =>
setNewPasssword(e.target.value);
const handleConfirmPasswordValidation = (e: ChangeEvent<HTMLInputElement>) => {
Expand All @@ -67,15 +72,15 @@ export const ChangePasswordForm = ({ customerId, customerToken }: Props) => {
};

if (state.status === 'success') {
setAccountState({ status: 'success', messages: [t('confirmChangePassword')] });
setAccountState({ status: 'success', message: t('confirmChangePassword') });
router.push('/login');
}

return (
<>
{state.status === 'error' && (
<Message className="mb-8 w-full text-gray-500" variant={state.status}>
<SubmitMessagesList messages={state.messages} />
<p>{messageText}</p>
</Message>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { Message } from '~/components/ui/message';

import { useAccountStatusContext } from '../../../account/(tabs)/_components/account-status-provider';
import { SubmitMessagesList } from '../../../account/(tabs)/_components/submit-messages-list';
import { login } from '../_actions/login';

const SubmitButton = () => {
Expand Down Expand Up @@ -67,7 +66,7 @@ export const LoginForm = () => {
<>
{accountState.status === 'success' && (
<Message className="col-span-full mb-8 w-full text-gray-500" variant={accountState.status}>
<SubmitMessagesList messages={accountState.messages} />
<p>{accountState.message}</p>
</Message>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const processZodErrors = (err: z.ZodError) => {
}));

if (formErrors.length > 0) {
return formErrors.map(({ message }) => message);
return formErrors.join('\n');
}

return Object.entries(fieldErrors).flatMap(([, errorList]) => {
return errorList?.map(({ message }) => message) ?? [''];
});
return Object.entries(fieldErrors)
.map(([, errorList]) => {
return `${errorList?.map(({ message }) => message).join('\n')}`;
})
.join('\n');
};

const ResetPasswordMutation = graphql(`
Expand Down Expand Up @@ -80,20 +82,20 @@ export const resetPassword = async ({

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

if (error instanceof Error) {
return { status: 'error', errors: [error.message] };
return { status: 'error', error: error.message };
}

return { status: 'error', errors: [t('Errors.error')] };
return { status: 'error', error: t('Errors.error') };
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { ChangeEvent, useEffect, useRef, useState } from 'react';
import { useFormStatus } from 'react-dom';
import ReCaptcha from 'react-google-recaptcha';

import {
AccountState as FormStatus,
useAccountStatusContext,
} from '~/app/[locale]/(default)/account/(tabs)/_components/account-status-provider';
import { SubmitMessagesList } from '~/app/[locale]/(default)/account/(tabs)/_components/submit-messages-list';
import { useAccountStatusContext } from '~/app/[locale]/(default)/account/(tabs)/_components/account-status-provider';
import { type FragmentOf } from '~/client/graphql';
import { Button } from '~/components/ui/button';
import {
Expand All @@ -32,6 +28,11 @@ interface Props {
reCaptchaSettings?: FragmentOf<typeof ResetPasswordFormFragment>;
}

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

const SubmitButton = () => {
const t = useTranslations('Login.ForgotPassword.Form');

Expand Down Expand Up @@ -64,7 +65,7 @@ export const ResetPasswordForm = ({ reCaptchaSettings }: Props) => {
const router = useRouter();

useEffect(() => {
setAccountState({ status: 'idle', messages: [''] });
setAccountState({ status: 'idle' });
}, [setAccountState]);

const onReCatpchaChange = (token: string | null) => {
Expand Down Expand Up @@ -106,13 +107,13 @@ export const ResetPasswordForm = ({ reCaptchaSettings }: Props) => {

setAccountState({
status: 'success',
messages: [t('confirmResetPassword', { email: customerEmail?.toString() })],
message: t('confirmResetPassword', { email: customerEmail?.toString() }),
});
router.push('/login');
}

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

reCaptchaRef.current?.reset();
Expand All @@ -122,7 +123,7 @@ export const ResetPasswordForm = ({ reCaptchaSettings }: Props) => {
<>
{formStatus?.status === 'error' && (
<Message className="mb-8 w-full whitespace-pre" variant={formStatus.status}>
<SubmitMessagesList messages={formStatus.messages} />
<p>{formStatus.message}</p>
</Message>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { ChangeEvent, MouseEvent, useRef, useState } from 'react';
import { useFormStatus } from 'react-dom';
import ReCaptcha from 'react-google-recaptcha';

import {
AccountState as FormStatus,
useAccountStatusContext,
} from '~/app/[locale]/(default)/account/(tabs)/_components/account-status-provider';
import { useAccountStatusContext } from '~/app/[locale]/(default)/account/(tabs)/_components/account-status-provider';
import { ExistingResultType } from '~/client/util';
import {
Checkboxes,
Expand Down Expand Up @@ -38,11 +35,16 @@ import { Button } from '~/components/ui/button';
import { Field, Form, FormSubmit } from '~/components/ui/form';
import { Message } from '~/components/ui/message';

import { SubmitMessagesList } from '../../../account/(tabs)/_components/submit-messages-list';
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';
messages: string[];
}

type CustomerFields = ExistingResultType<typeof getRegisterCustomerQuery>['customerFields'];
type AddressFields = ExistingResultType<typeof getRegisterCustomerQuery>['addressFields'];

Expand Down Expand Up @@ -223,7 +225,7 @@ export const RegisterCustomerForm = ({
const submit = await registerCustomer({ formData, reCaptchaToken });

if (submit.status === 'success') {
setAccountState({ status: 'success', messages: [''] });
setAccountState({ status: 'success' });

await login(formData);
}
Expand All @@ -240,9 +242,9 @@ export const RegisterCustomerForm = ({

return (
<>
{formStatus && formStatus.status !== 'idle' && (
{formStatus && (
<Message className="mb-8" variant={formStatus.status}>
<SubmitMessagesList messages={formStatus.messages} />
<SubmitErrorsList errors={formStatus.messages} />
</Message>
)}
<Form action={onSubmit} onClick={preSubmitFieldsValidation} ref={form}>
Expand Down

0 comments on commit 00c8d07

Please sign in to comment.