import { AxiosError } from 'axios'; import React from 'react'; import { useIntl } from 'react-intl'; import snackbar from 'soapbox/actions/snackbar'; import { checkEmailVerification, postEmailVerification, requestEmailVerification } from 'soapbox/actions/verification'; import Icon from 'soapbox/components/icon'; import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui'; import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; const Statuses = { IDLE: 'IDLE', REQUESTED: 'REQUESTED', FAIL: 'FAIL', }; const EMAIL_REGEX = /^[^@\s]+@[^@\s]+$/; interface IEmailSent { handleSubmit: React.FormEventHandler, } const EmailSent: React.FC = ({ handleSubmit }) => { const dispatch = useAppDispatch(); const checkEmailConfirmation = () => { dispatch(checkEmailVerification()) .then(() => dispatch(postEmailVerification())) .catch(() => null); }; React.useEffect(() => { const intervalId = setInterval(() => checkEmailConfirmation(), 2500); return () => clearInterval(intervalId); }, []); return (
We sent you an email Click on the link in the email to validate your email.
); }; const EmailVerification = () => { const intl = useIntl(); const dispatch = useAppDispatch(); const isLoading = useAppSelector((state) => state.verification.isLoading) as boolean; const [email, setEmail] = React.useState(''); const [status, setStatus] = React.useState(Statuses.IDLE); const [errors, setErrors] = React.useState>([]); const isValid = email.length > 0 && EMAIL_REGEX.test(email); const onChange = React.useCallback((event) => setEmail(event.target.value), []); const handleSubmit: React.FormEventHandler = React.useCallback((event) => { event.preventDefault(); setErrors([]); submitEmailForVerification(); }, [email]); const submitEmailForVerification = () => { return dispatch(requestEmailVerification((email))) .then(() => { setStatus(Statuses.REQUESTED); dispatch( snackbar.success( intl.formatMessage({ id: 'email_verification.exists', defaultMessage: 'Verification email sent successfully.', }), ), ); }) .catch((error: AxiosError) => { const isEmailTaken = (error.response?.data as any)?.error === 'email_taken'; const message = isEmailTaken ? ( intl.formatMessage({ id: 'email_verification.exists', defaultMessage: 'This email has already been taken.' }) ) : ( intl.formatMessage({ id: 'email_verification.fail', defaultMessage: 'Failed to request email verification.' }) ); if (isEmailTaken) { setErrors([intl.formatMessage({ id: 'email_verification.taken', defaultMessage: 'is taken' })]); } dispatch(snackbar.error(message)); setStatus(Statuses.FAIL); }); }; if (status === Statuses.REQUESTED) { return ; } return (

{intl.formatMessage({ id: 'email_verification.header', defaultMessage: 'Enter your email address' })}

); }; export default EmailVerification;