Skip to content

Commit 9d29131

Browse files
committed
Configure error handling for existing username and email, handle cancel edit user details revert
1 parent d01d322 commit 9d29131

File tree

2 files changed

+44
-52
lines changed

2 files changed

+44
-52
lines changed

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

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,6 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
7777
</div>
7878
</div>
7979
<div className="right-header">
80-
{/* {isEditable && (
81-
<>
82-
<Button
83-
icon={<SaveOutlined />}
84-
type="primary"
85-
className="save-button"
86-
htmlType="submit"
87-
>
88-
Save
89-
</Button>
90-
<Button
91-
className="cancel-button"
92-
onClick={() => {
93-
setIsEditable(false);
94-
}}
95-
>
96-
Cancel
97-
</Button>
98-
</>
99-
)} */}
10080
{!isEditable && (
10181
<Button
10282
icon={<EditOutlined />}
@@ -125,7 +105,9 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
125105
setRefresh(!refresh);
126106
success("Profile Updated");
127107
})
128-
.catch((error) => error(error.message));
108+
.catch((errors: Error) => {
109+
error(errors.message);
110+
});
129111
}}
130112
layout="vertical"
131113
disabled={!isEditable}
@@ -156,22 +138,13 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
156138
},
157139
]}
158140
>
159-
<Input name="email" type="email" disabled />
141+
<Input name="email" type="email" />
160142
</Form.Item>
161143
</Col>
162144
</Row>
163145
<Row gutter={16}>
164146
<Col span={12}>
165-
<Form.Item
166-
name="password"
167-
label="Password"
168-
// rules={[
169-
// {
170-
// required: true,
171-
// message: "Please enter valid password!",
172-
// },
173-
// ]}
174-
>
147+
<Form.Item name="password" label="Password">
175148
<Input.Password
176149
name="password"
177150
type="password"
@@ -188,6 +161,9 @@ const ProfilePage = (props: ProfilePageProps): JSX.Element => {
188161
<Button
189162
className="cancel-button"
190163
onClick={() => {
164+
form.setFieldValue("username", username);
165+
form.setFieldValue("email", email);
166+
form.setFieldValue("password", undefined);
191167
setIsEditable(false);
192168
}}
193169
>

apps/frontend/src/app/services/user.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const USER_SERVICE_URL = process.env.NEXT_PUBLIC_USER_SERVICE_URL
1+
const USER_SERVICE_URL = process.env.NEXT_PUBLIC_USER_SERVICE_URL;
22

33
export interface User {
44
id: string;
@@ -25,51 +25,65 @@ export interface VerifyTokenResponseType {
2525
}
2626

2727
type UserResponseData = {
28-
"id": string,
29-
"username": string,
30-
"email": string,
31-
"isAdmin": boolean,
32-
"createdAt": string,
33-
}
28+
id: string;
29+
username: string;
30+
email: string;
31+
isAdmin: boolean;
32+
createdAt: string;
33+
};
3434

3535
function withDefaultHeaders(opts: RequestInit): RequestInit {
3636
return {
3737
...opts,
3838
headers: {
3939
...(opts.headers ?? {}),
40-
"Content-Type": "application/json"
40+
"Content-Type": "application/json",
4141
},
42-
}
42+
};
4343
}
4444

45-
export async function createUser(username: string, email: string, password: string): Promise<void> {
45+
export async function createUser(
46+
username: string,
47+
email: string,
48+
password: string
49+
): Promise<void> {
4650
const opts = withDefaultHeaders({
4751
method: "POST",
4852
body: JSON.stringify({
49-
username, email, password
53+
username,
54+
email,
55+
password,
5056
}),
51-
})
52-
const res = await fetch(`${USER_SERVICE_URL}users`, opts)
57+
});
58+
const res = await fetch(`${USER_SERVICE_URL}users`, opts);
5359

5460
if (!res.ok) {
55-
throw new Error(`User service responded with ${res.status}: ${await res.text()}`)
61+
throw new Error(
62+
`User service responded with ${res.status}: ${await res.text()}`
63+
);
5664
}
5765
}
5866

59-
export async function loginUser(email: string, password: string): Promise<string> {
67+
export async function loginUser(
68+
email: string,
69+
password: string
70+
): Promise<string> {
6071
const opts = withDefaultHeaders({
6172
method: "POST",
6273
body: JSON.stringify({
63-
email, password
64-
})
74+
email,
75+
password,
76+
}),
6577
});
6678
const res = await fetch(`${USER_SERVICE_URL}auth/login`, opts);
6779

6880
if (!res.ok) {
69-
throw new Error(`User service responded with ${res.status}: ${await res.text()}`);
81+
throw new Error(
82+
`User service responded with ${res.status}: ${await res.text()}`
83+
);
7084
}
7185

72-
const ret: { "data": { "accessToken": string } } = await res.json();
86+
const ret: { data: { accessToken: string } } = await res.json();
7387
return ret.data.accessToken;
7488
}
7589

@@ -89,9 +103,11 @@ export const UpdateUser = async (
89103
body: JSON.stringify(user),
90104
}
91105
);
92-
93106
if (response.status === 200) {
94107
return response.json();
108+
} else if (response.status === 409) {
109+
const errorResponse = await response.json();
110+
throw new Error(errorResponse.message);
95111
} else {
96112
throw new Error(
97113
`Error updating user: ${response.status} ${response.statusText}`

0 commit comments

Comments
 (0)