ente/src/components/ChangeEmail.tsx

177 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-08-05 06:18:50 +00:00
import { Formik, FormikHelpers } from 'formik';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { Button, Col, Form, FormControl } from 'react-bootstrap';
import * as Yup from 'yup';
import constants from 'utils/strings/constants';
import SubmitButton from 'components/SubmitButton';
import router from 'next/router';
import { changeEmail, getOTTForEmailChange } from 'services/userService';
import styled from 'styled-components';
2021-08-13 03:57:09 +00:00
import { AppContext, FLASH_MESSAGE_TYPE } from 'pages/_app';
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { PAGES } from 'types';
2021-08-05 06:18:50 +00:00
interface formValues {
email: string;
2021-08-13 02:38:38 +00:00
ott?: string;
2021-08-05 06:18:50 +00:00
}
2021-08-13 02:38:38 +00:00
const EmailRow = styled.div`
2021-08-05 06:18:50 +00:00
display: flex;
flex-wrap: wrap;
border: 1px solid grey;
margin-bottom: 19px;
align-items: center;
text-align: left;
color: #fff;
`;
2021-08-13 02:38:38 +00:00
interface Props {
showMessage: (value: boolean) => void;
setEmail: (email: string) => void;
2021-08-05 06:18:50 +00:00
}
2021-08-13 02:38:38 +00:00
function ChangeEmailForm(props: Props) {
const [loading, setLoading] = useState(false);
const [ottInputVisible, setShowOttInputVisibility] = useState(false);
2021-08-05 06:18:50 +00:00
const emailInputElement = useRef(null);
2021-08-13 02:38:38 +00:00
const ottInputRef = useRef(null);
2021-08-05 06:18:50 +00:00
const appContext = useContext(AppContext);
useEffect(() => {
setTimeout(() => {
emailInputElement.current?.focus();
}, 250);
}, []);
2021-08-13 02:38:38 +00:00
useEffect(() => {
2021-08-05 06:19:19 +00:00
if (!ottInputVisible) {
2021-08-05 06:18:50 +00:00
props.showMessage(false);
}
2021-08-05 06:19:19 +00:00
}, [ottInputVisible]);
2021-08-05 06:18:50 +00:00
2021-08-13 02:38:38 +00:00
const requestOTT = async (
{ email }: formValues,
{ setFieldError }: FormikHelpers<formValues>
) => {
2021-08-05 06:18:50 +00:00
try {
setLoading(true);
await getOTTForEmailChange(email);
props.setEmail(email);
setShowOttInputVisibility(true);
props.showMessage(true);
setTimeout(() => {
ottInputRef.current?.focus();
}, 250);
} catch (e) {
setFieldError('email', `${constants.EMAIl_ALREADY_OWNED}`);
}
setLoading(false);
};
2021-08-13 02:38:38 +00:00
const requestEmailChange = async (
{ email, ott }: formValues,
{ setFieldError }: FormikHelpers<formValues>
) => {
2021-08-05 06:18:50 +00:00
try {
setLoading(true);
await changeEmail(email, ott);
setData(LS_KEYS.USER, { ...getData(LS_KEYS.USER), email });
2021-08-13 02:38:38 +00:00
appContext.setDisappearingFlashMessage({
message: constants.EMAIL_UDPATE_SUCCESSFUL,
2021-08-13 03:57:09 +00:00
type: FLASH_MESSAGE_TYPE.SUCCESS,
2021-08-13 02:38:38 +00:00
});
router.push(PAGES.GALLERY);
2021-08-05 06:18:50 +00:00
} catch (e) {
setFieldError('ott', `${constants.INCORRECT_CODE}`);
2021-08-05 06:18:50 +00:00
}
setLoading(false);
};
return (
<Formik<formValues>
initialValues={{ email: '' }}
validationSchema={Yup.object().shape({
email: Yup.string()
.email(constants.EMAIL_ERROR)
.required(constants.REQUIRED),
})}
validateOnChange={false}
validateOnBlur={false}
2021-08-13 02:38:38 +00:00
onSubmit={!ottInputVisible ? requestOTT : requestEmailChange}>
{({ values, errors, touched, handleChange, handleSubmit }) => (
2021-08-05 06:18:50 +00:00
<Form noValidate onSubmit={handleSubmit}>
2021-08-13 02:38:38 +00:00
{!ottInputVisible ? (
<Form.Group controlId="formBasicEmail">
2021-08-05 06:18:50 +00:00
<Form.Control
ref={emailInputElement}
type="email"
placeholder={constants.ENTER_EMAIL}
value={values.email}
onChange={handleChange('email')}
isInvalid={Boolean(
2021-08-13 02:38:38 +00:00
touched.email && errors.email
2021-08-05 06:18:50 +00:00
)}
autoFocus
disabled={loading}
/>
<FormControl.Feedback type="invalid">
{errors.email}
</FormControl.Feedback>
2021-08-12 15:40:02 +00:00
</Form.Group>
2021-08-13 02:38:38 +00:00
) : (
<>
2021-08-05 06:18:50 +00:00
<EmailRow>
2021-08-13 02:38:38 +00:00
<Col xs="8">{values.email}</Col>
<Col xs="4">
<Button
variant="link"
onClick={() =>
setShowOttInputVisibility(false)
}>
2021-08-06 12:22:45 +00:00
{constants.CHANGE}
2021-08-05 06:18:50 +00:00
</Button>
</Col>
</EmailRow>
<Form.Group controlId="formBasicEmail">
<Form.Control
ref={ottInputRef}
type="text"
placeholder={constants.ENTER_OTT}
value={values.ott}
onChange={handleChange('ott')}
isInvalid={Boolean(
2021-08-13 02:38:38 +00:00
touched.ott && errors.ott
2021-08-05 06:18:50 +00:00
)}
disabled={loading}
/>
<FormControl.Feedback type="invalid">
{errors.ott}
</FormControl.Feedback>
</Form.Group>
2021-08-13 02:38:38 +00:00
</>
)}
2021-08-05 06:18:50 +00:00
<SubmitButton
2021-08-13 02:38:38 +00:00
buttonText={
!ottInputVisible
? constants.SEND_OTT
: constants.VERIFY
}
2021-08-05 06:18:50 +00:00
loading={loading}
/>
<br />
2021-08-13 02:38:38 +00:00
<Button
block
variant="link"
className="text-center"
onClick={router.back}>
2021-08-05 06:18:50 +00:00
{constants.GO_BACK}
</Button>
</Form>
)}
</Formik>
);
}
export default ChangeEmailForm;