|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Input } from '@components/ui/input'; |
| 3 | +import { Label } from '@components/ui/label'; |
| 4 | +import { Button } from '@components/ui/button'; |
| 5 | +import { |
| 6 | + InputGroup, |
| 7 | + InputGroupAddon, |
| 8 | + InputGroupInput, |
| 9 | +} from '@components/ui/input-group'; |
| 10 | +import { EyeIcon, EyeOffIcon } from 'lucide-react'; |
| 11 | +import { PasswordCriterion } from './PasswordCriterion'; |
| 12 | +import apiClient from '@api/apiClient'; |
| 13 | + |
| 14 | +interface ResetPasswordFormProps { |
| 15 | + email: string; |
| 16 | + onSuccess?: () => void; |
| 17 | +} |
| 18 | + |
| 19 | +export const ResetPasswordForm: React.FC<ResetPasswordFormProps> = ({ |
| 20 | + email, |
| 21 | + onSuccess, |
| 22 | +}) => { |
| 23 | + const [confirmationCode, setConfirmationCode] = useState(''); |
| 24 | + const [newPassword, setNewPassword] = useState(''); |
| 25 | + const [confirmPassword, setConfirmPassword] = useState(''); |
| 26 | + const [showPassword, setShowPassword] = useState(false); |
| 27 | + const [showConfirmPassword, setShowConfirmPassword] = useState(false); |
| 28 | + const [error, setError] = useState(''); |
| 29 | + const [isLoading, setIsLoading] = useState(false); |
| 30 | + |
| 31 | + const hasMinLength = newPassword.length >= 8; |
| 32 | + const hasUppercase = /[A-Z]/.test(newPassword); |
| 33 | + const hasLowercase = /[a-z]/.test(newPassword); |
| 34 | + const hasNumber = /\d/.test(newPassword); |
| 35 | + const hasSpecialChar = /[^A-Za-z0-9]/.test(newPassword); |
| 36 | + const passwordsMatch = |
| 37 | + newPassword.length > 0 && |
| 38 | + confirmPassword.length > 0 && |
| 39 | + newPassword === confirmPassword; |
| 40 | + const allCriteriaMet = |
| 41 | + hasMinLength && |
| 42 | + hasUppercase && |
| 43 | + hasLowercase && |
| 44 | + hasNumber && |
| 45 | + hasSpecialChar && |
| 46 | + passwordsMatch; |
| 47 | + |
| 48 | + const handleSubmit = async (e: React.FormEvent) => { |
| 49 | + e.preventDefault(); |
| 50 | + setError(''); |
| 51 | + setIsLoading(true); |
| 52 | + |
| 53 | + try { |
| 54 | + await apiClient.confirmPassword({ |
| 55 | + email, |
| 56 | + confirmationCode, |
| 57 | + newPassword, |
| 58 | + }); |
| 59 | + onSuccess?.(); |
| 60 | + } catch (err: unknown) { |
| 61 | + if (err instanceof Error) { |
| 62 | + setError(err.message); |
| 63 | + } else { |
| 64 | + setError('Failed to reset password'); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + setIsLoading(false); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <form onSubmit={handleSubmit} className="flex flex-col gap-4" noValidate> |
| 73 | + <div> |
| 74 | + <Label |
| 75 | + htmlFor="confirmation-code" |
| 76 | + className="font-semibold mb-1 text-[#404040]" |
| 77 | + > |
| 78 | + Confirmation Code |
| 79 | + </Label> |
| 80 | + <Input |
| 81 | + id="confirmation-code" |
| 82 | + type="text" |
| 83 | + placeholder="Enter code from email" |
| 84 | + required |
| 85 | + value={confirmationCode} |
| 86 | + onChange={(e) => setConfirmationCode(e.target.value)} |
| 87 | + className={`w-full py-5 focus:ring-[2.5px] focus:ring-[#007B64] ${ |
| 88 | + error ? 'ring-[2.5px] ring-[#B4444D] bg-[#FFFAFA]' : '' |
| 89 | + }`} |
| 90 | + /> |
| 91 | + </div> |
| 92 | + |
| 93 | + <div> |
| 94 | + <Label |
| 95 | + htmlFor="new-password" |
| 96 | + className="font-semibold mb-1 text-[#404040]" |
| 97 | + > |
| 98 | + New Password |
| 99 | + </Label> |
| 100 | + <InputGroup |
| 101 | + className={`w-full focus-within:border-[#007B64] focus-within:ring-[2.5px] focus-within:ring-[#007B64] py-5 ${ |
| 102 | + error ? 'border-[#B4444D] ring-[2px] ring-[#B4444D]' : '' |
| 103 | + }`} |
| 104 | + > |
| 105 | + <InputGroupInput |
| 106 | + id="new-password" |
| 107 | + type={showPassword ? 'text' : 'password'} |
| 108 | + value={newPassword} |
| 109 | + onChange={(e) => setNewPassword(e.target.value)} |
| 110 | + placeholder="Password" |
| 111 | + className={`focus:ring-[#007B64] ${error ? 'bg-[#FFFAFA]' : ''}`} |
| 112 | + /> |
| 113 | + <InputGroupAddon |
| 114 | + align="inline-end" |
| 115 | + className="hover:cursor-pointer" |
| 116 | + onClick={() => setShowPassword(!showPassword)} |
| 117 | + > |
| 118 | + {showPassword ? <EyeIcon /> : <EyeOffIcon />} |
| 119 | + </InputGroupAddon> |
| 120 | + </InputGroup> |
| 121 | + </div> |
| 122 | + |
| 123 | + <div> |
| 124 | + <Label |
| 125 | + htmlFor="confirm-password" |
| 126 | + className="font-semibold mb-1 text-[#404040]" |
| 127 | + > |
| 128 | + Confirm Password |
| 129 | + </Label> |
| 130 | + <InputGroup className="w-full focus-within:border-[#007B64] focus-within:ring-[2.5px] focus-within:ring-[#007B64] py-5"> |
| 131 | + <InputGroupInput |
| 132 | + id="confirm-password" |
| 133 | + type={showConfirmPassword ? 'text' : 'password'} |
| 134 | + value={confirmPassword} |
| 135 | + onChange={(e) => setConfirmPassword(e.target.value)} |
| 136 | + placeholder="Re-enter Password" |
| 137 | + className="focus:ring-[#007B64]" |
| 138 | + /> |
| 139 | + <InputGroupAddon |
| 140 | + align="inline-end" |
| 141 | + className="hover:cursor-pointer" |
| 142 | + onClick={() => setShowConfirmPassword(!showConfirmPassword)} |
| 143 | + > |
| 144 | + {showConfirmPassword ? <EyeIcon /> : <EyeOffIcon />} |
| 145 | + </InputGroupAddon> |
| 146 | + </InputGroup> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="flex gap-1 flex-wrap w-full"> |
| 150 | + <PasswordCriterion name="8+ characters" criterionMet={hasMinLength} /> |
| 151 | + <PasswordCriterion name="Uppercase" criterionMet={hasUppercase} /> |
| 152 | + <PasswordCriterion name="Lowercase" criterionMet={hasLowercase} /> |
| 153 | + <PasswordCriterion |
| 154 | + name="Special character" |
| 155 | + criterionMet={hasSpecialChar} |
| 156 | + /> |
| 157 | + <PasswordCriterion name="Number" criterionMet={hasNumber} /> |
| 158 | + <PasswordCriterion name="Matching" criterionMet={passwordsMatch} /> |
| 159 | + </div> |
| 160 | + |
| 161 | + {error && <p className="text-sm text-[#B4444D]">{error}</p>} |
| 162 | + |
| 163 | + <Button |
| 164 | + id="reset-password-submit-btn" |
| 165 | + type="submit" |
| 166 | + disabled={!confirmationCode || !allCriteriaMet || isLoading} |
| 167 | + className={`py-5 h-14 rounded-full font-semibold text-xl text-white ${ |
| 168 | + !confirmationCode || !allCriteriaMet || isLoading |
| 169 | + ? 'bg-[#737373] cursor-not-allowed' |
| 170 | + : 'bg-[#007B64]' |
| 171 | + }`} |
| 172 | + > |
| 173 | + {isLoading ? '...' : 'Reset Password'} |
| 174 | + </Button> |
| 175 | + </form> |
| 176 | + ); |
| 177 | +}; |
0 commit comments