1
1
import { useContext , useState } from "react" ;
2
2
import { SessionContext } from "../Session" ;
3
3
import { Header , Input , Spacer } from "../components" ;
4
+ import fetchApi , { ApiError } from "../helpers/fetchApi" ;
5
+
6
+ type Errors = {
7
+ name ?: string ;
8
+ email ?: string ;
9
+ current_password ?: string ;
10
+ password ?: string ;
11
+ repeat_password ?: string ;
12
+ } ;
4
13
5
14
export type ProfileData = {
6
15
name : string ;
@@ -23,23 +32,68 @@ export const DEFAULT_PASSWORD_DATA: PasswordData = {
23
32
new_password2 : "" ,
24
33
} ;
25
34
35
+ // eslint-disable-next-line max-lines-per-function
26
36
export function Account ( ) {
27
37
const { session } = useContext ( SessionContext ) ;
28
- const [ profile , setProfile ] = useState < ProfileData > ( DEFAULT_PROFILE_DATA ) ;
38
+ const [ profile , setProfile ] = useState < ProfileData > ( {
39
+ ...DEFAULT_PROFILE_DATA ,
40
+ name : session . user ?. name ?? "" ,
41
+ email : session . user ?. email ?? "" ,
42
+ } ) ;
29
43
const [ password , setPassword ] = useState < PasswordData > ( DEFAULT_PASSWORD_DATA ) ;
44
+ const [ errors , setErrors ] = useState < Errors > ( { } ) ;
30
45
31
46
const storeProfile = ( key : keyof ProfileData , value : string ) => {
32
47
setProfile ( { ...profile , [ key ] : value } ) ;
33
48
} ;
34
49
const saveProfile = ( ) => {
35
- console . log ( profile ) ;
50
+ setErrors ( { } ) ;
51
+ fetchApi < ProfileData , ProfileData > (
52
+ "/auth/user" ,
53
+ session ?. token || "" ,
54
+ "put" ,
55
+ profile
56
+ )
57
+ . then ( ( user ) => {
58
+ setProfile ( user ) ;
59
+ setErrors ( { } ) ;
60
+ } )
61
+ . catch ( ( err : ApiError ) => {
62
+ if ( err . code === 400 ) {
63
+ setErrors ( { ...( err . body . errors as Errors ) } ) ;
64
+ }
65
+ } ) ;
36
66
} ;
37
67
38
68
const storePassword = ( key : keyof PasswordData , value : string ) => {
39
69
setPassword ( { ...password , [ key ] : value } ) ;
40
70
} ;
41
71
const savePassword = ( ) => {
42
- console . log ( password ) ;
72
+ setErrors ( { } ) ;
73
+
74
+ if ( password . new_password1 !== password . new_password2 ) {
75
+ setErrors ( { repeat_password : "Passwords do not match" } ) ;
76
+ return ;
77
+ }
78
+
79
+ fetchApi < PasswordData , { current_password : string ; password : string } > (
80
+ "/auth/password" ,
81
+ session ?. token || "" ,
82
+ "post" ,
83
+ {
84
+ current_password : password . current_password ,
85
+ password : password . new_password1 ,
86
+ }
87
+ )
88
+ . then ( ( ) => {
89
+ setPassword ( DEFAULT_PASSWORD_DATA ) ;
90
+ setErrors ( { } ) ;
91
+ } )
92
+ . catch ( ( err : ApiError ) => {
93
+ if ( err . code === 400 ) {
94
+ setErrors ( { ...( err . body . errors as Errors ) } ) ;
95
+ }
96
+ } ) ;
43
97
} ;
44
98
45
99
return (
@@ -50,12 +104,14 @@ export function Account() {
50
104
name = "name"
51
105
label = "Name"
52
106
value = { profile . name }
107
+ error = { errors . name }
53
108
onChange = { ( value ) => storeProfile ( "name" , value ) }
54
109
/>
55
110
< Input
56
111
name = "email"
57
112
label = "Email"
58
113
value = { profile . email }
114
+ error = { errors . email }
59
115
onChange = { ( value ) => storeProfile ( "email" , value ) }
60
116
/>
61
117
< button onClick = { saveProfile } > Save</ button >
@@ -66,21 +122,24 @@ export function Account() {
66
122
name = "current_password"
67
123
type = "password"
68
124
label = "Current Password"
69
- value = { password . current_password ?? "" }
125
+ value = { password . current_password }
126
+ error = { errors . current_password }
70
127
onChange = { ( value ) => storePassword ( "current_password" , value ) }
71
128
/>
72
129
< Input
73
130
name = "new_password1"
74
131
type = "password"
75
132
label = "New Password"
76
- value = { password . new_password1 ?? "" }
133
+ value = { password . new_password1 }
134
+ error = { errors . password }
77
135
onChange = { ( value ) => storePassword ( "new_password1" , value ) }
78
136
/>
79
137
< Input
80
138
name = "new_password2"
81
139
type = "password"
82
140
label = "Repeat New Password"
83
- value = { password . new_password2 ?? "" }
141
+ value = { password . new_password2 }
142
+ error = { errors . repeat_password }
84
143
onChange = { ( value ) => storePassword ( "new_password2" , value ) }
85
144
/>
86
145
0 commit comments