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,10 +35,22 @@ 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);
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], { fetch(_endpoints[props.setting], {
method: "POST", method: "POST",
headers: { headers: {
@ -46,20 +60,35 @@ export const ProfileEditPrivacyModal = (props: {
permission: el.target.value, permission: el.target.value,
}), }),
}) })
.then((res) => { );
if (res.ok) {
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); setLoading(false);
privacySettings[el.target.name] = el.target.value; privacySettings[el.target.name] = el.target.value;
props.setPrivacySettings(privacySettings); props.setPrivacySettings(privacySettings);
props.setIsOpen(false) props.setIsOpen(false);
} else {
new Error("failed to send data");
}
})
.catch((err) => {
console.log(err);
setLoading(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>
); );