11import { Button } from '@/ui/Button'
2- import { Form } from '@/ui/forms/Form'
3- import { useZodForm } from '@/lib/hooks/useZodForm'
42import { Input } from '@/ui/forms/Input'
5- import { useEffect , useState } from 'react'
6- import { profileSchema , userService } from '@/lib/api/user'
7- import { useDialog } from '@/lib/hooks/useDialog'
8- import { ErrorDialog } from '../dialogs/ErrorDialog'
9- import { getObjectKeys } from '@/utils/helpers'
103import { ChangePasswordForm } from './ChangePasswordForm'
114import { usePasswordContext } from '@/lib/context/password'
125import { UserResponse } from '@wallet/shared'
@@ -17,132 +10,38 @@ type PersonalSettingsFormProps = {
1710
1811// TODO: Can these details be updated by the user when switching to GateHub?
1912export const PersonalSettingsForm = ( { user } : PersonalSettingsFormProps ) => {
20- const [ isReadOnly , setIsReadOnly ] = useState ( true )
2113 const { isChangePassword, setIsChangePassword } = usePasswordContext ( )
22- const [ openDialog , closeDialog ] = useDialog ( )
23- const profileForm = useZodForm ( {
24- schema : profileSchema ,
25- defaultValues : user
26- } )
27-
28- useEffect ( ( ) => {
29- profileForm . setFocus ( 'firstName' )
30- } , [ isReadOnly , profileForm ] )
3114
3215 return (
3316 < >
3417 < div className = "mb-5" >
35- < h3 className = "text-2xl text-green dark:text-teal-neon" > Profile </ h3 >
18+ < h3 className = "text-2xl text-green dark:text-teal-neon" > Details </ h3 >
3619 </ div >
37- < Form
38- form = { profileForm }
39- onSubmit = { async ( data ) => {
40- const response = await userService . updateProfile ( data )
41-
42- if ( ! response ) {
43- openDialog (
44- < ErrorDialog
45- onClose = { closeDialog }
46- content = "Update profile failed. Please try again."
47- />
48- )
49- return
50- }
51-
52- if ( response . success ) {
53- setIsReadOnly ( ! isReadOnly )
54- } else {
55- const { errors, message } = response
56-
57- if ( errors ) {
58- getObjectKeys ( errors ) . map ( ( field ) =>
59- profileForm . setError ( field , {
60- message : errors [ field ]
61- } )
62- )
63- }
64- if ( message ) {
65- profileForm . setError ( 'root' , { message } )
66- }
67- }
68- } }
69- readOnly = { isReadOnly }
70- >
20+ < div className = "mb-4" >
7121 < Input
72- required
7322 label = "First name"
7423 placeholder = "First name"
75- error = { profileForm . formState . errors . firstName ?. message }
76- { ... profileForm . register ( ' firstName' ) }
24+ disabled
25+ value = { user . firstName }
7726 />
27+ </ div >
28+ < div className = "mb-4" >
7829 < Input
79- required
8030 label = "Last name"
8131 placeholder = "Last name"
82- error = { profileForm . formState . errors . lastName ?. message }
83- { ... profileForm . register ( ' lastName' ) }
32+ disabled
33+ value = { user . lastName }
8434 />
85-
86- { ! isReadOnly && (
87- < div className = "mt-2 flex justify-between" >
88- < Button
89- intent = "outline"
90- aria-label = "stop editing"
91- onClick = { ( ) => setIsReadOnly ( ! isReadOnly ) }
92- >
93- Close editing
94- </ Button >
95- < Button
96- type = "submit"
97- aria-label = "save personal settings"
98- loading = { profileForm . formState . isSubmitting }
99- >
100- Save profile
101- </ Button >
102- </ div >
103- ) }
104- </ Form >
105- { isReadOnly && (
106- < div className = "mt-4 flex justify-between" >
107- < Button
108- intent = "primary"
109- aria-label = "edit personal details"
110- onClick = { ( ) => {
111- setIsReadOnly ( ! isReadOnly )
112- setIsChangePassword ( false )
113- } }
114- >
115- Edit
116- </ Button >
117- { ! isChangePassword ? (
118- < Button
119- intent = "primary"
120- aria-label = "change password"
121- onClick = { ( ) => setIsChangePassword ( ! isChangePassword ) }
122- >
123- Change Password
124- </ Button >
125- ) : (
126- < Button
127- intent = "outline"
128- aria-label = "Cancel change password"
129- onClick = { ( ) => setIsChangePassword ( ! isChangePassword ) }
130- >
131- Cancel Change Password
132- </ Button >
133- ) }
134- </ div >
135- ) }
136- { isChangePassword && < ChangePasswordForm /> }
137- < div className = "mb-4 mt-6" >
35+ </ div >
36+ < div className = "mb-4" >
13837 < Input
13938 label = "Address"
14039 placeholder = "Address"
14140 disabled
14241 value = { user . address }
14342 />
14443 </ div >
145- < div >
44+ < div className = "mb-4" >
14645 < Input
14746 type = "email"
14847 label = "Email"
@@ -151,6 +50,26 @@ export const PersonalSettingsForm = ({ user }: PersonalSettingsFormProps) => {
15150 value = { user . email }
15251 />
15352 </ div >
53+ < div className = "mt-4 flex justify-between" >
54+ { ! isChangePassword ? (
55+ < Button
56+ intent = "primary"
57+ aria-label = "change password"
58+ onClick = { ( ) => setIsChangePassword ( ! isChangePassword ) }
59+ >
60+ Change Password
61+ </ Button >
62+ ) : (
63+ < Button
64+ intent = "outline"
65+ aria-label = "Cancel change password"
66+ onClick = { ( ) => setIsChangePassword ( ! isChangePassword ) }
67+ >
68+ Cancel Change Password
69+ </ Button >
70+ ) }
71+ </ div >
72+ { isChangePassword && < ChangePasswordForm /> }
15473 </ >
15574 )
15675}
0 commit comments