import React, { useState } from 'react'; import { Loader, Icon } from 'UI'; import { observer } from 'mobx-react-lite'; import { useStore } from 'App/mstore'; import { Form, Input, Button, Typography } from 'antd'; import { SquareArrowOutUpRight } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import withCaptcha, { WithCaptchaProps } from 'App/withRecaptcha'; interface Props { } function ResetPasswordRequest(props: Props & WithCaptchaProps) { const { t } = useTranslation(); const { userStore } = useStore(); const { loading } = userStore; const { requestResetPassword } = userStore; const [requested, setRequested] = useState(false); const [email, setEmail] = useState(''); const [error, setError] = useState(null); const [smtpError, setSmtpError] = useState(false); const { submitWithCaptcha, isVerifyingCaptcha, resetCaptcha } = props; const write = (e: any) => { const { name, value } = e.target; if (name === 'email') setEmail(value); }; const onSubmit = () => { // Validation check if (!email || email.trim() === '') { return; } submitWithCaptcha({ email: email.trim() }) .then((data) => { handleSubmit(data['g-recaptcha-response']); }) .catch((error: any) => { console.error('Captcha verification failed:', error); }); }; const handleSubmit = (token?: string) => { setError(null); requestResetPassword({ email: email.trim(), 'g-recaptcha-response': token }) .catch((err: any) => { if (err.message?.toLowerCase().includes('smtp')) { setSmtpError(true); } setError(err.message); // Reset captcha for the next attempt resetCaptcha(); }) .finally(() => { setRequested(true); }); }; return (
{!requested && ( <> } required /> )} {requested && !error && (
{t('Alright! a reset link was emailed to')}{' '} {email}.{' '} {t('Click on it to reset')} {t('your account password.')}
)} {error && (
{smtpError ? ( {t('Email delivery failed due to invalid SMTP configuration. Please contact your admin.')} {t('Learn More')} ) : ( {error} )}
)}
); } export default withCaptcha(observer(ResetPasswordRequest));