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,111 +97,53 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
}; };
return ( return (
<Form {...form}>
<form <form
className={cn('flex w-full flex-col gap-y-4', className)} className={cn('flex w-full flex-col gap-y-4', className)}
onSubmit={handleSubmit(onFormSubmit)} onSubmit={form.handleSubmit(onFormSubmit)}
> >
<div> <FormField
<Label htmlFor="current-password" className="text-muted-foreground"> control={form.control}
Current Password name="currentPassword"
</Label> render={({ field }) => (
<FormItem>
<div className="relative"> <FormLabel>Current Password</FormLabel>
<Input <FormControl>
id="current-password" <PasswordInput autoComplete="current-password" {...field} />
type={showCurrentPassword ? 'text' : 'password'} </FormControl>
minLength={6} <FormMessage />
maxLength={72} </FormItem>
autoComplete="current-password" )}
className="bg-background mt-2 pr-10"
{...register('currentPassword')}
/> />
<Button <FormField
variant="link" control={form.control}
type="button" name="password"
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>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>
)} )}
</Button>
</div>
<FormErrorMessage className="mt-1.5" error={errors.currentPassword} />
</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 <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={showPassword ? 'Mask password' : 'Reveal password'} <FormItem>
onClick={() => setShowPassword((show) => !show)} <FormLabel> Repeat Password</FormLabel>
> <FormControl>
{showPassword ? ( <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>
)} )}
</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"> <div className="mt-4">
<Button type="submit" disabled={isSubmitting}> <Button type="submit" disabled={isSubmitting}>
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />} {isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
@ -213,5 +151,6 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
</Button> </Button>
</div> </div>
</form> </form>
</Form>
); );
}; };