import { AxiosError } from 'axios'; import React from 'react'; import { defineMessages, FormattedMessage, 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 messages = defineMessages({ verificationSuccess: { id: 'email_verification.success', defaultMessage: 'Verification email sent successfully.' }, verificationFail: { id: 'email_verification.fail', defaultMessage: 'Failed to request email verification.' }, verificationFailTakenAlert: { id: 'email_verifilcation.exists', defaultMessage: 'This email has already been taken.' }, verificationFailTaken: { id: 'email_verification.taken', defaultMessage: 'is taken' }, emailLabel: { id: 'email_verification.email.label', defaultMessage: 'E-mail address' }, }); 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(messages.verificationSuccess), ), ); }) .catch((error: AxiosError) => { const errorMessage = (error.response?.data as any)?.error; const isEmailTaken = errorMessage === 'email_taken'; let message = intl.formatMessage(messages.verificationFail); if (isEmailTaken) { message = intl.formatMessage(messages.verificationFailTakenAlert); } else if (errorMessage) { message = errorMessage; } if (isEmailTaken) { setErrors([intl.formatMessage(messages.verificationFailTaken)]); } dispatch(snackbar.error(message)); setStatus(Statuses.FAIL); }); }; if (status === Statuses.REQUESTED) { return ; } return (

); }; export default EmailVerification;