mirror of
https://github.com/documenso/documenso.git
synced 2025-11-16 01:32:06 +10:00
refactor: reset password form
This commit is contained in:
@ -1,11 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
|
||||||
|
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { Eye, EyeOff } from 'lucide-react';
|
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
@ -13,9 +10,15 @@ 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 { FormErrorMessage } from '@documenso/ui/primitives/form/form-error-message';
|
import {
|
||||||
import { Input } from '@documenso/ui/primitives/input';
|
Form,
|
||||||
import { Label } from '@documenso/ui/primitives/label';
|
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';
|
||||||
|
|
||||||
export const ZResetPasswordFormSchema = z
|
export const ZResetPasswordFormSchema = z
|
||||||
@ -40,15 +43,7 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
|
|||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
const [showPassword, setShowPassword] = useState(false);
|
const form = useForm<TResetPasswordFormSchema>({
|
||||||
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
|
|
||||||
|
|
||||||
const {
|
|
||||||
register,
|
|
||||||
reset,
|
|
||||||
handleSubmit,
|
|
||||||
formState: { errors, isSubmitting },
|
|
||||||
} = useForm<TResetPasswordFormSchema>({
|
|
||||||
values: {
|
values: {
|
||||||
password: '',
|
password: '',
|
||||||
repeatedPassword: '',
|
repeatedPassword: '',
|
||||||
@ -56,6 +51,8 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
|
|||||||
resolver: zodResolver(ZResetPasswordFormSchema),
|
resolver: zodResolver(ZResetPasswordFormSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isSubmitting = form.formState.isSubmitting;
|
||||||
|
|
||||||
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
|
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
|
||||||
|
|
||||||
const onFormSubmit = async ({ password }: Omit<TResetPasswordFormSchema, 'repeatedPassword'>) => {
|
const onFormSubmit = async ({ password }: Omit<TResetPasswordFormSchema, 'repeatedPassword'>) => {
|
||||||
@ -65,7 +62,7 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
|
|||||||
token,
|
token,
|
||||||
});
|
});
|
||||||
|
|
||||||
reset();
|
form.reset();
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: 'Password updated',
|
title: 'Password updated',
|
||||||
@ -93,81 +90,42 @@ export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps)
|
|||||||
};
|
};
|
||||||
|
|
||||||
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="password" className="text-muted-foreground">
|
<FormField
|
||||||
<span>Password</span>
|
control={form.control}
|
||||||
</Label>
|
name="password"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Password</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<PasswordInput {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="repeatedPassword"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
<FormLabel>Repeat Password</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<PasswordInput {...field} />
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
<div className="relative">
|
<Button type="submit" size="lg" loading={isSubmitting}>
|
||||||
<Input
|
{isSubmitting ? 'Resetting Password...' : 'Reset Password'}
|
||||||
id="password"
|
</Button>
|
||||||
type={showPassword ? 'text' : 'password'}
|
</form>
|
||||||
minLength={6}
|
</Form>
|
||||||
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="repeatedPassword" className="text-muted-foreground">
|
|
||||||
<span>Repeat Password</span>
|
|
||||||
</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>
|
|
||||||
|
|
||||||
<Button size="lg" loading={isSubmitting}>
|
|
||||||
{isSubmitting ? 'Resetting Password...' : 'Reset Password'}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user