import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { useForm, useWatch } from 'react-hook-form'; import axios from 'axios'; import { useTranslation } from 'react-i18next'; import Field from '../../common/Field'; import ArrowBigRight from '../../icons/ArrowBigRight'; import RoundedButton from '../../common/RoundedButton'; import { ForgotPasswordForm } from '../../../model/components/forgotPassword'; import { CustomResponse } from '../../../model/common'; const regExp = new RegExp( /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/, 'i', ); const ForgotPassword: React.FC = () => { const [loading, setLoading] = useState(false); const [changed, setChanged] = useState(false); const { t } = useTranslation(); const { formState: { errors }, setError, handleSubmit, register, control, } = useForm(); const emailField = useWatch({ control, name: 'email' }); const [valid, setValid] = useState(false); const [invalid, setInvalid] = useState(false); useEffect(() => { if (emailField && emailField.length) { if (!regExp.test(emailField)) { setInvalid(true); setValid(false); } else { setValid(true); setInvalid(false); } } else { setValid(false); setInvalid(false); } }, [emailField]); const onSubmit = ({ email }: ForgotPasswordForm): void => { setLoading(true); setError('email', null); axios .post>('/api/forgotPassword', { email, }) .then((data) => { const { ok, error: apiError } = data.data; if (!ok) { setError('email', { message: apiError }); } else { setChanged(true); } setLoading(false); }) .catch((err) => { console.error(err.stack || err.message || err); setLoading(false); }); }; return (

Recuperar
ContraseƱa

Escribe tu email.

Te mandaremos un correo al email para que cambies tu contraseƱa por una nueva.

{changed ? (

{t('EMAIL_SENT')}

) : (
} />
)}

{changed ? t('LOGIN') : t('CANCEL')}

); }; export default ForgotPassword;