|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 4 | +import { useState, useCallback } from 'react'; |
| 5 | +import { useForm } from 'react-hook-form'; |
| 6 | +import { toast } from 'sonner'; |
| 7 | +import z from 'zod'; |
| 8 | +import { Mail, ArrowLeft } from 'lucide-react'; |
| 9 | +import Link from 'next/link'; |
| 10 | +import { BoundlessButton } from '../buttons'; |
| 11 | +import { |
| 12 | + Form, |
| 13 | + FormControl, |
| 14 | + FormField, |
| 15 | + FormItem, |
| 16 | + FormLabel, |
| 17 | + FormMessage, |
| 18 | +} from '../ui/form'; |
| 19 | +import { Input } from '../ui/input'; |
| 20 | +import { forgotPassword } from '@/lib/api/auth'; |
| 21 | + |
| 22 | +const forgotPasswordSchema = z.object({ |
| 23 | + email: z.string().email({ |
| 24 | + message: 'Please enter a valid email address', |
| 25 | + }), |
| 26 | +}); |
| 27 | + |
| 28 | +type ForgotPasswordFormData = z.infer<typeof forgotPasswordSchema>; |
| 29 | + |
| 30 | +interface ForgotPasswordWrapperProps { |
| 31 | + setLoadingState: (isLoading: boolean) => void; |
| 32 | +} |
| 33 | + |
| 34 | +const ForgotPasswordWrapper = ({ |
| 35 | + setLoadingState, |
| 36 | +}: ForgotPasswordWrapperProps) => { |
| 37 | + const [isLoading, setIsLoading] = useState(false); |
| 38 | + const [isSuccess, setIsSuccess] = useState(false); |
| 39 | + |
| 40 | + const form = useForm<ForgotPasswordFormData>({ |
| 41 | + resolver: zodResolver(forgotPasswordSchema), |
| 42 | + defaultValues: { |
| 43 | + email: '', |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + const handleSubmit = useCallback( |
| 48 | + async (data: ForgotPasswordFormData) => { |
| 49 | + setIsLoading(true); |
| 50 | + setLoadingState(true); |
| 51 | + |
| 52 | + try { |
| 53 | + // const response = await fetch('/api/auth/forgot-password', { |
| 54 | + // method: 'POST', |
| 55 | + // headers: { |
| 56 | + // 'Content-Type': 'application/json', |
| 57 | + // }, |
| 58 | + // body: JSON.stringify({ email: data.email }), |
| 59 | + // }); |
| 60 | + const response = await forgotPassword({ email: data.email }); |
| 61 | + |
| 62 | + const result = response; |
| 63 | + console.log(result); |
| 64 | + |
| 65 | + if (response) { |
| 66 | + setIsSuccess(true); |
| 67 | + toast.success('Password reset instructions sent to your email'); |
| 68 | + form.reset(); |
| 69 | + } else { |
| 70 | + form.setError('root', { |
| 71 | + type: 'manual', |
| 72 | + message: result.message || 'Failed to send reset email', |
| 73 | + }); |
| 74 | + toast.error(result.message || 'Failed to send reset email'); |
| 75 | + } |
| 76 | + } catch { |
| 77 | + form.setError('root', { |
| 78 | + type: 'manual', |
| 79 | + message: 'An unexpected error occurred. Please try again.', |
| 80 | + }); |
| 81 | + toast.error('An unexpected error occurred'); |
| 82 | + } finally { |
| 83 | + setIsLoading(false); |
| 84 | + setLoadingState(false); |
| 85 | + } |
| 86 | + }, |
| 87 | + [form, setLoadingState] |
| 88 | + ); |
| 89 | + |
| 90 | + if (isSuccess) { |
| 91 | + return ( |
| 92 | + <div className='space-y-6'> |
| 93 | + <div className='text-center'> |
| 94 | + <div className='mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-full bg-green-500/20'> |
| 95 | + <Mail className='h-8 w-8 text-green-500' /> |
| 96 | + </div> |
| 97 | + <h2 className='text-2xl font-semibold text-white'> |
| 98 | + Check Your Email |
| 99 | + </h2> |
| 100 | + <p className='mt-2 text-sm text-[#D9D9D9]'> |
| 101 | + We've sent password reset instructions to your email address. |
| 102 | + </p> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div className='text-center'> |
| 106 | + <Link |
| 107 | + href='/auth' |
| 108 | + className='text-primary hover:text-primary/80 inline-flex items-center text-sm' |
| 109 | + > |
| 110 | + <ArrowLeft className='mr-1 h-4 w-4' /> |
| 111 | + Back to sign in |
| 112 | + </Link> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className='space-y-6'> |
| 120 | + <div className=''> |
| 121 | + <h2 className='text-[40px] leading-[120%] font-medium tracking-[-1.6px] text-white'> |
| 122 | + Reset Password |
| 123 | + </h2> |
| 124 | + <p className='mt-2 text-[16px] leading-relaxed tracking-[-0.64px] text-[#D9D9D9]'> |
| 125 | + Enter the email address you used when you joined, and we'll send you |
| 126 | + instructions to reset your password. |
| 127 | + </p> |
| 128 | + </div> |
| 129 | + |
| 130 | + <Form {...form}> |
| 131 | + <form onSubmit={form.handleSubmit(handleSubmit)} className='space-y-4'> |
| 132 | + <FormField |
| 133 | + control={form.control} |
| 134 | + name='email' |
| 135 | + render={({ field }) => ( |
| 136 | + <FormItem> |
| 137 | + <FormLabel className='text-xs font-medium text-white'> |
| 138 | + Email |
| 139 | + </FormLabel> |
| 140 | + <FormControl> |
| 141 | + <div className='relative'> |
| 142 | + <Mail className='absolute top-3 left-3 h-4 w-4 text-[#B5B5B5]' /> |
| 143 | + <Input |
| 144 | + {...field} |
| 145 | + type='email' |
| 146 | + placeholder='Enter your email' |
| 147 | + className='w-full border-[#2B2B2B] bg-[#1C1C1C] pl-10 text-white caret-white placeholder:text-[#B5B5B5] focus-visible:ring-0 focus-visible:ring-offset-0' |
| 148 | + /> |
| 149 | + </div> |
| 150 | + </FormControl> |
| 151 | + <FormMessage /> |
| 152 | + </FormItem> |
| 153 | + )} |
| 154 | + /> |
| 155 | + |
| 156 | + {form.formState.errors.root && ( |
| 157 | + <div className='text-sm text-red-500'> |
| 158 | + {form.formState.errors.root.message} |
| 159 | + </div> |
| 160 | + )} |
| 161 | + |
| 162 | + <BoundlessButton |
| 163 | + type='submit' |
| 164 | + className='w-full' |
| 165 | + disabled={isLoading || !form.formState.isValid} |
| 166 | + fullWidth |
| 167 | + loading={isLoading} |
| 168 | + > |
| 169 | + Send Reset Link |
| 170 | + </BoundlessButton> |
| 171 | + </form> |
| 172 | + </Form> |
| 173 | + |
| 174 | + <div className='text-center'> |
| 175 | + <Link |
| 176 | + href='/auth' |
| 177 | + className='text-primary hover:text-primary/80 inline-flex items-center text-sm' |
| 178 | + > |
| 179 | + <ArrowLeft className='mr-1 h-4 w-4' /> |
| 180 | + Back to sign in |
| 181 | + </Link> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + ); |
| 185 | +}; |
| 186 | + |
| 187 | +export default ForgotPasswordWrapper; |
0 commit comments