2019-06-23 07:45:09 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-06-23 07:25:38 +01:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import * as Yup from 'yup';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2019-06-23 07:45:09 +01:00
|
|
|
import updateAccountPassword from '@/api/account/updateAccountPassword';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-07-10 05:25:57 +01:00
|
|
|
import { ApplicationStore } from '@/state';
|
2020-07-03 23:37:26 +01:00
|
|
|
import tw from 'twin.macro';
|
2022-07-03 19:27:37 +01:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2019-06-23 07:25:38 +01:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
current: string;
|
|
|
|
password: string;
|
|
|
|
confirmPassword: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = Yup.object().shape({
|
|
|
|
current: Yup.string().min(1).required('You must provide your current password.'),
|
|
|
|
password: Yup.string().min(8).required(),
|
2022-06-26 20:13:52 +01:00
|
|
|
confirmPassword: Yup.string().test(
|
|
|
|
'password',
|
|
|
|
'Password confirmation does not match the password you entered.',
|
|
|
|
function (value) {
|
|
|
|
return value === this.parent.password;
|
|
|
|
}
|
|
|
|
),
|
2019-06-23 07:25:38 +01:00
|
|
|
});
|
2019-06-23 02:53:50 +01:00
|
|
|
|
|
|
|
export default () => {
|
2019-07-10 05:25:57 +01:00
|
|
|
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
|
|
|
|
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-06-23 02:53:50 +01:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-05 02:46:09 +01:00
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2019-06-23 07:45:09 +01:00
|
|
|
clearFlashes('account:password');
|
|
|
|
updateAccountPassword({ ...values })
|
|
|
|
.then(() => {
|
2022-06-26 20:30:05 +01:00
|
|
|
// @ts-expect-error this is valid
|
2019-12-28 20:07:42 +00:00
|
|
|
window.location = '/auth/login';
|
2019-06-23 07:45:09 +01:00
|
|
|
})
|
2022-06-26 20:13:52 +01:00
|
|
|
.catch((error) =>
|
|
|
|
addFlash({
|
|
|
|
key: 'account:password',
|
|
|
|
type: 'error',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
|
|
|
})
|
|
|
|
)
|
2019-06-23 07:45:09 +01:00
|
|
|
.then(() => setSubmitting(false));
|
2019-06-23 07:25:38 +01:00
|
|
|
};
|
|
|
|
|
2019-06-23 02:53:50 +01:00
|
|
|
return (
|
2019-06-23 07:25:38 +01:00
|
|
|
<React.Fragment>
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
validationSchema={schema}
|
|
|
|
initialValues={{ current: '', password: '', confirmPassword: '' }}
|
|
|
|
>
|
2022-06-26 20:13:52 +01:00
|
|
|
{({ isSubmitting, isValid }) => (
|
|
|
|
<React.Fragment>
|
|
|
|
<SpinnerOverlay size={'large'} visible={isSubmitting} />
|
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<Field
|
|
|
|
id={'current_password'}
|
|
|
|
type={'password'}
|
|
|
|
name={'current'}
|
|
|
|
label={'Current Password'}
|
|
|
|
/>
|
|
|
|
<div css={tw`mt-6`}>
|
2019-06-23 07:25:38 +01:00
|
|
|
<Field
|
2022-06-26 20:13:52 +01:00
|
|
|
id={'new_password'}
|
2019-06-23 07:25:38 +01:00
|
|
|
type={'password'}
|
2022-06-26 20:13:52 +01:00
|
|
|
name={'password'}
|
|
|
|
label={'New Password'}
|
|
|
|
description={
|
|
|
|
'Your new password should be at least 8 characters in length and unique to this website.'
|
|
|
|
}
|
2019-06-23 07:25:38 +01:00
|
|
|
/>
|
2022-06-26 20:13:52 +01:00
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Field
|
|
|
|
id={'confirm_new_password'}
|
|
|
|
type={'password'}
|
|
|
|
name={'confirmPassword'}
|
|
|
|
label={'Confirm New Password'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6`}>
|
2022-07-03 19:27:37 +01:00
|
|
|
<Button disabled={isSubmitting || !isValid}>Update Password</Button>
|
2022-06-26 20:13:52 +01:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</React.Fragment>
|
|
|
|
)}
|
2019-06-23 07:25:38 +01:00
|
|
|
</Formik>
|
|
|
|
</React.Fragment>
|
2019-06-23 02:53:50 +01:00
|
|
|
);
|
|
|
|
};
|