Files
pterodactyl-panel/resources/scripts/components/dashboard/forms/UpdateEmailAddressForm.tsx
T

64 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-06-24 21:43:46 -07:00
import React from 'react';
2026-10-05 18:10:18 -04:00
import { useStore } from '@tanstack/react-form';
import { useAppForm, Form } from '@/components/form';
2019-06-24 21:43:46 -07:00
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
2026-10-05 18:10:18 -04:00
import { useCurrentUser, useUpdateAccountEmail } from '@/api/account/queries';
2019-06-24 21:43:46 -07:00
2026-10-05 18:10:18 -04:00
const isEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
2019-06-24 21:43:46 -07:00
2026-10-05 18:10:18 -04:00
function UpdateEmailAddressForm() {
const user = useCurrentUser();
const updateEmail = useUpdateAccountEmail();
2019-06-24 21:43:46 -07:00
2026-10-05 18:10:18 -04:00
const form = useAppForm({
defaultValues: { email: user.email, password: '' },
onSubmit: async ({ value, formApi }) => {
try {
await updateEmail.mutateAsync({ body: value });
formApi.setFieldValue('password', '');
} catch {
// Error toast is handled by the mutation.
} finally {
formApi.setFieldMeta('password', (meta) => ({ ...meta, errors: [] }));
}
},
});
2019-06-24 21:43:46 -07:00
2026-10-05 18:10:18 -04:00
const isSubmitting = useStore(form.store, (state) => state.isSubmitting) || updateEmail.isPending;
2019-06-24 21:43:46 -07:00
return (
2026-10-08 16:53:20 -04:00
<>
<SpinnerOverlay size='large' visible={isSubmitting} />
<Form form={form} className='m-0'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='email'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
isEmail(value) ? undefined : 'A valid email address must be provided.',
}}
>
2026-10-08 16:53:20 -04:00
{(field) => <field.TextField id='current_email' type='email' label='Email' />}
2026-10-05 18:10:18 -04:00
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='password'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
value.length >= 1 ? undefined : 'You must provide your current account password.',
}}
>
2026-10-08 16:53:20 -04:00
{(field) => <field.TextField id='confirm_password' type='password' label='Confirm Password' />}
2026-10-05 18:10:18 -04:00
</form.AppField>
</div>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<form.AppForm>
<form.SubmitButton disabled={updateEmail.isPending}>Update Email</form.SubmitButton>
</form.AppForm>
</div>
</Form>
2026-10-08 16:53:20 -04:00
</>
2019-06-24 21:43:46 -07:00
);
2026-10-05 18:10:18 -04:00
}
export default UpdateEmailAddressForm;