2021-06-21 15:22:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
|
|
import { Formik, FormikHelpers } from 'formik';
|
|
|
|
import { DeadCenter } from 'pages/gallery';
|
2021-06-26 05:46:42 +00:00
|
|
|
import React, { useRef, useState } from 'react';
|
2021-06-26 10:51:15 +00:00
|
|
|
import { Form } from 'react-bootstrap';
|
2021-06-21 15:22:21 +00:00
|
|
|
import OtpInput from 'react-otp-input';
|
|
|
|
import constants from 'utils/strings/constants';
|
|
|
|
import SubmitButton from './SubmitButton';
|
|
|
|
|
|
|
|
interface formValues {
|
|
|
|
otp: string;
|
|
|
|
}
|
|
|
|
interface Props {
|
2021-08-13 02:38:38 +00:00
|
|
|
onSubmit: any;
|
|
|
|
back: any;
|
2021-06-21 15:22:21 +00:00
|
|
|
buttonText: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function VerifyTwoFactor(props: Props) {
|
|
|
|
const [waiting, setWaiting] = useState(false);
|
2021-06-26 05:46:42 +00:00
|
|
|
const otpInputRef = useRef(null);
|
2021-06-21 15:22:21 +00:00
|
|
|
const submitForm = async (
|
|
|
|
{ otp }: formValues,
|
2021-08-13 02:38:38 +00:00
|
|
|
{ setFieldError, resetForm }: FormikHelpers<formValues>
|
2021-06-21 15:22:21 +00:00
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
setWaiting(true);
|
2021-06-22 05:37:47 +00:00
|
|
|
await props.onSubmit(otp);
|
2021-06-21 15:22:21 +00:00
|
|
|
} catch (e) {
|
2021-06-22 06:58:26 +00:00
|
|
|
resetForm();
|
2021-06-26 05:46:42 +00:00
|
|
|
for (let i = 0; i < 6; i++) {
|
|
|
|
otpInputRef.current?.focusPrevInput();
|
|
|
|
}
|
2021-06-21 15:22:21 +00:00
|
|
|
setFieldError('otp', `${constants.UNKNOWN_ERROR} ${e.message}`);
|
|
|
|
}
|
|
|
|
setWaiting(false);
|
|
|
|
};
|
2021-06-26 05:46:42 +00:00
|
|
|
|
2021-08-13 02:38:38 +00:00
|
|
|
const onChange = (
|
|
|
|
otp: string,
|
|
|
|
callback: Function,
|
|
|
|
triggerSubmit: Function
|
|
|
|
) => {
|
2021-06-26 05:46:42 +00:00
|
|
|
callback(otp);
|
|
|
|
if (otp.length === 6) {
|
|
|
|
triggerSubmit(otp);
|
|
|
|
}
|
|
|
|
};
|
2021-06-21 15:22:21 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-08-13 02:38:38 +00:00
|
|
|
<p style={{ marginBottom: '30px' }}>
|
|
|
|
enter the 6-digit code from your authenticator app.
|
|
|
|
</p>
|
2021-06-21 15:22:21 +00:00
|
|
|
<Formik<formValues>
|
|
|
|
initialValues={{ otp: '' }}
|
|
|
|
validateOnChange={false}
|
|
|
|
validateOnBlur={false}
|
2021-08-13 02:38:38 +00:00
|
|
|
onSubmit={submitForm}>
|
2021-06-21 15:22:21 +00:00
|
|
|
{({
|
|
|
|
values,
|
|
|
|
errors,
|
|
|
|
handleChange,
|
|
|
|
handleSubmit,
|
2021-06-26 05:46:42 +00:00
|
|
|
submitForm,
|
2021-06-21 15:22:21 +00:00
|
|
|
}) => (
|
2021-08-13 02:38:38 +00:00
|
|
|
<Form
|
|
|
|
noValidate
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
style={{ width: '100%' }}>
|
|
|
|
<Form.Group
|
|
|
|
style={{ marginBottom: '32px' }}
|
|
|
|
controlId="formBasicEmail">
|
2021-06-21 15:22:21 +00:00
|
|
|
<DeadCenter>
|
2021-06-26 05:46:42 +00:00
|
|
|
<OtpInput
|
2021-07-01 09:16:21 +00:00
|
|
|
placeholder="123456"
|
2021-06-26 05:46:42 +00:00
|
|
|
ref={otpInputRef}
|
2021-06-26 10:51:15 +00:00
|
|
|
shouldAutoFocus
|
2021-06-26 05:46:42 +00:00
|
|
|
value={values.otp}
|
|
|
|
onChange={(otp) => {
|
2021-08-13 02:38:38 +00:00
|
|
|
onChange(
|
|
|
|
otp,
|
|
|
|
handleChange('otp'),
|
|
|
|
submitForm
|
|
|
|
);
|
2021-06-26 05:46:42 +00:00
|
|
|
}}
|
2021-06-21 15:22:21 +00:00
|
|
|
numInputs={6}
|
|
|
|
separator={'-'}
|
|
|
|
isInputNum
|
|
|
|
className={'otp-input'}
|
|
|
|
/>
|
2021-08-13 02:38:38 +00:00
|
|
|
{errors.otp && (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'block',
|
|
|
|
marginTop: '16px',
|
|
|
|
}}
|
|
|
|
className="invalid-feedback">
|
|
|
|
{constants.INCORRECT_CODE}
|
|
|
|
</div>
|
|
|
|
)}
|
2021-06-21 15:22:21 +00:00
|
|
|
</DeadCenter>
|
|
|
|
</Form.Group>
|
|
|
|
<SubmitButton
|
|
|
|
buttonText={props.buttonText}
|
|
|
|
loading={waiting}
|
|
|
|
disabled={values.otp.length < 6}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|