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