|
2 | 2 |
|
3 | 3 | import Link from "next/link";
|
4 | 4 | import { Label, TextInput } from "flowbite-react";
|
5 |
| -import { Form, ZodFieldError } from "react-form-action/client"; |
| 5 | +import { |
| 6 | + Form, |
| 7 | + createComponents, |
| 8 | + useActionContext, |
| 9 | +} from "react-form-action/client"; |
6 | 10 | import { Alert } from "flowbite-react";
|
7 | 11 | import { useTranslation } from "react-i18next";
|
8 | 12 | import { Trans } from "react-i18next/TransWithoutContext";
|
9 | 13 | import { SubmitButton } from "@/app/_components/submit-button";
|
10 |
| -import { signIn } from "@/app/actions/auth"; |
11 | 14 | import { FormLabel, FormItem, Stack } from "@/app/_components";
|
| 15 | +import { signIn } from "@/app/actions/auth"; |
| 16 | + |
| 17 | +const { FieldError } = createComponents(signIn); |
12 | 18 |
|
13 | 19 | export function SignInForm() {
|
14 | 20 | const { t } = useTranslation("auth");
|
15 | 21 |
|
| 22 | + const { isPending, isFailure, isSuccess, isInvalid, error, validationError } = |
| 23 | + useActionContext(signIn); |
| 24 | + |
16 | 25 | return (
|
17 |
| - <Form action={signIn} initialData=""> |
18 |
| - {({ |
19 |
| - error, |
20 |
| - validationError, |
21 |
| - isInvalid, |
22 |
| - isFailure, |
23 |
| - isSuccess, |
24 |
| - isPending, |
25 |
| - }) => { |
26 |
| - return ( |
27 |
| - <Stack> |
28 |
| - {isFailure && ( |
29 |
| - <div> |
30 |
| - <Alert color="failure">{error.message}</Alert> |
31 |
| - </div> |
32 |
| - )} |
33 |
| - <FormItem> |
34 |
| - <FormLabel> |
35 |
| - <Label htmlFor="email">{t("signIn.email")}</Label> |
36 |
| - </FormLabel> |
37 |
| - <TextInput |
38 |
| - id="email" |
39 |
| - name="email" |
40 |
| - disabled={isPending} |
41 |
| - color={ |
42 |
| - isInvalid && validationError.email |
43 |
| - ? "failure" |
44 |
| - : isSuccess |
45 |
| - ? "success" |
46 |
| - : undefined |
47 |
| - } |
48 |
| - type="text" |
49 |
| - |
50 |
| - helperText={ |
51 |
| - isInvalid && ( |
52 |
| - <ZodFieldError errors={validationError} name="email" /> |
53 |
| - ) |
54 |
| - } |
55 |
| - /> |
56 |
| - </FormItem> |
57 |
| - <FormItem> |
58 |
| - <FormLabel> |
59 |
| - <Label htmlFor="password" className="flex justify-between"> |
60 |
| - <span>{t("signIn.password")}</span> |
61 |
| - <Link |
62 |
| - tabIndex={-1} |
63 |
| - href="/reset-password-email" |
64 |
| - className="text-cyan-600 hover:underline dark:text-cyan-500" |
65 |
| - > |
66 |
| - {t("signIn.forgotPassword")} |
67 |
| - </Link> |
68 |
| - </Label> |
69 |
| - </FormLabel> |
70 |
| - <TextInput |
71 |
| - id="password" |
72 |
| - name="password" |
73 |
| - disabled={isPending} |
74 |
| - color={ |
75 |
| - isInvalid && validationError.password |
76 |
| - ? "failure" |
77 |
| - : isSuccess |
78 |
| - ? "success" |
79 |
| - : undefined |
80 |
| - } |
81 |
| - type="password" |
82 |
| - placeholder="Your password" |
83 |
| - helperText={ |
84 |
| - isInvalid && ( |
85 |
| - <ZodFieldError errors={validationError} name="password" /> |
86 |
| - ) |
87 |
| - } |
88 |
| - /> |
89 |
| - </FormItem> |
90 |
| - <SubmitButton /> |
91 |
| - <Label> |
92 |
| - <Trans i18nKey="signIn.linkToSignUp" t={t}> |
93 |
| - Don't have an account? |
94 |
| - <Link |
95 |
| - href="/signup" |
96 |
| - className="text-cyan-600 hover:underline dark:text-cyan-500" |
97 |
| - > |
98 |
| - Sign up |
99 |
| - </Link> |
100 |
| - </Trans> |
| 26 | + <Form> |
| 27 | + <Stack> |
| 28 | + {isFailure && ( |
| 29 | + <div> |
| 30 | + <Alert color="failure">{error.message}</Alert> |
| 31 | + </div> |
| 32 | + )} |
| 33 | + <FormItem> |
| 34 | + <FormLabel> |
| 35 | + <Label htmlFor="email">{t("signIn.email")}</Label> |
| 36 | + </FormLabel> |
| 37 | + <TextInput |
| 38 | + id="email" |
| 39 | + name="email" |
| 40 | + disabled={isPending} |
| 41 | + color={ |
| 42 | + isInvalid && validationError.email |
| 43 | + ? "failure" |
| 44 | + : isSuccess |
| 45 | + ? "success" |
| 46 | + : undefined |
| 47 | + } |
| 48 | + type="text" |
| 49 | + |
| 50 | + helperText={<FieldError name="email" />} |
| 51 | + /> |
| 52 | + </FormItem> |
| 53 | + <FormItem> |
| 54 | + <FormLabel> |
| 55 | + <Label htmlFor="password" className="flex justify-between"> |
| 56 | + <span>{t("signIn.password")}</span> |
| 57 | + <Link |
| 58 | + tabIndex={-1} |
| 59 | + href="/reset-password-email" |
| 60 | + className="text-cyan-600 hover:underline dark:text-cyan-500" |
| 61 | + > |
| 62 | + {t("signIn.forgotPassword")} |
| 63 | + </Link> |
101 | 64 | </Label>
|
102 |
| - </Stack> |
103 |
| - ); |
104 |
| - }} |
| 65 | + </FormLabel> |
| 66 | + <TextInput |
| 67 | + id="password" |
| 68 | + name="password" |
| 69 | + disabled={isPending} |
| 70 | + color={ |
| 71 | + isInvalid && validationError.password |
| 72 | + ? "failure" |
| 73 | + : isSuccess |
| 74 | + ? "success" |
| 75 | + : undefined |
| 76 | + } |
| 77 | + type="password" |
| 78 | + placeholder="Your password" |
| 79 | + helperText={<FieldError name="password" />} |
| 80 | + /> |
| 81 | + </FormItem> |
| 82 | + <SubmitButton /> |
| 83 | + <Label> |
| 84 | + <Trans i18nKey="signIn.linkToSignUp" t={t}> |
| 85 | + Don't have an account? |
| 86 | + <Link |
| 87 | + href="/signup" |
| 88 | + className="text-cyan-600 hover:underline dark:text-cyan-500" |
| 89 | + > |
| 90 | + Sign up |
| 91 | + </Link> |
| 92 | + </Trans> |
| 93 | + </Label> |
| 94 | + </Stack> |
105 | 95 | </Form>
|
106 | 96 | );
|
107 | 97 | }
|
0 commit comments