1- " use client" ;
1+ ' use client'
22
3- import { useCurrentUser , usePartialUpdateUser } from " @/lib/hooks/useAuth" ;
4- import { fieldApiError } from " @/lib/forms" ;
5- import { profileFormSchema } from " @/lib/validation" ;
6- import { ApiError } from " @/lib/api-client" ;
7- import { FormHeader } from " @frontend/ui/forms/form-header" ;
8- import { SubmitField } from " @frontend/ui/forms/submit-field" ;
9- import { TextField } from " @frontend/ui/forms/text-field" ;
10- import { SuccessMessage } from " @frontend/ui/messages/success-message" ;
11- import { zodResolver } from " @hookform/resolvers/zod" ;
12- import React , { useState } from " react" ;
13- import { useForm } from " react-hook-form" ;
14- import type { z } from " zod" ;
3+ import { ApiError } from ' @/lib/api-client'
4+ import { fieldApiError } from ' @/lib/forms'
5+ import { useCurrentUser , usePartialUpdateUser } from ' @/lib/hooks/useAuth'
6+ import { profileFormSchema } from ' @/lib/validation'
7+ import { FormHeader } from ' @frontend/ui/forms/form-header'
8+ import { SubmitField } from ' @frontend/ui/forms/submit-field'
9+ import { TextField } from ' @frontend/ui/forms/text-field'
10+ import { SuccessMessage } from ' @frontend/ui/messages/success-message'
11+ import { zodResolver } from ' @hookform/resolvers/zod'
12+ import React , { useState } from ' react'
13+ import { useForm } from ' react-hook-form'
14+ import type { z } from ' zod'
1515
16- export type ProfileFormSchema = z . infer < typeof profileFormSchema > ;
16+ export type ProfileFormSchema = z . infer < typeof profileFormSchema >
1717
1818export function ProfileForm ( ) {
19- const [ success , setSuccess ] = useState < boolean > ( false ) ;
20- const { data : currentUser , isLoading : userLoading } = useCurrentUser ( ) ;
21- const updateUserMutation = usePartialUpdateUser ( ) ;
19+ const [ success , setSuccess ] = useState < boolean > ( false )
20+ const { data : currentUser , isLoading : userLoading } = useCurrentUser ( )
21+ const updateUserMutation = usePartialUpdateUser ( )
2222
2323 const { formState, handleSubmit, register, setError, reset } =
2424 useForm < ProfileFormSchema > ( {
2525 resolver : zodResolver ( profileFormSchema ) ,
2626 defaultValues : {
27- firstName : currentUser ?. first_name || "" ,
28- lastName : currentUser ?. last_name || "" ,
29- } ,
30- } ) ;
27+ firstName : currentUser ?. first_name || '' ,
28+ lastName : currentUser ?. last_name || ''
29+ }
30+ } )
3131
3232 // 当用户数据加载完成后,重置表单
3333 React . useEffect ( ( ) => {
3434 if ( currentUser ) {
3535 reset ( {
36- firstName : currentUser . first_name || "" ,
37- lastName : currentUser . last_name || "" ,
38- } ) ;
36+ firstName : currentUser . first_name || '' ,
37+ lastName : currentUser . last_name || ''
38+ } )
3939 }
40- } , [ currentUser , reset ] ) ;
40+ } , [ currentUser , reset ] )
4141
4242 if ( userLoading ) {
43- return < div > Loading...</ div > ;
43+ return < div > Loading...</ div >
4444 }
4545
4646 return (
4747 < >
4848 < FormHeader
49- title = ' Update you profile information'
50- description = ' Change your account data'
49+ title = " Update you profile information"
50+ description = " Change your account data"
5151 />
5252
5353 < form
5454 onSubmit = { handleSubmit ( async ( data ) => {
5555 try {
5656 await updateUserMutation . mutateAsync ( {
5757 first_name : data . firstName ,
58- last_name : data . lastName ,
59- } ) ;
58+ last_name : data . lastName
59+ } )
6060
61- setSuccess ( true ) ;
61+ setSuccess ( true )
6262 } catch ( error : any ) {
63- setSuccess ( false ) ;
63+ setSuccess ( false )
6464
6565 if ( error instanceof ApiError && error . data ) {
66- const errorData = error . data ;
67- fieldApiError ( " first_name" , " firstName" , errorData , setError ) ;
68- fieldApiError ( " last_name" , " lastName" , errorData , setError ) ;
66+ const errorData = error . data
67+ fieldApiError ( ' first_name' , ' firstName' , errorData , setError )
68+ fieldApiError ( ' last_name' , ' lastName' , errorData , setError )
6969 }
7070 }
7171 } ) }
@@ -75,23 +75,23 @@ export function ProfileForm() {
7575 ) }
7676
7777 < TextField
78- type = ' text'
79- register = { register ( " firstName" ) }
80- label = ' First name'
78+ type = " text"
79+ register = { register ( ' firstName' ) }
80+ label = " First name"
8181 formState = { formState }
8282 />
8383
8484 < TextField
85- type = ' text'
86- register = { register ( " lastName" ) }
87- label = ' Last name'
85+ type = " text"
86+ register = { register ( ' lastName' ) }
87+ label = " Last name"
8888 formState = { formState }
8989 />
9090
9191 < SubmitField disabled = { updateUserMutation . isPending } >
92- { updateUserMutation . isPending ? " Updating..." : " Update profile" }
92+ { updateUserMutation . isPending ? ' Updating...' : ' Update profile' }
9393 </ SubmitField >
9494 </ form >
9595 </ >
96- ) ;
96+ )
9797}
0 commit comments