2022-11-25 20:25:03 +00:00
|
|
|
import { Fragment } from 'react';
|
2019-06-25 04:43:46 +00:00
|
|
|
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-06-25 04:43:46 +00:00
|
|
|
import * as Yup from 'yup';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
2020-07-03 22:37:26 +00:00
|
|
|
import tw from 'twin.macro';
|
2022-07-03 18:27:37 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2019-06-25 04:43:46 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
email: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = Yup.object().shape({
|
|
|
|
email: Yup.string().email().required(),
|
|
|
|
password: Yup.string().required('You must provide your current account password.'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export default () => {
|
2019-07-10 04:25:57 +00:00
|
|
|
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
|
|
|
|
const updateEmail = useStoreActions((state: Actions<ApplicationStore>) => state.user.updateUserEmail);
|
2019-06-25 04:43:46 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-06-25 04:43:46 +00:00
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = (values: Values, { resetForm, setSubmitting }: FormikHelpers<Values>) => {
|
2019-06-25 04:43:46 +00:00
|
|
|
clearFlashes('account:email');
|
|
|
|
|
|
|
|
updateEmail({ ...values })
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(() =>
|
|
|
|
addFlash({
|
|
|
|
type: 'success',
|
|
|
|
key: 'account:email',
|
|
|
|
message: 'Your primary email has been updated.',
|
2022-11-25 20:25:03 +00:00
|
|
|
}),
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error =>
|
2022-06-26 19:13:52 +00:00
|
|
|
addFlash({
|
|
|
|
type: 'error',
|
|
|
|
key: 'account:email',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
2022-11-25 20:25:03 +00:00
|
|
|
}),
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
2019-06-25 04:43:46 +00:00
|
|
|
.then(() => {
|
|
|
|
resetForm();
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-06-26 19:13:52 +00:00
|
|
|
<Formik onSubmit={submit} validationSchema={schema} initialValues={{ email: user!.email, password: '' }}>
|
|
|
|
{({ isSubmitting, isValid }) => (
|
2022-11-25 20:25:03 +00:00
|
|
|
<Fragment>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay size={'large'} visible={isSubmitting} />
|
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<Field id={'current_email'} type={'email'} name={'email'} label={'Email'} />
|
|
|
|
<div css={tw`mt-6`}>
|
2019-06-25 04:43:46 +00:00
|
|
|
<Field
|
2022-06-26 19:13:52 +00:00
|
|
|
id={'confirm_password'}
|
|
|
|
type={'password'}
|
|
|
|
name={'password'}
|
|
|
|
label={'Confirm Password'}
|
2019-06-25 04:43:46 +00:00
|
|
|
/>
|
2022-06-26 19:13:52 +00:00
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6`}>
|
2022-07-03 18:27:37 +00:00
|
|
|
<Button disabled={isSubmitting || !isValid}>Update Email</Button>
|
2022-06-26 19:13:52 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
2022-11-25 20:25:03 +00:00
|
|
|
</Fragment>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2019-06-25 04:43:46 +00:00
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|