|
1 | 1 | "use client";
|
2 |
| -import { SubmitButton } from "@/app/_components/submit-button"; |
| 2 | + |
3 | 3 | import { Label, TextInput } from "flowbite-react";
|
4 |
| -import { Form, ZodFieldError } from "react-form-action/client"; |
5 |
| -import { resetPassword } from "@/app/actions/auth"; |
| 4 | +import { |
| 5 | + Form, |
| 6 | + useActionContext, |
| 7 | + createComponents, |
| 8 | +} from "react-form-action/client"; |
6 | 9 | import { Alert } from "flowbite-react";
|
7 | 10 | import Link from "next/link";
|
8 | 11 | import { useTranslation } from "react-i18next";
|
9 | 12 | import { Trans } from "react-i18next/TransWithoutContext";
|
| 13 | +import { SubmitButton } from "@/app/_components/submit-button"; |
10 | 14 | import { type ResetTokenParam, resetTokenFieldName } from "@/edgedb/shared";
|
11 | 15 | import { Stack, FormItem, FormLabel } from "@/app/_components";
|
| 16 | +import { resetPassword } from "@/app/actions/auth"; |
| 17 | + |
| 18 | +const { FieldError } = createComponents(resetPassword); |
12 | 19 |
|
13 | 20 | export function ResetPasswordForm({ reset_token }: ResetTokenParam) {
|
14 | 21 | const { t } = useTranslation("auth");
|
15 | 22 |
|
| 23 | + const { |
| 24 | + isPending, |
| 25 | + isFailure, |
| 26 | + isSuccess, |
| 27 | + isInvalid, |
| 28 | + error, |
| 29 | + validationError, |
| 30 | + data, |
| 31 | + } = useActionContext(resetPassword); |
| 32 | + |
16 | 33 | return (
|
17 |
| - <Form action={resetPassword} initialData=""> |
18 |
| - {({ |
19 |
| - error, |
20 |
| - data, |
21 |
| - validationError, |
22 |
| - isInvalid, |
23 |
| - isFailure, |
24 |
| - isSuccess, |
25 |
| - isPending, |
26 |
| - }) => ( |
27 |
| - <Stack> |
28 |
| - {isSuccess && ( |
29 |
| - <div> |
30 |
| - <Alert color="success">{data}</Alert> |
31 |
| - </div> |
32 |
| - )} |
33 |
| - {isFailure && ( |
34 |
| - <div> |
35 |
| - <Alert color="failure">{error.message}</Alert> |
36 |
| - </div> |
37 |
| - )} |
38 |
| - <FormItem> |
39 |
| - <FormLabel> |
40 |
| - <Label |
41 |
| - htmlFor="password" |
42 |
| - color={ |
43 |
| - isInvalid && validationError.password |
44 |
| - ? "failure" |
45 |
| - : isSuccess |
46 |
| - ? "success" |
47 |
| - : undefined |
48 |
| - } |
49 |
| - > |
50 |
| - {t("resetPassword.newPassword")} |
51 |
| - </Label> |
52 |
| - </FormLabel> |
53 |
| - <TextInput |
54 |
| - id="password" |
55 |
| - name="password" |
56 |
| - type="password" |
57 |
| - disabled={isPending} |
| 34 | + <Form> |
| 35 | + <Stack> |
| 36 | + {isSuccess && ( |
| 37 | + <div> |
| 38 | + <Alert color="success">{data}</Alert> |
| 39 | + </div> |
| 40 | + )} |
| 41 | + {isFailure && ( |
| 42 | + <div> |
| 43 | + <Alert color="failure">{error.message}</Alert> |
| 44 | + </div> |
| 45 | + )} |
| 46 | + <FormItem> |
| 47 | + <FormLabel> |
| 48 | + <Label |
| 49 | + htmlFor="password" |
58 | 50 | color={
|
59 | 51 | isInvalid && validationError.password
|
60 | 52 | ? "failure"
|
61 | 53 | : isSuccess
|
62 | 54 | ? "success"
|
63 | 55 | : undefined
|
64 | 56 | }
|
65 |
| - helperText={ |
66 |
| - isInvalid && ( |
67 |
| - <ZodFieldError errors={validationError} name="password" /> |
68 |
| - ) |
69 |
| - } |
70 |
| - /> |
71 |
| - </FormItem> |
72 |
| - <input name={resetTokenFieldName} defaultValue={reset_token} hidden /> |
73 |
| - <SubmitButton /> |
74 |
| - <Label> |
75 |
| - <Trans i18nKey="resetPassword.linkToReset" t={t}> |
76 |
| - Link expired? |
77 |
| - <Link |
78 |
| - href="/reset-password-email" |
79 |
| - className="text-cyan-600 hover:underline dark:text-cyan-500" |
80 |
| - > |
81 |
| - Get another. |
82 |
| - </Link> |
83 |
| - </Trans> |
84 |
| - </Label> |
85 |
| - </Stack> |
86 |
| - )} |
| 57 | + > |
| 58 | + {t("resetPassword.newPassword")} |
| 59 | + </Label> |
| 60 | + </FormLabel> |
| 61 | + <TextInput |
| 62 | + id="password" |
| 63 | + name="password" |
| 64 | + type="password" |
| 65 | + disabled={isPending} |
| 66 | + color={ |
| 67 | + isInvalid && validationError.password |
| 68 | + ? "failure" |
| 69 | + : isSuccess |
| 70 | + ? "success" |
| 71 | + : undefined |
| 72 | + } |
| 73 | + helperText={<FieldError name="password" />} |
| 74 | + /> |
| 75 | + </FormItem> |
| 76 | + <input name={resetTokenFieldName} defaultValue={reset_token} hidden /> |
| 77 | + <SubmitButton /> |
| 78 | + <Label> |
| 79 | + <Trans i18nKey="resetPassword.linkToReset" t={t}> |
| 80 | + Link expired? |
| 81 | + <Link |
| 82 | + href="/reset-password-email" |
| 83 | + className="text-cyan-600 hover:underline dark:text-cyan-500" |
| 84 | + > |
| 85 | + Get another. |
| 86 | + </Link> |
| 87 | + </Trans> |
| 88 | + </Label> |
| 89 | + </Stack> |
87 | 90 | </Form>
|
88 | 91 | );
|
89 | 92 | }
|
0 commit comments