1
- import { useContext } from "react" ;
1
+ import { useContext , useState } from "react" ;
2
2
import { SessionContext } from "../Session" ;
3
3
import { Header , Input , Spacer } from "../components" ;
4
4
5
+ export type ProfileData = {
6
+ name : string ;
7
+ email : string ;
8
+ } ;
9
+ export const DEFAULT_PROFILE_DATA : ProfileData = {
10
+ name : "" ,
11
+ email : "" ,
12
+ } ;
13
+
14
+ export type PasswordData = {
15
+ current_password : string ;
16
+ new_password1 : string ;
17
+ new_password2 : string ;
18
+ } ;
19
+
20
+ export const DEFAULT_PASSWORD_DATA : PasswordData = {
21
+ current_password : "" ,
22
+ new_password1 : "" ,
23
+ new_password2 : "" ,
24
+ } ;
25
+
5
26
export function Account ( ) {
6
27
const { session } = useContext ( SessionContext ) ;
7
- const setValue = ( value : string ) => {
8
- console . info ( value ) ;
28
+ const [ profile , setProfile ] = useState < ProfileData > ( DEFAULT_PROFILE_DATA ) ;
29
+ const [ password , setPassword ] = useState < PasswordData > ( DEFAULT_PASSWORD_DATA ) ;
30
+
31
+ const storeProfile = ( key : keyof ProfileData , value : string ) => {
32
+ setProfile ( { ...profile , [ key ] : value } ) ;
33
+ } ;
34
+ const saveProfile = ( ) => {
35
+ console . log ( profile ) ;
36
+ } ;
37
+
38
+ const storePassword = ( key : keyof PasswordData , value : string ) => {
39
+ setPassword ( { ...password , [ key ] : value } ) ;
40
+ } ;
41
+ const savePassword = ( ) => {
42
+ console . log ( password ) ;
9
43
} ;
10
- const saveProfile = ( ) => { } ;
11
- const updatePassword = ( ) => { } ;
12
44
13
45
return (
14
46
< >
@@ -17,14 +49,14 @@ export function Account() {
17
49
< Input
18
50
name = "name"
19
51
label = "Name"
20
- value = { session . user ?. name ?? "" }
21
- onChange = { ( value ) => setValue ( value ) }
52
+ value = { profile . name }
53
+ onChange = { ( value ) => storeProfile ( "name" , value ) }
22
54
/>
23
55
< Input
24
56
name = "email"
25
57
label = "Email"
26
- value = { session . user ?. email ?? "" }
27
- onChange = { ( value ) => setValue ( value ) }
58
+ value = { profile . email }
59
+ onChange = { ( value ) => storeProfile ( "email" , value ) }
28
60
/>
29
61
< button onClick = { saveProfile } > Save</ button >
30
62
@@ -34,25 +66,25 @@ export function Account() {
34
66
name = "current_password"
35
67
type = "password"
36
68
label = "Current Password"
37
- value = { "" }
38
- onChange = { ( value ) => setValue ( value ) }
69
+ value = { password . current_password ?? "" }
70
+ onChange = { ( value ) => storePassword ( "current_password" , value ) }
39
71
/>
40
72
< Input
41
73
name = "new_password1"
42
74
type = "password"
43
75
label = "New Password"
44
- value = { "" }
45
- onChange = { ( value ) => setValue ( value ) }
76
+ value = { password . new_password1 ?? "" }
77
+ onChange = { ( value ) => storePassword ( "new_password1" , value ) }
46
78
/>
47
79
< Input
48
80
name = "new_password2"
49
81
type = "password"
50
82
label = "Repeat New Password"
51
- value = { "" }
52
- onChange = { ( value ) => setValue ( value ) }
83
+ value = { password . new_password2 ?? "" }
84
+ onChange = { ( value ) => storePassword ( "new_password2" , value ) }
53
85
/>
54
86
55
- < button onClick = { updatePassword } > Update</ button >
87
+ < button onClick = { savePassword } > Update</ button >
56
88
</ >
57
89
) ;
58
90
}
0 commit comments