2019-06-12 05:02:18 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import requestPasswordResetEmail from '@/api/auth/requestPasswordResetEmail';
|
2019-06-12 06:12:03 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-06-22 20:53:41 +00:00
|
|
|
import LoginFormContainer from '@/components/auth/LoginFormContainer';
|
2019-06-22 23:45:51 +00:00
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
2019-07-10 04:25:57 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
2020-03-28 22:42:53 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { Formik, FormikHelpers } from 'formik';
|
|
|
|
import { object, string } from 'yup';
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
interface Values {
|
|
|
|
email: string;
|
|
|
|
}
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
export default () => {
|
2019-07-10 04:25:57 +00:00
|
|
|
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
const handleSubmission = ({ email }: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
|
2019-06-22 23:45:51 +00:00
|
|
|
setSubmitting(true);
|
|
|
|
clearFlashes();
|
|
|
|
requestPasswordResetEmail(email)
|
|
|
|
.then(response => {
|
2020-03-28 22:42:53 +00:00
|
|
|
resetForm();
|
2019-06-22 23:45:51 +00:00
|
|
|
addFlash({ type: 'success', title: 'Success', message: response });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
|
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
2019-06-12 06:12:03 +00:00
|
|
|
};
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2019-06-22 23:45:51 +00:00
|
|
|
return (
|
2020-03-28 22:42:53 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={handleSubmission}
|
|
|
|
initialValues={{ email: '' }}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
email: string().email('A valid email address must be provided to continue.')
|
|
|
|
.required('A valid email address must be provided to continue.'),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<LoginFormContainer
|
|
|
|
title={'Request Password Reset'}
|
|
|
|
className={'w-full flex'}
|
|
|
|
>
|
|
|
|
<Field
|
|
|
|
light={true}
|
|
|
|
label={'Email'}
|
|
|
|
description={'Enter your account email address to receive instructions on resetting your password.'}
|
|
|
|
name={'email'}
|
|
|
|
type={'email'}
|
|
|
|
/>
|
|
|
|
<div className={'mt-6'}>
|
|
|
|
<button
|
|
|
|
type={'submit'}
|
|
|
|
className={'btn btn-primary btn-jumbo flex justify-center'}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting ?
|
|
|
|
<div className={'spinner-circle spinner-sm spinner-white'}></div>
|
|
|
|
:
|
|
|
|
'Send Email'
|
|
|
|
}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className={'mt-6 text-center'}>
|
|
|
|
<Link
|
|
|
|
type={'button'}
|
|
|
|
to={'/auth/login'}
|
|
|
|
className={'text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700'}
|
|
|
|
>
|
|
|
|
Return to Login
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</LoginFormContainer>
|
|
|
|
)}
|
|
|
|
</Formik>
|
2019-06-22 23:45:51 +00:00
|
|
|
);
|
2019-06-12 06:12:03 +00:00
|
|
|
};
|