import * as React from 'react'; import { useIntl } from 'react-intl'; import OtpInput from 'react-otp-input'; import { useDispatch, useSelector } from 'react-redux'; import snackbar from 'soapbox/actions/snackbar'; import { confirmPhoneVerification, requestPhoneVerification } from 'soapbox/actions/verification'; import { formatPhoneNumber } from 'soapbox/utils/phone'; import { Button, Form, FormGroup, Input, Text } from '../../../components/ui'; const Statuses = { IDLE: 'IDLE', REQUESTED: 'REQUESTED', FAIL: 'FAIL', }; const validPhoneNumberRegex = /^\+1\s\(\d{3}\)\s\d{3}-\d{4}/; const SmsVerification = () => { const intl = useIntl(); const dispatch = useDispatch(); const isLoading = useSelector((state) => state.verification.get('isLoading')); const [phone, setPhone] = React.useState(''); const [status, setStatus] = React.useState(Statuses.IDLE); const [verificationCode, setVerificationCode] = React.useState(''); const [requestedAnother, setAlreadyRequestedAnother] = React.useState(false); const isValid = validPhoneNumberRegex.test(phone); const onChange = React.useCallback((event) => { const formattedPhone = formatPhoneNumber(event.target.value); setPhone(formattedPhone); }, []); const handleSubmit = React.useCallback((event) => { event.preventDefault(); if (!isValid) { setStatus(Statuses.IDLE); dispatch( snackbar.error( intl.formatMessage({ id: 'sms_verification.invalid', defaultMessage: 'Please enter a valid phone number.', }), ), ); return; } dispatch(requestPhoneVerification(phone)).then(() => { dispatch( snackbar.success( intl.formatMessage({ id: 'sms_verification.success', defaultMessage: 'A verification code has been sent to your phone number.', }), ), ); setStatus(Statuses.REQUESTED); }).catch(() => { dispatch( snackbar.error( intl.formatMessage({ id: 'sms_verification.fail', defaultMessage: 'Failed to send SMS message to your phone number.', }), ), ); setStatus(Statuses.FAIL); }); }, [phone, isValid]); const resendVerificationCode = React.useCallback((event) => { setAlreadyRequestedAnother(true); handleSubmit(event); }, [isValid]); const submitVerification = () => { // TODO: handle proper validation from Pepe -- expired vs invalid dispatch(confirmPhoneVerification(verificationCode)) .catch(() => dispatch( snackbar.error( intl.formatMessage({ id: 'sms_verification.invalid', defaultMessage: 'Your SMS token has expired.', }), ), )); }; React.useEffect(() => { if (verificationCode.length === 6) { submitVerification(); } }, [verificationCode]); if (status === Statuses.REQUESTED) { return (