import { useState, useContext, useEffect, useCallback, MouseEvent } from 'react'; import { AccountUserProfileContext } from '../hooks'; import { toast } from 'sonner'; import { useAuthContext } from '@/auth'; import { KeenIcon } from '@/components'; import clsx from 'clsx'; type PasswordType = 'password' | 'retype_password' | 'current_password'; const PinCode = () => { const { setPassword } = useContext(AccountUserProfileContext); const { getUser } = useAuthContext(); const [newPassword, setNewPassword] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [messagePassword, setMessagePassword] = useState(true); const [showPassword, setShowPassword] = useState({ current_password: false, password: false, retype_password: false }); const [passwordErrors, setPasswordErrors] = useState([]); const validatePassword = (password: string) => { const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); const hasCapitalLetter = /[A-Z]/.test(password); const hasNumber = /[0-9]/.test(password); return hasSpecialChar && hasCapitalLetter && hasNumber; }; useEffect(() => { const passwordsMatch = newPassword === confirmPassword; const isPasswordValid = validatePassword(newPassword); setMessagePassword(passwordsMatch && isPasswordValid); const errors: string[] = []; if (newPassword) { if (!/[A-Z]/.test(newPassword)) { errors.push('Password must contain at least one capital letter'); } if (!/[0-9]/.test(newPassword)) { errors.push('Password must contain at least one number'); } if (!/[!@#$%^&*(),.?":{}|<>]/.test(newPassword)) { errors.push('Password must contain at least one special character'); } if (newPassword !== confirmPassword) { errors.push('Passwords do not match'); } } setPasswordErrors(errors); }, [newPassword, confirmPassword]); const handleResetPassword = useCallback(async () => { const user: any = await getUser(); if (!messagePassword) { toast.error('Passwords do not match!'); return; } setIsSubmitting(true); try { await setPassword({ current_password: currentPassword, password: newPassword, retype_password: confirmPassword, username: user.data.username ?? '' }); setNewPassword(''); setConfirmPassword(''); setCurrentPassword(''); } catch (error: any) { const errorMessage = error?.response?.data?.message || error?.message || 'An error occurred while resetting the password.'; toast.error(errorMessage); } finally { setIsSubmitting(false); } }, [currentPassword, newPassword, newPassword, messagePassword, getUser]); const isButtonDisabled = isSubmitting || !newPassword || !confirmPassword || !messagePassword; const togglePassword = useCallback((event: MouseEvent, key: string) => { event.preventDefault(); setShowPassword((prev) => ({ ...prev, [key]: !prev[key as PasswordType] })); }, []); return (

Pin Code

setCurrentPassword(e.target.value)} disabled={isSubmitting} />
setNewPassword(e.target.value)} disabled={isSubmitting} type={showPassword.password ? 'text' : 'password'} />
setConfirmPassword(e.target.value)} disabled={isSubmitting} />
{passwordErrors.length > 0 && (
{passwordErrors.map((error, index) => (

{error}

))}
)}
); }; export { PinCode };