import PropTypes from 'prop-types'; import * as React from 'react'; import { useIntl } from 'react-intl'; import { useDispatch, useSelector } from 'react-redux'; import snackbar from 'soapbox/actions/snackbar'; import { checkEmailVerification, requestEmailVerification } from 'soapbox/actions/verification'; import { postEmailVerification } from 'soapbox/actions/verification'; import Icon from 'soapbox/components/icon'; import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui'; const Statuses = { IDLE: 'IDLE', REQUESTED: 'REQUESTED', FAIL: 'FAIL', }; const EMAIL_REGEX = /^[^@\s]+@[^@\s]+$/; const EmailSent = ({ handleSubmit }) => { const dispatch = useDispatch(); 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 = useDispatch(); const isLoading = useSelector((state) => state.verification.get('isLoading')); 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.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 => { const isEmailTaken = error.response?.data?.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' })}

); }; EmailSent.propTypes = { handleSubmit: PropTypes.func.isRequired, }; export default EmailVerification;