'use client' import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import useSWR from 'swr' import { useSearchParams } from 'next/navigation' import cn from 'classnames' import { CheckCircleIcon } from '@heroicons/react/24/solid' import Button from '@/app/components/base/button' import { changePasswordWithToken, verifyForgotPasswordToken } from '@/service/common' import Toast from '@/app/components/base/toast' import Loading from '@/app/components/base/loading' const validPassword = /^(?=.*[a-zA-Z])(?=.*\d).{8,}$/ const ChangePasswordForm = () => { const { t } = useTranslation() const searchParams = useSearchParams() const token = searchParams.get('token') const verifyTokenParams = { url: '/forgot-password/validity', body: { token }, } const { data: verifyTokenRes, mutate: revalidateToken } = useSWR(verifyTokenParams, verifyForgotPasswordToken, { revalidateOnFocus: false, }) const [password, setPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [showSuccess, setShowSuccess] = useState(false) const showErrorMessage = useCallback((message: string) => { Toast.notify({ type: 'error', message, }) }, []) const valid = useCallback(() => { if (!password.trim()) { showErrorMessage(t('login.error.passwordEmpty')) return false } if (!validPassword.test(password)) { showErrorMessage(t('login.error.passwordInvalid')) return false } if (password !== confirmPassword) { showErrorMessage(t('common.account.notEqual')) return false } return true }, [password, confirmPassword, showErrorMessage, t]) const handleChangePassword = useCallback(async () => { const token = searchParams.get('token') || '' if (!valid()) return try { await changePasswordWithToken({ url: '/forgot-password/resets', body: { token, new_password: password, password_confirm: confirmPassword, }, }) setShowSuccess(true) } catch { await revalidateToken() } }, [password, revalidateToken, token, valid]) return (
{!verifyTokenRes && } {verifyTokenRes && !verifyTokenRes.is_valid && (
🤷‍♂️

{t('login.invalid')}

)} {verifyTokenRes && verifyTokenRes.is_valid && !showSuccess && (

{t('login.changePassword')}

{t('login.changePasswordTip')}

{/* Password */}
setPassword(e.target.value)} placeholder={t('login.passwordPlaceholder') || ''} className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'} />
{t('login.error.passwordInvalid')}
{/* Confirm Password */}
setConfirmPassword(e.target.value)} placeholder={t('login.confirmPasswordPlaceholder') || ''} className={'appearance-none block w-full rounded-lg pl-[14px] px-3 py-2 border border-gray-200 hover:border-gray-300 hover:shadow-sm focus:outline-none focus:ring-primary-500 focus:border-primary-500 placeholder-gray-400 caret-primary-600 sm:text-sm pr-10'} />
)} {verifyTokenRes && verifyTokenRes.is_valid && showSuccess && (

{t('login.passwordChangedTip')}

)}
) } export default ChangePasswordForm