1
1
"use client" ;
2
2
import Header from "@/components/Header/header" ;
3
- import { Avatar , Button , Col , Divider , Form , Input , Layout , Row } from "antd" ;
3
+ import {
4
+ Avatar ,
5
+ Button ,
6
+ Col ,
7
+ Divider ,
8
+ Form ,
9
+ Input ,
10
+ Layout ,
11
+ message ,
12
+ Row ,
13
+ } from "antd" ;
4
14
import { Content } from "antd/es/layout/layout" ;
5
15
import "./styles.scss" ;
6
16
import { EditOutlined , SaveOutlined } from "@ant-design/icons" ;
7
17
import { useEffect , useState } from "react" ;
18
+ import {
19
+ UpdateUser ,
20
+ ValidateUser ,
21
+ VerifyTokenResponseType ,
22
+ } from "../services/user" ;
8
23
9
24
interface ProfilePageProps { }
10
25
11
26
const ProfilePage = ( props : ProfilePageProps ) : JSX . Element => {
12
27
const [ form ] = Form . useForm ( ) ;
28
+ const [ id , setId ] = useState < string | undefined > ( undefined ) ;
29
+ const [ email , setEmail ] = useState < string | undefined > ( undefined ) ;
30
+ const [ username , setUsername ] = useState < string | undefined > ( undefined ) ;
13
31
const [ isEditable , setIsEditable ] = useState < boolean > ( false ) ;
14
32
const [ refresh , setRefresh ] = useState < boolean > ( false ) ;
33
+ const [ messageApi , contextHolder ] = message . useMessage ( ) ;
15
34
16
35
useEffect ( ( ) => {
17
36
// TODO: Retrieve the user details via validate JWT token api
18
-
19
- // TODO: Set initial form value with the retrieved value above
20
- form . setFieldsValue ( {
21
- username : "" ,
22
- password : "" ,
23
- email : "" ,
37
+ ValidateUser ( ) . then ( ( data : VerifyTokenResponseType ) => {
38
+ form . setFieldsValue ( {
39
+ username : data . data . username ,
40
+ password : "" ,
41
+ email : data . data . email ,
42
+ } ) ;
43
+ setId ( data . data . id ) ;
44
+ setEmail ( data . data . email ) ;
45
+ setUsername ( data . data . username ) ;
24
46
} ) ;
25
47
} , [ refresh ] ) ;
26
48
49
+ const success = ( message : string ) => {
50
+ messageApi . open ( {
51
+ type : "success" ,
52
+ content : message ,
53
+ } ) ;
54
+ } ;
55
+
56
+ const error = ( message : string ) => {
57
+ messageApi . open ( {
58
+ type : "error" ,
59
+ content : message ,
60
+ } ) ;
61
+ } ;
62
+
27
63
return (
28
64
< Layout className = "layout" >
65
+ { contextHolder }
29
66
< Header selectedKey = { undefined } />
30
67
< Content className = "content" >
31
68
< div className = "content-card1" >
@@ -34,16 +71,16 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
34
71
< div className = "left-header" >
35
72
{ /* TODO: Replace with the first initial of username */ }
36
73
< Avatar size = { 80 } className = "avatar" >
37
- A
74
+ { username ?. charAt ( 0 ) . toUpperCase ( ) }
38
75
</ Avatar >
39
76
< div className = "name-container" >
40
77
{ /* TODO: Set the value in field correctly within the useEffect above and this should work */ }
41
- < div className = "username" > { form . getFieldValue ( " username" ) } </ div >
42
- < div className = "email" > { form . getFieldValue ( " email" ) } </ div >
78
+ < div className = "username" > { username } </ div >
79
+ < div className = "email" > { email } </ div >
43
80
</ div >
44
81
</ div >
45
82
< div className = "right-header" >
46
- { isEditable && (
83
+ { /* { isEditable && (
47
84
<>
48
85
<Button
49
86
icon={<SaveOutlined />}
@@ -62,7 +99,7 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
62
99
Cancel
63
100
</Button>
64
101
</>
65
- ) }
102
+ )} */ }
66
103
{ ! isEditable && (
67
104
< Button
68
105
icon = { < EditOutlined /> }
@@ -78,15 +115,21 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
78
115
< Form
79
116
form = { form }
80
117
onFinish = { ( values ) => {
81
- // TODO: Make PATCH api request to update user details here
82
-
83
- // TODO: On success show success notification message else display error notification
84
-
85
- // Set editable to false
86
- setIsEditable ( false ) ;
87
-
88
- // Refresh data
89
- setRefresh ( ! refresh ) ;
118
+ // TODO: Check password
119
+ let data = values ;
120
+ if ( ! values . password || values . password . trim ( ) === "" ) {
121
+ data = {
122
+ username : values . username ,
123
+ email : values . email ,
124
+ } ;
125
+ }
126
+ UpdateUser ( data , id as string )
127
+ . then ( ( value ) => {
128
+ setIsEditable ( false ) ;
129
+ setRefresh ( ! refresh ) ;
130
+ success ( "Profile Updated" ) ;
131
+ } )
132
+ . catch ( ( error ) => error ( error . message ) ) ;
90
133
} }
91
134
layout = "vertical"
92
135
disabled = { ! isEditable }
@@ -126,17 +169,45 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
126
169
< Form . Item
127
170
name = "password"
128
171
label = "Password"
129
- rules = { [
130
- {
131
- required : true ,
132
- message : "Please enter valid password!" ,
133
- } ,
134
- ] }
172
+ // rules={[
173
+ // {
174
+ // required: true,
175
+ // message: "Please enter valid password!",
176
+ // },
177
+ // ]}
135
178
>
136
- < Input . Password name = "password" type = "password" />
179
+ < Input . Password
180
+ name = "password"
181
+ type = "password"
182
+ placeholder = "Enter new password"
183
+ />
137
184
</ Form . Item >
138
185
</ Col >
139
186
</ Row >
187
+ < Form . Item
188
+ style = { { display : "flex" , justifyContent : "flex-end" } }
189
+ >
190
+ { isEditable && (
191
+ < >
192
+ < Button
193
+ className = "cancel-button"
194
+ onClick = { ( ) => {
195
+ setIsEditable ( false ) ;
196
+ } }
197
+ >
198
+ Cancel
199
+ </ Button >
200
+ < Button
201
+ icon = { < SaveOutlined /> }
202
+ type = "primary"
203
+ className = "save-button"
204
+ htmlType = "submit"
205
+ >
206
+ Save
207
+ </ Button >
208
+ </ >
209
+ ) }
210
+ </ Form . Item >
140
211
</ Form >
141
212
</ div >
142
213
</ div >
0 commit comments