bigbuffet-rw/app/soapbox/features/verification/steps/sms-verification.tsx

152 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-06-13 08:34:59 -07:00
import { AxiosError } from 'axios';
import React from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
2022-03-21 11:09:01 -07:00
import OtpInput from 'react-otp-input';
import { confirmPhoneVerification, requestPhoneVerification } from 'soapbox/actions/verification';
2022-07-13 07:42:58 -07:00
import { Button, Form, FormGroup, PhoneInput, Text } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
2022-12-20 07:47:46 -08:00
import toast from 'soapbox/toast';
2022-03-21 11:09:01 -07:00
const messages = defineMessages({
verificationInvalid: { id: 'sms_verification.invalid', defaultMessage: 'Please enter a valid phone number.' },
verificationSuccess: { id: 'sms_verification.success', defaultMessage: 'A verification code has been sent to your phone number.' },
verificationFail: { id: 'sms_verification.fail', defaultMessage: 'Failed to send SMS message to your phone number.' },
verificationExpired: { id: 'sms_verification.expired', defaultMessage: 'Your SMS token has expired.' },
phoneLabel: { id: 'sms_verification.phone.label', defaultMessage: 'Phone number' },
});
2022-03-21 11:09:01 -07:00
const Statuses = {
IDLE: 'IDLE',
REQUESTED: 'REQUESTED',
FAIL: 'FAIL',
};
const SmsVerification = () => {
const intl = useIntl();
const dispatch = useAppDispatch();
2022-03-21 11:09:01 -07:00
const isLoading = useAppSelector((state) => state.verification.isLoading) as boolean;
2022-03-21 11:09:01 -07:00
const [phone, setPhone] = React.useState<string>();
2022-03-21 11:09:01 -07:00
const [status, setStatus] = React.useState(Statuses.IDLE);
const [verificationCode, setVerificationCode] = React.useState('');
const [requestedAnother, setAlreadyRequestedAnother] = React.useState(false);
2022-07-13 17:08:19 -07:00
const isValid = !!phone;
2022-03-21 11:09:01 -07:00
const onChange = React.useCallback((phone?: string) => {
2022-07-13 07:42:58 -07:00
setPhone(phone);
2022-03-21 11:09:01 -07:00
}, []);
2023-01-10 15:03:15 -08:00
const handleSubmit: React.FormEventHandler = React.useCallback((event) => {
2022-03-21 11:09:01 -07:00
event.preventDefault();
if (!isValid) {
setStatus(Statuses.IDLE);
2022-12-20 07:47:46 -08:00
toast.error(intl.formatMessage(messages.verificationInvalid));
2022-03-21 11:09:01 -07:00
return;
}
dispatch(requestPhoneVerification(phone!)).then(() => {
2022-12-20 07:47:46 -08:00
toast.success(intl.formatMessage(messages.verificationSuccess));
2022-03-21 11:09:01 -07:00
setStatus(Statuses.REQUESTED);
2022-06-13 08:34:59 -07:00
}).catch((error: AxiosError) => {
const message = (error.response?.data as any)?.message || intl.formatMessage(messages.verificationFail);
2022-06-13 08:34:59 -07:00
2022-12-20 07:47:46 -08:00
toast.error(message);
2022-03-21 11:09:01 -07:00
setStatus(Statuses.FAIL);
});
}, [phone, isValid]);
2023-01-10 15:03:15 -08:00
const resendVerificationCode: React.MouseEventHandler = React.useCallback((event) => {
2022-03-21 11:09:01 -07:00
setAlreadyRequestedAnother(true);
handleSubmit(event);
}, [isValid]);
const submitVerification = () => {
// TODO: handle proper validation from Pepe -- expired vs invalid
dispatch(confirmPhoneVerification(verificationCode))
2022-12-20 07:47:46 -08:00
.catch(() => {
toast.error(intl.formatMessage(messages.verificationExpired));
});
2022-03-21 11:09:01 -07:00
};
React.useEffect(() => {
if (verificationCode.length === 6) {
submitVerification();
}
}, [verificationCode]);
if (status === Statuses.REQUESTED) {
return (
<div>
<div className='pb-4 sm:pb-10 mb-4 border-b border-gray-200 dark:border-gray-800 border-solid -mx-4 sm:-mx-10'>
2022-03-21 11:09:01 -07:00
<h1 className='text-center font-bold text-2xl'>
<FormattedMessage id='sms_verification.sent.header' defaultMessage='Verification code' />
2022-03-21 11:09:01 -07:00
</h1>
</div>
<div className='sm:pt-10 sm:w-2/3 md:w-1/2 mx-auto space-y-4'>
<Text theme='muted' size='sm' align='center'>
<FormattedMessage id='sms_verification.sent.body' defaultMessage='We sent you a 6-digit code via SMS. Enter it below.' />
2022-03-21 11:09:01 -07:00
</Text>
<OtpInput
value={verificationCode}
onChange={setVerificationCode}
numInputs={6}
isInputNum
shouldAutoFocus
isDisabled={isLoading}
containerStyle='flex justify-center mt-2 space-x-4'
inputStyle='w-10i border-gray-300 dark:bg-gray-800 dark:border-gray-800 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500'
2022-03-21 11:09:01 -07:00
/>
<div className='text-center'>
<Button
size='sm'
type='button'
theme='tertiary'
2022-03-21 11:09:01 -07:00
onClick={resendVerificationCode}
disabled={requestedAnother}
>
<FormattedMessage id='sms_verification.sent.actions.resend' defaultMessage='Resend verification code?' />
2022-03-21 11:09:01 -07:00
</Button>
</div>
</div>
</div>
);
}
return (
<div>
<div className='pb-4 sm:pb-10 mb-4 border-b border-gray-200 dark:border-gray-800 border-solid -mx-4 sm:-mx-10'>
<h1 className='text-center font-bold text-2xl'>
<FormattedMessage id='sms_verification.header' defaultMessage='Enter your phone number' />
</h1>
2022-03-21 11:09:01 -07:00
</div>
<div className='sm:pt-10 sm:w-2/3 md:w-1/2 mx-auto'>
2022-04-04 08:54:00 -07:00
<Form onSubmit={handleSubmit}>
<FormGroup labelText={intl.formatMessage(messages.phoneLabel)}>
2022-07-13 07:42:58 -07:00
<PhoneInput
2022-03-21 11:09:01 -07:00
value={phone}
onChange={onChange}
required
/>
</FormGroup>
<div className='text-center'>
<Button block theme='primary' type='submit' disabled={isLoading || !isValid}>
<FormattedMessage id='onboarding.next' defaultMessage='Next' />
</Button>
2022-03-21 11:09:01 -07:00
</div>
</Form>
</div>
</div>
);
};
2022-07-13 17:08:19 -07:00
export { SmsVerification as default };