feat: add toast to user privacy setting update

This commit is contained in:
Kentai Radiquum 2025-03-22 01:36:14 +05:00
parent 0bf00b11e5
commit e3fe979714
Signed by: Radiquum
GPG key ID: 858E8EE696525EED

View file

@ -1,8 +1,10 @@
"use client"; "use client";
import { Modal } from "flowbite-react"; import { Modal, useThemeMode } from "flowbite-react";
import { ENDPOINTS } from "#/api/config"; import { ENDPOINTS } from "#/api/config";
import { useState } from "react"; import { useState } from "react";
import { toast } from "react-toastify";
import { tryCatchAPI } from "#/api/utils";
export const ProfileEditPrivacyModal = (props: { export const ProfileEditPrivacyModal = (props: {
isOpen: boolean; isOpen: boolean;
@ -33,33 +35,60 @@ export const ProfileEditPrivacyModal = (props: {
}; };
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const theme = useThemeMode();
function _setPrivacySetting(el: any) { async function _setPrivacySetting(el: any) {
let privacySettings = structuredClone(props.privacySettings); let privacySettings = structuredClone(props.privacySettings);
setLoading(true); setLoading(true);
fetch(_endpoints[props.setting], {
method: "POST", const tid = toast.loading("Обновление настроек приватности...", {
headers: { position: "bottom-center",
"Content-Type": "application/json", hideProgressBar: true,
}, closeOnClick: false,
body: JSON.stringify({ pauseOnHover: false,
permission: el.target.value, draggable: false,
}), theme: theme.mode == "light" ? "light" : "dark",
}) });
.then((res) => {
if (res.ok) { const { data, error } = await tryCatchAPI(
setLoading(false); fetch(_endpoints[props.setting], {
privacySettings[el.target.name] = el.target.value; method: "POST",
props.setPrivacySettings(privacySettings); headers: {
props.setIsOpen(false) "Content-Type": "application/json",
} else { },
new Error("failed to send data"); body: JSON.stringify({
} permission: el.target.value,
}),
}) })
.catch((err) => { );
console.log(err);
setLoading(false); if (error) {
toast.update(tid, {
render: "Ошибка обновления настроек приватности",
type: "error",
autoClose: 2500,
isLoading: false,
closeOnClick: true,
draggable: true,
}); });
setLoading(false);
return;
}
toast.update(tid, {
render: "Настройки приватности обновлены",
type: "success",
autoClose: 2500,
isLoading: false,
closeOnClick: true,
draggable: true,
});
setLoading(false);
privacySettings[el.target.name] = el.target.value;
props.setPrivacySettings(privacySettings);
props.setIsOpen(false);
} }
return ( return (
@ -71,10 +100,10 @@ export const ProfileEditPrivacyModal = (props: {
> >
<Modal.Header>{setting_text[props.setting]}</Modal.Header> <Modal.Header>{setting_text[props.setting]}</Modal.Header>
<Modal.Body> <Modal.Body>
{props.setting != "none" ? ( {props.setting != "none" ?
<> <>
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
{props.setting == "privacy_friend_requests" ? ( {props.setting == "privacy_friend_requests" ?
<> <>
<div className="flex items-center"> <div className="flex items-center">
<input <input
@ -113,8 +142,7 @@ export const ProfileEditPrivacyModal = (props: {
</label> </label>
</div> </div>
</> </>
) : ( : <>
<>
<div className="flex items-center"> <div className="flex items-center">
<input <input
disabled={loading} disabled={loading}
@ -170,12 +198,10 @@ export const ProfileEditPrivacyModal = (props: {
</label> </label>
</div> </div>
</> </>
)} }
</div> </div>
</> </>
) : ( : ""}
""
)}
</Modal.Body> </Modal.Body>
</Modal> </Modal>
); );