Skip to content

Commit 19a73be

Browse files
committed
feat: profile delete
1 parent d227516 commit 19a73be

File tree

3 files changed

+91
-12
lines changed

3 files changed

+91
-12
lines changed

frontend/src/app/admin/question/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ const QuestionPage = () => {
311311
</div>
312312
</dialog>
313313
<dialog id="delete_modal" className="modal">
314-
<div className="modal-box max-w-screen-xl p-6">
314+
<div className="max-w-screen modal-box p-6">
315315
<form method="dialog" className="pb">
316316
<button className="btn btn-circle btn-ghost btn-sm absolute right-2 top-2">
317317

frontend/src/app/api/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const FetchAuth = {
3838
const res = await fetch(url, options);
3939
if (!res.ok) {
4040
console.log(res);
41-
// throw Error();
41+
throw Error();
4242
}
4343
return res;
4444
},
@@ -139,6 +139,12 @@ export const deleteQuestionUrl = async (questionId: string) => {
139139
}).then((res) => res.json());
140140
};
141141

142+
export const deleteProfileUrl = async () => {
143+
return FetchAuth.fetch(`${API_URL}/users/profile`, {
144+
method: "DELETE",
145+
}).then((res) => res.json());
146+
};
147+
142148
interface ProfileResponse {
143149
payload: Profile;
144150
statusMessage: {

frontend/src/app/settings/page.tsx

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
"use client";
22
import { ChangeEvent, useEffect, useState } from "react";
33
import { BiUserCircle } from "@react-icons/all-files/bi/BiUserCircle";
4-
import { fetchProfileUrl, updateProfileUrl } from "../api";
4+
import {
5+
deleteProfileUrl,
6+
deleteQuestionUrl,
7+
fetchProfileUrl,
8+
updateProfileUrl,
9+
} from "../api";
510
import useLogin from "../hooks/useLogin";
6-
import useAdmin from "../hooks/useAdmin";
711
import { message } from "antd";
12+
import { useMutation } from "@tanstack/react-query";
813

914
const SettingPage = () => {
1015
const user = useLogin();
1116

17+
const deleteProfileMutation = useMutation(async () => deleteProfileUrl(), {
18+
onSuccess: () => {
19+
closeModal("delete_modal");
20+
api.open({
21+
type: "success",
22+
content: "Successfully deleted profile!",
23+
});
24+
},
25+
onError: (e) => {
26+
api.open({
27+
type: "error",
28+
content: "Failed to delete profile!",
29+
});
30+
},
31+
});
32+
1233
useEffect(() => {
1334
fetchProfileUrl().then((res) => {
1435
setProfileImageUrl(res.payload.imageUrl);
1536
setPreferredLang(res.payload.preferredLang);
1637
setName(res.payload.name || "");
1738
});
18-
}, []);
19-
20-
// useEffect(() => {
21-
// setName(user?.reloadUserInfo?.displayName ?? null);
22-
// }, [user]);
39+
}, [deleteProfileMutation.status]);
2340

2441
const [api, contextHolder] = message.useMessage();
2542

@@ -53,9 +70,56 @@ const SettingPage = () => {
5370
});
5471
};
5572

73+
const onEscKeyDown = (e: React.KeyboardEvent<HTMLDialogElement>) => {
74+
if (e.key == "Escape" || e.key === "Esc") {
75+
e.preventDefault();
76+
}
77+
};
78+
79+
const onClickModal = (modalId: string) => {
80+
if (document) {
81+
(document.getElementById(modalId) as HTMLFormElement).showModal();
82+
}
83+
};
84+
85+
const closeModal = (modalId: string) => {
86+
(document.getElementById(modalId) as HTMLFormElement).close();
87+
};
88+
5689
return (
5790
<main className="flex flex-col items-center gap-4 p-12">
5891
{contextHolder}
92+
<dialog id="delete_modal" className="modal">
93+
<div className="modal-box p-6">
94+
<form method="dialog" className="pb">
95+
<button className="btn btn-circle btn-ghost btn-sm absolute right-2 top-2">
96+
97+
</button>
98+
</form>
99+
<h1 className="m-4">Are you sure you want to delete your profile?</h1>
100+
<div className="flex justify-end gap-2">
101+
<button
102+
type="reset"
103+
onClick={() => {
104+
closeModal("delete_modal");
105+
}}
106+
className="btn btn-error btn-sm text-white hover:bg-red-500"
107+
>
108+
No
109+
</button>
110+
<button
111+
type="submit"
112+
className="btn btn-accent btn-sm text-white"
113+
onClick={() => {
114+
deleteProfileMutation.mutate();
115+
closeModal("delete_modal");
116+
}}
117+
>
118+
Yes
119+
</button>
120+
</div>
121+
</div>
122+
</dialog>
59123
<h1 className="text-5xl font-bold text-white underline">User Profile</h1>
60124
<form
61125
className="flex flex-col justify-center gap-8 bg-secondary p-12"
@@ -110,9 +174,18 @@ const SettingPage = () => {
110174
</select>
111175
</div>
112176
</section>
113-
<button type="submit" className="btn btn-accent">
114-
Save Changes
115-
</button>
177+
<div className="flex flex-row justify-between gap-10">
178+
<button
179+
type="button"
180+
className="btn btn-error"
181+
onClick={() => onClickModal("delete_modal")}
182+
>
183+
Delete Profile
184+
</button>
185+
<button type="submit" className="btn btn-accent flex-grow">
186+
Save Changes
187+
</button>
188+
</div>
116189
</form>
117190
</main>
118191
);

0 commit comments

Comments
 (0)