|
| 1 | +import { Paper, Flex, rem, Title, Button, TextInput } from '@mantine/core'; |
| 2 | +import { RoutesDef } from '@notes/utils'; |
| 3 | +import { IconLogin2, IconMailCheck } from '@tabler/icons-react'; |
| 4 | +import { useForm } from '@tanstack/react-form'; |
| 5 | +import { Link } from '@tanstack/react-router'; |
| 6 | +import { zodValidator } from '@tanstack/zod-form-adapter'; |
| 7 | +import { z } from 'zod'; |
| 8 | +import classes from './style.module.css'; |
| 9 | +import { useAuthContext } from '@notes/hooks'; |
| 10 | + |
| 11 | +export function ResetPassword() { |
| 12 | + const { resetPassword } = useAuthContext(); |
| 13 | + |
| 14 | + const { Field, Subscribe, handleSubmit, state } = useForm({ |
| 15 | + defaultValues: { email: '' }, |
| 16 | + validatorAdapter: zodValidator(), |
| 17 | + onSubmit: async () => { |
| 18 | + resetPassword(state.values.email); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + return ( |
| 23 | + <Paper shadow="sm" p="xl" w="fit-content" bg={'var(--dark-bg-color)'} maw={'600px'} m="40px auto"> |
| 24 | + <Flex direction={'column'} align={'center'}> |
| 25 | + <IconMailCheck color={'var(--primary)'} style={{ width: rem(40), height: rem(40) }} /> |
| 26 | + <br /> |
| 27 | + {state.isSubmitted ? ( |
| 28 | + <Title order={3} c={'var(--white)'} mb={'xl'}> |
| 29 | + We have sent you a verification link to provided e-mail. Please click on it to reset your password. |
| 30 | + </Title> |
| 31 | + ) : ( |
| 32 | + <form |
| 33 | + onSubmit={e => { |
| 34 | + e.preventDefault(); |
| 35 | + e.stopPropagation(); |
| 36 | + handleSubmit(); |
| 37 | + }} |
| 38 | + > |
| 39 | + <Title order={2} c={'var(--white'} mb={'xl'}> |
| 40 | + Please provide your e-mail. |
| 41 | + </Title> |
| 42 | + <Field |
| 43 | + name="email" |
| 44 | + validators={{ |
| 45 | + onSubmit: z.string().email('Invalid e-mail').trim(), |
| 46 | + onBlur: z.string().email('Invalid e-mail') |
| 47 | + }} |
| 48 | + children={({ state, handleChange, handleBlur }) => { |
| 49 | + return ( |
| 50 | + <TextInput |
| 51 | + className={classes.textInput} |
| 52 | + data-autofocus |
| 53 | + size="md" |
| 54 | + defaultValue={state.value} |
| 55 | + onChange={e => handleChange(e.target.value)} |
| 56 | + onBlur={handleBlur} |
| 57 | + withAsterisk |
| 58 | + label="E-mail" |
| 59 | + placeholder="Enter e-mail address" |
| 60 | + error={state.meta?.errors[0]} |
| 61 | + /> |
| 62 | + ); |
| 63 | + }} |
| 64 | + /> |
| 65 | + <Flex justify={'flex-end'} mt={5} align={'center'}> |
| 66 | + <Subscribe |
| 67 | + selector={state => [state.canSubmit, state.isSubmitting]} |
| 68 | + children={([canSubmit, isSubmitting]) => { |
| 69 | + return ( |
| 70 | + <Button |
| 71 | + size="medium" |
| 72 | + right={0} |
| 73 | + type="submit" |
| 74 | + variant="notes-transparent-border" |
| 75 | + loading={isSubmitting} |
| 76 | + disabled={!canSubmit} |
| 77 | + loaderProps={{ color: 'var(--white)', size: 20 }} |
| 78 | + > |
| 79 | + <IconLogin2 stroke={1.5} /> |
| 80 | + </Button> |
| 81 | + ); |
| 82 | + }} |
| 83 | + /> |
| 84 | + </Flex> |
| 85 | + </form> |
| 86 | + )} |
| 87 | + <br /> |
| 88 | + <Button component={Link} variant="notes-transparent-border" size="md" fz={'md'} to={RoutesDef.SIGNIN}> |
| 89 | + Go to Sign In{' '} |
| 90 | + </Button> |
| 91 | + </Flex> |
| 92 | + </Paper> |
| 93 | + ); |
| 94 | +} |
0 commit comments