misc_pterodactyl-panel/resources/scripts/components/auth/ForgotPasswordContainer.tsx

81 lines
3.1 KiB
TypeScript
Raw Normal View History

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';
import { httpErrorToHuman } from '@/api/http';
2019-06-22 20:53:41 +00:00
import LoginFormContainer from '@/components/auth/LoginFormContainer';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import Field from '@/components/elements/Field';
import { Formik, FormikHelpers } from 'formik';
import { object, string } from 'yup';
2020-07-04 21:21:28 +00:00
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
2019-06-12 05:02:18 +00:00
interface Values {
email: string;
}
2019-06-12 05:02:18 +00:00
export default () => {
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2019-06-12 05:02:18 +00:00
const handleSubmission = ({ email }: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
setSubmitting(true);
clearFlashes();
requestPasswordResetEmail(email)
.then(response => {
resetForm();
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 05:02:18 +00:00
return (
<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'}
2020-07-04 21:21:28 +00:00
css={tw`w-full flex`}
>
<Field
2020-07-04 21:21:28 +00:00
light
label={'Email'}
description={'Enter your account email address to receive instructions on resetting your password.'}
name={'email'}
type={'email'}
/>
2020-07-04 21:21:28 +00:00
<div css={tw`mt-6`}>
<Button
type={'submit'}
2020-07-04 21:21:28 +00:00
size={'xlarge'}
disabled={isSubmitting}
2020-07-04 21:21:28 +00:00
isLoading={isSubmitting}
>
2020-07-04 21:21:28 +00:00
Send Email
</Button>
</div>
2020-07-04 21:21:28 +00:00
<div css={tw`mt-6 text-center`}>
<Link
type={'button'}
to={'/auth/login'}
2020-07-04 21:21:28 +00:00
css={tw`text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700`}
>
Return to Login
</Link>
</div>
</LoginFormContainer>
)}
</Formik>
);
};