2019-06-22 23:45:51 +00:00
|
|
|
import React, { useState } from 'react';
|
2019-06-16 23:57:57 +00:00
|
|
|
import { RouteComponentProps } from 'react-router';
|
|
|
|
import { parse } from 'query-string';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import performPasswordReset from '@/api/auth/performPasswordReset';
|
|
|
|
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';
|
2019-12-22 22:53:27 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2020-03-28 22:42:53 +00:00
|
|
|
import { Formik, FormikHelpers } from 'formik';
|
|
|
|
import { object, ref, string } from 'yup';
|
|
|
|
import Field from '@/components/elements/Field';
|
2020-07-04 21:34:43 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2019-06-16 23:57:57 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
interface Values {
|
|
|
|
password: string;
|
|
|
|
passwordConfirmation: string;
|
|
|
|
}
|
|
|
|
|
2020-07-04 21:34:43 +00:00
|
|
|
export default ({ match, location }: RouteComponentProps<{ token: string }>) => {
|
2019-06-22 23:45:51 +00:00
|
|
|
const [ email, setEmail ] = useState('');
|
2019-06-16 23:57:57 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-06-16 23:57:57 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
const parsed = parse(location.search);
|
2019-06-22 23:45:51 +00:00
|
|
|
if (email.length === 0 && parsed.email) {
|
|
|
|
setEmail(parsed.email as string);
|
2019-06-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
const submit = ({ password, passwordConfirmation }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2019-06-22 23:45:51 +00:00
|
|
|
clearFlashes();
|
2020-03-28 22:42:53 +00:00
|
|
|
performPasswordReset(email, { token: match.params.token, password, passwordConfirmation })
|
2019-06-22 23:45:51 +00:00
|
|
|
.then(() => {
|
2020-03-28 22:42:53 +00:00
|
|
|
// @ts-ignore
|
|
|
|
window.location = '/';
|
2019-06-22 23:45:51 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-03-28 22:42:53 +00:00
|
|
|
|
|
|
|
setSubmitting(false);
|
2019-06-22 23:45:51 +00:00
|
|
|
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
|
2020-03-28 22:42:53 +00:00
|
|
|
});
|
2019-06-16 23:57:57 +00:00
|
|
|
};
|
|
|
|
|
2019-06-22 23:45:51 +00:00
|
|
|
return (
|
2020-03-28 22:42:53 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
|
|
|
password: '',
|
|
|
|
passwordConfirmation: '',
|
|
|
|
}}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
password: string().required('A new password is required.')
|
|
|
|
.min(8, 'Your new password should be at least 8 characters in length.'),
|
|
|
|
passwordConfirmation: string()
|
|
|
|
.required('Your new password does not match.')
|
|
|
|
.oneOf([ref('password'), null], 'Your new password does not match.'),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{({ isSubmitting }) => (
|
|
|
|
<LoginFormContainer
|
|
|
|
title={'Reset Password'}
|
2020-07-04 21:34:43 +00:00
|
|
|
css={tw`w-full flex`}
|
2020-03-28 22:42:53 +00:00
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<label>Email</label>
|
2020-07-04 21:34:43 +00:00
|
|
|
<Input value={email} light disabled/>
|
2020-03-28 22:42:53 +00:00
|
|
|
</div>
|
2020-07-04 21:34:43 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-03-28 22:42:53 +00:00
|
|
|
<Field
|
2020-07-04 21:34:43 +00:00
|
|
|
light
|
2020-03-28 22:42:53 +00:00
|
|
|
label={'New Password'}
|
|
|
|
name={'password'}
|
|
|
|
type={'password'}
|
|
|
|
description={'Passwords must be at least 8 characters in length.'}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-04 21:34:43 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-03-28 22:42:53 +00:00
|
|
|
<Field
|
2020-07-04 21:34:43 +00:00
|
|
|
light
|
2020-03-28 22:42:53 +00:00
|
|
|
label={'Confirm New Password'}
|
|
|
|
name={'passwordConfirmation'}
|
|
|
|
type={'password'}
|
|
|
|
/>
|
|
|
|
</div>
|
2020-07-04 21:34:43 +00:00
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Button
|
|
|
|
size={'xlarge'}
|
2020-03-28 22:42:53 +00:00
|
|
|
type={'submit'}
|
|
|
|
disabled={isSubmitting}
|
2020-07-04 21:34:43 +00:00
|
|
|
isLoading={isSubmitting}
|
2020-03-28 22:42:53 +00:00
|
|
|
>
|
2020-07-04 21:34:43 +00:00
|
|
|
Reset Password
|
|
|
|
</Button>
|
2020-03-28 22:42:53 +00:00
|
|
|
</div>
|
2020-07-04 21:34:43 +00:00
|
|
|
<div css={tw`mt-6 text-center`}>
|
2020-03-28 22:42:53 +00:00
|
|
|
<Link
|
|
|
|
to={'/auth/login'}
|
2020-07-04 21:34:43 +00:00
|
|
|
css={tw`text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600`}
|
2020-03-28 22:42:53 +00:00
|
|
|
>
|
|
|
|
Return to Login
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</LoginFormContainer>
|
|
|
|
)}
|
|
|
|
</Formik>
|
2019-06-22 23:45:51 +00:00
|
|
|
);
|
2019-06-16 23:57:57 +00:00
|
|
|
};
|