|
1 | 1 | import { Button, Stack, TextField, Typography } from '@mui/material'; |
2 | 2 | import { useTranslations } from 'next-intl'; |
3 | 3 | import React from 'react'; |
4 | | -import { Controller, useForm } from 'react-hook-form'; |
| 4 | +import { |
| 5 | + Controller, |
| 6 | + FormProvider, |
| 7 | + SubmitHandler, |
| 8 | + useForm, |
| 9 | +} from 'react-hook-form'; |
| 10 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 11 | +import * as yup from 'yup'; |
| 12 | +import toast from 'react-hot-toast'; |
| 13 | + |
| 14 | +type FieldNames = { |
| 15 | + email: string; |
| 16 | +}; |
5 | 17 |
|
6 | 18 | const Newsletter = () => { |
7 | 19 | const t = useTranslations(); |
8 | | - const form = useForm(); |
| 20 | + |
| 21 | + const labels: FieldNames = { |
| 22 | + email: t('pages.login.email'), |
| 23 | + }; |
| 24 | + |
| 25 | + const resolveSchema: yup.ObjectSchema<FieldNames> = yup.object({ |
| 26 | + email: yup.string().email().nullable().required().label(labels.email), |
| 27 | + }); |
| 28 | + |
| 29 | + const form = useForm({ |
| 30 | + resolver: yupResolver(resolveSchema), |
| 31 | + }); |
| 32 | + |
| 33 | + const onSubmit: SubmitHandler<FieldNames> = (payload) => { |
| 34 | + toast.success(t('messages.defaultSuccess')); |
| 35 | + form.reset(); |
| 36 | + }; |
9 | 37 |
|
10 | 38 | return ( |
11 | | - <Stack component="form" spacing={1}> |
12 | | - <Typography variant="h6">{t('footer.newsletter.title')}</Typography> |
13 | | - <Stack spacing={1} alignItems="center" direction="row"> |
14 | | - <Controller |
15 | | - name="email" |
16 | | - control={form.control} |
17 | | - render={(props) => { |
18 | | - const { |
19 | | - field: { name, value }, |
20 | | - fieldState: { error }, |
21 | | - } = props; |
22 | | - return ( |
23 | | - <TextField |
24 | | - name={name} |
25 | | - value={value} |
26 | | - placeholder={t('pages.login.email')} |
27 | | - variant="outlined" |
28 | | - size="small" |
29 | | - fullWidth |
30 | | - error={!!error?.message} |
31 | | - helperText={error?.message?.toString()} |
32 | | - /> |
33 | | - ); |
34 | | - }} |
35 | | - /> |
36 | | - <Button color="primary" variant="contained"> |
37 | | - {t('buttons.submit')} |
38 | | - </Button> |
| 39 | + <FormProvider {...form}> |
| 40 | + <Stack |
| 41 | + component="form" |
| 42 | + spacing={1} |
| 43 | + onSubmit={form.handleSubmit(onSubmit)} |
| 44 | + > |
| 45 | + <Typography variant="h6">{t('footer.newsletter.title')}</Typography> |
| 46 | + <Stack spacing={1} alignItems="center" direction="row"> |
| 47 | + <Controller |
| 48 | + name="email" |
| 49 | + control={form.control} |
| 50 | + render={(props) => { |
| 51 | + const { |
| 52 | + field: { name, value, onChange }, |
| 53 | + fieldState: { error }, |
| 54 | + } = props; |
| 55 | + return ( |
| 56 | + <TextField |
| 57 | + name={name} |
| 58 | + value={value || ''} |
| 59 | + onChange={onChange} |
| 60 | + placeholder={labels.email} |
| 61 | + variant="outlined" |
| 62 | + size="small" |
| 63 | + fullWidth |
| 64 | + error={!!error?.message} |
| 65 | + helperText={error?.message?.toString()} |
| 66 | + /> |
| 67 | + ); |
| 68 | + }} |
| 69 | + /> |
| 70 | + <Button color="primary" variant="contained" type="submit"> |
| 71 | + {t('buttons.submit')} |
| 72 | + </Button> |
| 73 | + </Stack> |
39 | 74 | </Stack> |
40 | | - </Stack> |
| 75 | + </FormProvider> |
41 | 76 | ); |
42 | 77 | }; |
43 | 78 |
|
|
0 commit comments