|
1 | | -'use client' |
| 1 | +"use client"; |
2 | 2 |
|
3 | | -import type { changePasswordAction } from '@/actions/change-password-action' |
4 | | -import { fieldApiError } from '@/lib/forms' |
5 | | -import { changePasswordFormSchema } from '@/lib/validation' |
6 | | -import { FormHeader } from '@frontend/ui/forms/form-header' |
7 | | -import { SubmitField } from '@frontend/ui/forms/submit-field' |
8 | | -import { TextField } from '@frontend/ui/forms/text-field' |
9 | | -import { SuccessMessage } from '@frontend/ui/messages/success-message' |
10 | | -import { zodResolver } from '@hookform/resolvers/zod' |
11 | | -import { useState } from 'react' |
12 | | -import { useForm } from 'react-hook-form' |
13 | | -import type { z } from 'zod' |
| 3 | +import type { changePasswordAction } from "@/actions/change-password-action"; |
| 4 | +import { changePasswordFormSchema } from "@/lib/validation"; |
| 5 | +import { FormHeader } from "@frontend/ui/forms/form-header"; |
| 6 | +import { SubmitField } from "@frontend/ui/forms/submit-field"; |
| 7 | +import { TextField } from "@frontend/ui/forms/text-field"; |
| 8 | +import { ErrorMessage } from "@frontend/ui/messages/error-message"; |
| 9 | +import { SuccessMessage } from "@frontend/ui/messages/success-message"; |
| 10 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 11 | +import { useState } from "react"; |
| 12 | +import { useForm } from "react-hook-form"; |
| 13 | +import type { z } from "zod"; |
14 | 14 |
|
15 | | -export type ChangePasswordFormSchema = z.infer<typeof changePasswordFormSchema> |
| 15 | +export type ChangePasswordFormSchema = z.infer<typeof changePasswordFormSchema>; |
16 | 16 |
|
17 | 17 | export function ChangePaswordForm({ |
18 | | - onSubmitHandler |
| 18 | + onSubmitHandler, |
19 | 19 | }: { |
20 | | - onSubmitHandler: typeof changePasswordAction |
| 20 | + onSubmitHandler: typeof changePasswordAction; |
21 | 21 | }) { |
22 | | - const [success, setSuccess] = useState<boolean>(false) |
| 22 | + const [message, setMessage] = useState<{ |
| 23 | + type: "success" | "error"; |
| 24 | + content: string | string[]; |
| 25 | + } | null>(null); |
23 | 26 |
|
24 | | - const { formState, handleSubmit, register, reset, setError } = |
| 27 | + const { formState, handleSubmit, register, reset } = |
25 | 28 | useForm<ChangePasswordFormSchema>({ |
26 | | - resolver: zodResolver(changePasswordFormSchema) |
27 | | - }) |
| 29 | + resolver: zodResolver(changePasswordFormSchema), |
| 30 | + shouldUseNativeValidation: false, |
| 31 | + defaultValues: { |
| 32 | + password: "", |
| 33 | + passwordNew: "", |
| 34 | + passwordRetype: "", |
| 35 | + }, |
| 36 | + }); |
28 | 37 |
|
29 | 38 | return ( |
30 | 39 | <> |
31 | 40 | <FormHeader |
32 | | - title="Set new account password" |
33 | | - description="Change sign in access password" |
| 41 | + title='Set new account password' |
| 42 | + description='Change sign in access password' |
34 | 43 | /> |
35 | 44 |
|
36 | | - {success && ( |
37 | | - <SuccessMessage>Password has been successfully changed</SuccessMessage> |
| 45 | + {message?.type === "success" && ( |
| 46 | + <SuccessMessage>{message.content}</SuccessMessage> |
| 47 | + )} |
| 48 | + |
| 49 | + {message?.type === "error" && ( |
| 50 | + <ErrorMessage> |
| 51 | + {Array.isArray(message.content) ? ( |
| 52 | + <ul className='list-disc list-inside'> |
| 53 | + {message.content.map((msg, index) => ( |
| 54 | + <li key={index}>{msg}</li> |
| 55 | + ))} |
| 56 | + </ul> |
| 57 | + ) : ( |
| 58 | + message.content |
| 59 | + )} |
| 60 | + </ErrorMessage> |
38 | 61 | )} |
39 | 62 |
|
40 | 63 | <form |
41 | | - method="post" |
| 64 | + method='post' |
| 65 | + noValidate |
| 66 | + autoComplete='off' |
42 | 67 | onSubmit={handleSubmit(async (data) => { |
43 | | - const res = await onSubmitHandler(data) |
| 68 | + setMessage(null); |
| 69 | + |
| 70 | + const res = await onSubmitHandler(data); |
44 | 71 |
|
45 | | - if (res !== true && typeof res !== 'boolean') { |
46 | | - setSuccess(false) |
47 | | - fieldApiError('password', 'password', res, setError) |
48 | | - fieldApiError('password_new', 'passwordNew', res, setError) |
49 | | - fieldApiError('password_retype', 'passwordRetype', res, setError) |
50 | | - } else { |
51 | | - reset() |
52 | | - setSuccess(true) |
| 72 | + if (res === true) { |
| 73 | + reset(); |
| 74 | + setMessage({ |
| 75 | + type: "success", |
| 76 | + content: "Password has been successfully changed", |
| 77 | + }); |
| 78 | + } else if (res && typeof res === "object") { |
| 79 | + // 收集所有错误消息 |
| 80 | + const errors: string[] = []; |
| 81 | + for (const [field, messages] of Object.entries(res)) { |
| 82 | + if (Array.isArray(messages)) { |
| 83 | + errors.push(...messages); |
| 84 | + } else { |
| 85 | + errors.push(String(messages)); |
| 86 | + } |
| 87 | + } |
| 88 | + setMessage({ |
| 89 | + type: "error", |
| 90 | + content: errors, |
| 91 | + }); |
53 | 92 | } |
54 | 93 | })} |
55 | 94 | > |
56 | 95 | <TextField |
57 | | - type="text" |
58 | | - register={register('password')} |
59 | | - label="Current password" |
| 96 | + type='password' |
| 97 | + register={register("password")} |
| 98 | + label='Current password' |
60 | 99 | formState={formState} |
| 100 | + autoComplete='current-password' |
61 | 101 | /> |
62 | 102 |
|
63 | 103 | <TextField |
64 | | - type="text" |
65 | | - register={register('passwordNew')} |
66 | | - label="New password" |
| 104 | + type='password' |
| 105 | + register={register("passwordNew")} |
| 106 | + label='New password' |
67 | 107 | formState={formState} |
| 108 | + autoComplete='new-password' |
68 | 109 | /> |
69 | 110 |
|
70 | 111 | <TextField |
71 | | - type="text" |
72 | | - register={register('passwordRetype')} |
73 | | - label="Retype password" |
| 112 | + type='password' |
| 113 | + register={register("passwordRetype")} |
| 114 | + label='Retype password' |
74 | 115 | formState={formState} |
| 116 | + autoComplete='new-password' |
75 | 117 | /> |
76 | 118 |
|
77 | | - <SubmitField isLoading={formState.isLoading}> |
| 119 | + <SubmitField isLoading={formState.isSubmitting}> |
78 | 120 | Change password |
79 | 121 | </SubmitField> |
80 | 122 | </form> |
81 | 123 | </> |
82 | | - ) |
| 124 | + ); |
83 | 125 | } |
0 commit comments