refactor: password form

This commit is contained in:
nafees nazik
2023-11-29 22:32:42 +05:30
parent 62809e9506
commit dc56c2abf2

View File

@ -1,9 +1,7 @@
'use client'; 'use client';
import { useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { Eye, EyeOff, Loader } from 'lucide-react'; import { Loader } from 'lucide-react';
import { useForm } from 'react-hook-form'; import { useForm } from 'react-hook-form';
import { z } from 'zod'; import { z } from 'zod';
@ -12,12 +10,17 @@ import { TRPCClientError } from '@documenso/trpc/client';
import { trpc } from '@documenso/trpc/react'; import { trpc } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils'; import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button'; import { Button } from '@documenso/ui/primitives/button';
import { Input } from '@documenso/ui/primitives/input'; import {
import { Label } from '@documenso/ui/primitives/label'; Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
import { PasswordInput } from '@documenso/ui/primitives/password-input';
import { useToast } from '@documenso/ui/primitives/use-toast'; import { useToast } from '@documenso/ui/primitives/use-toast';
import { FormErrorMessage } from '../form/form-error-message';
export const ZPasswordFormSchema = z export const ZPasswordFormSchema = z
.object({ .object({
currentPassword: z currentPassword: z
@ -48,16 +51,7 @@ export type PasswordFormProps = {
export const PasswordForm = ({ className }: PasswordFormProps) => { export const PasswordForm = ({ className }: PasswordFormProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [showPassword, setShowPassword] = useState(false); const form = useForm<TPasswordFormSchema>({
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [showCurrentPassword, setShowCurrentPassword] = useState(false);
const {
register,
handleSubmit,
reset,
formState: { errors, isSubmitting },
} = useForm<TPasswordFormSchema>({
values: { values: {
currentPassword: '', currentPassword: '',
password: '', password: '',
@ -66,6 +60,8 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
resolver: zodResolver(ZPasswordFormSchema), resolver: zodResolver(ZPasswordFormSchema),
}); });
const isSubmitting = form.formState.isSubmitting;
const { mutateAsync: updatePassword } = trpc.profile.updatePassword.useMutation(); const { mutateAsync: updatePassword } = trpc.profile.updatePassword.useMutation();
const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => { const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => {
@ -75,7 +71,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
password, password,
}); });
reset(); form.reset();
toast({ toast({
title: 'Password updated', title: 'Password updated',
@ -101,117 +97,60 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
}; };
return ( return (
<form <Form {...form}>
className={cn('flex w-full flex-col gap-y-4', className)} <form
onSubmit={handleSubmit(onFormSubmit)} className={cn('flex w-full flex-col gap-y-4', className)}
> onSubmit={form.handleSubmit(onFormSubmit)}
<div> >
<Label htmlFor="current-password" className="text-muted-foreground"> <FormField
Current Password control={form.control}
</Label> name="currentPassword"
render={({ field }) => (
<FormItem>
<FormLabel>Current Password</FormLabel>
<FormControl>
<PasswordInput autoComplete="current-password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="relative"> <FormField
<Input control={form.control}
id="current-password" name="password"
type={showCurrentPassword ? 'text' : 'password'} render={({ field }) => (
minLength={6} <FormItem>
maxLength={72} <FormLabel>Password</FormLabel>
autoComplete="current-password" <FormControl>
className="bg-background mt-2 pr-10" <PasswordInput autoComplete="new-password" {...field} />
{...register('currentPassword')} </FormControl>
/> <FormMessage />
</FormItem>
)}
/>
<Button <FormField
variant="link" control={form.control}
type="button" name="repeatedPassword"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3" render={({ field }) => (
aria-label={showCurrentPassword ? 'Mask password' : 'Reveal password'} <FormItem>
onClick={() => setShowCurrentPassword((show) => !show)} <FormLabel> Repeat Password</FormLabel>
> <FormControl>
{showCurrentPassword ? ( <PasswordInput autoComplete="new-password" {...field} />
<EyeOff className="text-muted-foreground h-5 w-5" /> </FormControl>
) : ( <FormMessage />
<Eye className="text-muted-foreground h-5 w-5" /> </FormItem>
)} )}
/>
<div className="mt-4">
<Button type="submit" disabled={isSubmitting}>
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
Update password
</Button> </Button>
</div> </div>
</form>
<FormErrorMessage className="mt-1.5" error={errors.currentPassword} /> </Form>
</div>
<div>
<Label htmlFor="password" className="text-muted-foreground">
Password
</Label>
<div className="relative">
<Input
id="password"
type={showPassword ? 'text' : 'password'}
minLength={6}
maxLength={72}
autoComplete="new-password"
className="bg-background mt-2 pr-10"
{...register('password')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowPassword((show) => !show)}
>
{showPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" />
) : (
<Eye className="text-muted-foreground h-5 w-5" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.password} />
</div>
<div>
<Label htmlFor="repeated-password" className="text-muted-foreground">
Repeat Password
</Label>
<div className="relative">
<Input
id="repeated-password"
type={showConfirmPassword ? 'text' : 'password'}
minLength={6}
maxLength={72}
autoComplete="new-password"
className="bg-background mt-2 pr-10"
{...register('repeatedPassword')}
/>
<Button
variant="link"
type="button"
className="absolute right-0 top-0 flex h-full items-center justify-center pr-3"
aria-label={showConfirmPassword ? 'Mask password' : 'Reveal password'}
onClick={() => setShowConfirmPassword((show) => !show)}
>
{showConfirmPassword ? (
<EyeOff className="text-muted-foreground h-5 w-5" />
) : (
<Eye className="text-muted-foreground h-5 w-5" />
)}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.repeatedPassword} />
</div>
<div className="mt-4">
<Button type="submit" disabled={isSubmitting}>
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
Update password
</Button>
</div>
</form>
); );
}; };