|
1 | 1 | "use client";
|
2 | 2 | import { ChangeEvent, useEffect, useState } from "react";
|
3 | 3 | 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"; |
5 | 10 | import useLogin from "../hooks/useLogin";
|
6 |
| -import useAdmin from "../hooks/useAdmin"; |
7 | 11 | import { message } from "antd";
|
| 12 | +import { useMutation } from "@tanstack/react-query"; |
8 | 13 |
|
9 | 14 | const SettingPage = () => {
|
10 | 15 | const user = useLogin();
|
11 | 16 |
|
| 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 | + |
12 | 33 | useEffect(() => {
|
13 | 34 | fetchProfileUrl().then((res) => {
|
14 | 35 | setProfileImageUrl(res.payload.imageUrl);
|
15 | 36 | setPreferredLang(res.payload.preferredLang);
|
16 | 37 | setName(res.payload.name || "");
|
17 | 38 | });
|
18 |
| - }, []); |
19 |
| - |
20 |
| - // useEffect(() => { |
21 |
| - // setName(user?.reloadUserInfo?.displayName ?? null); |
22 |
| - // }, [user]); |
| 39 | + }, [deleteProfileMutation.status]); |
23 | 40 |
|
24 | 41 | const [api, contextHolder] = message.useMessage();
|
25 | 42 |
|
@@ -53,9 +70,56 @@ const SettingPage = () => {
|
53 | 70 | });
|
54 | 71 | };
|
55 | 72 |
|
| 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 | + |
56 | 89 | return (
|
57 | 90 | <main className="flex flex-col items-center gap-4 p-12">
|
58 | 91 | {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> |
59 | 123 | <h1 className="text-5xl font-bold text-white underline">User Profile</h1>
|
60 | 124 | <form
|
61 | 125 | className="flex flex-col justify-center gap-8 bg-secondary p-12"
|
@@ -110,9 +174,18 @@ const SettingPage = () => {
|
110 | 174 | </select>
|
111 | 175 | </div>
|
112 | 176 | </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> |
116 | 189 | </form>
|
117 | 190 | </main>
|
118 | 191 | );
|
|
0 commit comments