import ArrowLeftWhiteD from "@/components/icons/ArrowLeftWhiteD"; import { changeDateType, handleTimeExpiresToken, isValidIranNationalCode, removeAdditionalKeysDashboard, } from "@/helper"; import { request } from "@/services/response"; import { Button, CircularProgress } from "@mui/material"; import Cookies from "js-cookie"; import { toast } from "react-toastify"; function ButtonSendData({ loading, errors, information, setInformation, setLoading, setErrors, }) { const handleErrReq = (err) => { const errors = err?.response?.data; if (errors) { setErrors(typeof errors === "string" ? {} : errors); } setLoading(false); }; const postData = () => { request .postUserProfile(changeDateType(information, true)) .then((response) => { if (response.uuid) { fetch("/api/auth/refresh", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ refresh_token: Cookies.get("refresh_token") }), }) .then((r) => r.json()) .then((res) => { if (res?.access_token) { const expiresTime = handleTimeExpiresToken(res.expires_in); Cookies.set("access_token", res.access_token, { expires: expiresTime.accessTokenExpires, path: "/", secure: true, sameSite: "strict", }); } setInformation({ ...information, prev_data: true, uuid: response.uuid, }); setLoading(false); toast.success("اطلاعات با موفقیت ثبت شد"); }) .catch(() => { setLoading(false); }); } }) .catch((err) => { handleErrReq(err); }); }; const patchData = () => { const usedKays = removeAdditionalKeysDashboard(information); request .patchUserProfile( changeDateType(usedKays, true), information?.uuid, Cookies.get("access_token") ) .then(() => { setLoading(false); toast.success("اطلاعات با موفقیت ثبت شد"); }) .catch((err) => handleErrReq(err)); }; const sendReq = () => { if (information?.national_code && !isValidIranNationalCode(information.national_code)) { setErrors((prev) => ({ ...prev, national_code: true })); toast.error("کد ملی وارد شده معتبر نیست"); return; } setLoading(true); information.prev_data ? patchData() : postData(); }; const isDisabled = () => { return loading || Object.values(errors).some(Boolean) || !information; }; return ( ); } export default ButtonSendData;