Skip to content

Commit e58d99c

Browse files
committed
Add logic to integrate fetching and updating of user in profile page
1 parent d8b903e commit e58d99c

File tree

2 files changed

+102
-29
lines changed

2 files changed

+102
-29
lines changed

apps/frontend/src/app/profile/page.tsx

Lines changed: 99 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,68 @@
11
"use client";
22
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";
414
import { Content } from "antd/es/layout/layout";
515
import "./styles.scss";
616
import { EditOutlined, SaveOutlined } from "@ant-design/icons";
717
import { useEffect, useState } from "react";
18+
import {
19+
UpdateUser,
20+
ValidateUser,
21+
VerifyTokenResponseType,
22+
} from "../services/user";
823

924
interface ProfilePageProps {}
1025

1126
const ProfilePage = (props: ProfilePageProps): JSX.Element => {
1227
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);
1331
const [isEditable, setIsEditable] = useState<boolean>(false);
1432
const [refresh, setRefresh] = useState<boolean>(false);
33+
const [messageApi, contextHolder] = message.useMessage();
1534

1635
useEffect(() => {
1736
// 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);
2446
});
2547
}, [refresh]);
2648

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+
2763
return (
2864
<Layout className="layout">
65+
{contextHolder}
2966
<Header selectedKey={undefined} />
3067
<Content className="content">
3168
<div className="content-card1">
@@ -34,16 +71,16 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
3471
<div className="left-header">
3572
{/* TODO: Replace with the first initial of username */}
3673
<Avatar size={80} className="avatar">
37-
A
74+
{username?.charAt(0).toUpperCase()}
3875
</Avatar>
3976
<div className="name-container">
4077
{/* 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>
4380
</div>
4481
</div>
4582
<div className="right-header">
46-
{isEditable && (
83+
{/* {isEditable && (
4784
<>
4885
<Button
4986
icon={<SaveOutlined />}
@@ -62,7 +99,7 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
6299
Cancel
63100
</Button>
64101
</>
65-
)}
102+
)} */}
66103
{!isEditable && (
67104
<Button
68105
icon={<EditOutlined />}
@@ -78,15 +115,21 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
78115
<Form
79116
form={form}
80117
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));
90133
}}
91134
layout="vertical"
92135
disabled={!isEditable}
@@ -126,17 +169,45 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
126169
<Form.Item
127170
name="password"
128171
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+
// ]}
135178
>
136-
<Input.Password name="password" type="password" />
179+
<Input.Password
180+
name="password"
181+
type="password"
182+
placeholder="Enter new password"
183+
/>
137184
</Form.Item>
138185
</Col>
139186
</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>
140211
</Form>
141212
</div>
142213
</div>

apps/frontend/src/app/profile/styles.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@
5757
width: 80px;
5858
}
5959

60-
.save-button {
60+
.cancel-button {
6161
margin-right: 8px;
62+
}
63+
.save-button {
6264
box-shadow: none !important;
6365
}
6466

0 commit comments

Comments
 (0)