Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/develop/resources/scripts/components/dashboard/forms/UpdateEmailAddressForm.tsx
7454 views
1
import { Fragment } from 'react';
2
import { Actions, State, useStoreActions, useStoreState } from 'easy-peasy';
3
import { Form, Formik, FormikHelpers } from 'formik';
4
import * as Yup from 'yup';
5
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
6
import Field from '@/components/elements/Field';
7
import { httpErrorToHuman } from '@/api/http';
8
import { ApplicationStore } from '@/state';
9
import tw from 'twin.macro';
10
import { Button } from '@/components/elements/button/index';
11
12
interface Values {
13
email: string;
14
password: string;
15
}
16
17
const schema = Yup.object().shape({
18
email: Yup.string().email().required(),
19
password: Yup.string().required('You must provide your current account password.'),
20
});
21
22
export default () => {
23
const user = useStoreState((state: State<ApplicationStore>) => state.user.data);
24
const updateEmail = useStoreActions((state: Actions<ApplicationStore>) => state.user.updateUserEmail);
25
26
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
27
28
const submit = (values: Values, { resetForm, setSubmitting }: FormikHelpers<Values>) => {
29
clearFlashes('account:email');
30
31
updateEmail({ ...values })
32
.then(() =>
33
addFlash({
34
type: 'success',
35
key: 'account:email',
36
message: 'Your primary email has been updated.',
37
}),
38
)
39
.catch(error =>
40
addFlash({
41
type: 'error',
42
key: 'account:email',
43
title: 'Error',
44
message: httpErrorToHuman(error),
45
}),
46
)
47
.then(() => {
48
resetForm();
49
setSubmitting(false);
50
});
51
};
52
53
return (
54
<Formik onSubmit={submit} validationSchema={schema} initialValues={{ email: user!.email, password: '' }}>
55
{({ isSubmitting, isValid }) => (
56
<Fragment>
57
<SpinnerOverlay size={'large'} visible={isSubmitting} />
58
<Form css={tw`m-0`}>
59
<Field id={'current_email'} type={'email'} name={'email'} label={'Email'} />
60
<div css={tw`mt-6`}>
61
<Field
62
id={'confirm_password'}
63
type={'password'}
64
name={'password'}
65
label={'Confirm Password'}
66
/>
67
</div>
68
<div css={tw`mt-6`}>
69
<Button disabled={isSubmitting || !isValid}>Update Email</Button>
70
</div>
71
</Form>
72
</Fragment>
73
)}
74
</Formik>
75
);
76
};
77
78