-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add components for change password
- Loading branch information
1 parent
cfab55b
commit 841ffc1
Showing
7 changed files
with
271 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": minor | ||
--- | ||
|
||
add components for change password |
57 changes: 57 additions & 0 deletions
57
apps/core/app/[locale]/(default)/login/_actions/submit-change-password-form.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use server'; | ||
|
||
import { ZodError } from 'zod'; | ||
|
||
import { | ||
ChangePasswordSchema, | ||
submitChangePassword, | ||
} from '~/client/mutations/submit-change-password'; | ||
|
||
export interface State { | ||
status: 'idle' | 'error' | 'success'; | ||
message?: string; | ||
} | ||
|
||
export const submitChangePasswordForm = async (_previousState: unknown, formData: FormData) => { | ||
try { | ||
const parsedData = ChangePasswordSchema.parse({ | ||
customerId: formData.get('customer-id'), | ||
customerToken: formData.get('customer-token'), | ||
newPassword: formData.get('new-password'), | ||
confirmPassword: formData.get('confirm-password'), | ||
}); | ||
|
||
const response = await submitChangePassword({ | ||
newPassword: parsedData.newPassword, | ||
token: parsedData.customerToken, | ||
customerEntityId: Number(parsedData.customerId), | ||
}); | ||
|
||
if (response.customer.resetPassword.errors.length === 0) { | ||
return { status: 'success', message: '' }; | ||
} | ||
|
||
return { | ||
status: 'error', | ||
message: response.customer.resetPassword.errors.map((error) => error.message).join('\n'), | ||
}; | ||
} catch (error: unknown) { | ||
if (error instanceof ZodError) { | ||
return { | ||
status: 'error', | ||
message: error.issues | ||
.map(({ path, message }) => `${path.toString()}: ${message}.`) | ||
.join('\n'), | ||
}; | ||
} | ||
|
||
if (error instanceof Error) { | ||
return { | ||
status: 'error', | ||
message: error.message, | ||
}; | ||
} | ||
|
||
return { status: 'error', message: 'Unknown error' }; | ||
} | ||
}; |
152 changes: 152 additions & 0 deletions
152
apps/core/app/[locale]/(default)/login/_components/change-password-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
'use client'; | ||
|
||
import { Button } from '@bigcommerce/components/button'; | ||
import { | ||
Field, | ||
FieldControl, | ||
FieldLabel, | ||
FieldMessage, | ||
Form, | ||
FormSubmit, | ||
} from '@bigcommerce/components/form'; | ||
import { Input } from '@bigcommerce/components/input'; | ||
import { Message } from '@bigcommerce/components/message'; | ||
import { Loader2 as Spinner } from 'lucide-react'; | ||
import { useTranslations } from 'next-intl'; | ||
import { ChangeEvent, useRef, useState } from 'react'; | ||
import { useFormState, useFormStatus } from 'react-dom'; | ||
|
||
import { useRouter } from '~/navigation'; | ||
|
||
import { submitChangePasswordForm } from '../_actions/submit-change-password-form'; | ||
|
||
interface Props { | ||
customerId: number; | ||
customerToken: string; | ||
} | ||
|
||
const SubmitButton = () => { | ||
const { pending } = useFormStatus(); | ||
const t = useTranslations('Account.SubmitChangePassword'); | ||
|
||
return ( | ||
<Button | ||
className="relative w-fit items-center px-8 py-2" | ||
data-button | ||
disabled={pending} | ||
variant="primary" | ||
> | ||
<> | ||
{pending && ( | ||
<> | ||
<span className="absolute z-10 flex h-full w-full items-center justify-center bg-gray-400"> | ||
<Spinner aria-hidden="true" className="animate-spin" /> | ||
</span> | ||
<span className="sr-only">{t('spinnerText')}</span> | ||
</> | ||
)} | ||
<span aria-hidden={pending}>{t('submitText')}</span> | ||
</> | ||
</Button> | ||
); | ||
}; | ||
|
||
export const ChangePasswordForm = ({ customerId, customerToken }: Props) => { | ||
const form = useRef<HTMLFormElement>(null); | ||
const router = useRouter(); | ||
const [state, formAction] = useFormState(submitChangePasswordForm, { | ||
status: 'idle', | ||
message: '', | ||
}); | ||
|
||
const [newPassword, setNewPasssword] = useState(''); | ||
const [isConfirmPasswordValid, setIsConfirmPasswordValid] = useState(true); | ||
|
||
const t = useTranslations('Account.ChangePassword'); | ||
let messageText = ''; | ||
|
||
if (state.status === 'error') { | ||
messageText = state.message; | ||
} | ||
|
||
if (state.status === 'success') { | ||
messageText = t('successMessage'); | ||
} | ||
|
||
const handleNewPasswordChange = (e: ChangeEvent<HTMLInputElement>) => | ||
setNewPasssword(e.target.value); | ||
const handleConfirmPasswordValidation = (e: ChangeEvent<HTMLInputElement>) => { | ||
const confirmPassword = e.target.value; | ||
|
||
return setIsConfirmPasswordValid(confirmPassword === newPassword); | ||
}; | ||
|
||
if (state.status === 'success') { | ||
setTimeout(() => router.push('/login'), 2000); | ||
} | ||
|
||
return ( | ||
<> | ||
{(state.status === 'error' || state.status === 'success') && ( | ||
<Message className="mb-8 w-full text-gray-500" variant={state.status}> | ||
<p>{messageText}</p> | ||
</Message> | ||
)} | ||
|
||
<Form action={formAction} className="mb-14 flex flex-col gap-4 md:py-4 lg:p-0" ref={form}> | ||
<Field className="hidden" name="customer-id"> | ||
<FieldControl asChild> | ||
<Input id="customer-id" readOnly type="number" value={customerId} /> | ||
</FieldControl> | ||
</Field> | ||
<Field className="hidden" name="customer-token"> | ||
<FieldControl asChild> | ||
<Input id="customer-token" readOnly type="text" value={customerToken} /> | ||
</FieldControl> | ||
</Field> | ||
<Field className="relative space-y-2 pb-7" name="new-password"> | ||
<FieldLabel htmlFor="new-password" isRequired={true}> | ||
{t('newPasswordLabel')} | ||
</FieldLabel> | ||
<FieldControl asChild> | ||
<Input | ||
autoComplete="none" | ||
id="new-password" | ||
onChange={handleNewPasswordChange} | ||
required | ||
type="password" | ||
variant={state.status === 'error' ? 'error' : undefined} | ||
/> | ||
</FieldControl> | ||
</Field> | ||
|
||
<Field className="relative space-y-2 pb-7" name="confirm-password"> | ||
<FieldLabel htmlFor="confirm-password" isRequired={true}> | ||
{t('confirmPasswordLabel')} | ||
</FieldLabel> | ||
<FieldControl asChild> | ||
<Input | ||
autoComplete="none" | ||
id="confirm-password" | ||
onChange={handleConfirmPasswordValidation} | ||
onInvalid={handleConfirmPasswordValidation} | ||
required | ||
type="password" | ||
variant={!isConfirmPasswordValid || state.status === 'error' ? 'error' : undefined} | ||
/> | ||
</FieldControl> | ||
<FieldMessage | ||
className="absolute inset-x-0 bottom-0 inline-flex w-full text-sm text-gray-500" | ||
match={(value: string) => value !== newPassword} | ||
> | ||
{t('confirmPasswordValidationMessage')} | ||
</FieldMessage> | ||
</Field> | ||
|
||
<FormSubmit asChild> | ||
<SubmitButton /> | ||
</FormSubmit> | ||
</Form> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters