- JalaliDatePicker gains an opt-in `stepwise` mode (year → month → day): the year step is a scrollable list from the current Jalali year down to 1332, used by the birthday field. Default-off so appointment/panel pickers keep the month-grid behavior. Parse incoming Jalali string values safely (fixes NaN keys). - Birthday create path now converts Jalali→Unix on send (changeDateType true), matching the update path and the Unix-based backend contract. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
115 lines
3.1 KiB
JavaScript
115 lines
3.1 KiB
JavaScript
import ArrowLeftWhiteD from "@/components/icons/ArrowLeftWhiteD";
|
|
import {
|
|
changeDateType,
|
|
handleTimeExpiresToken,
|
|
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 = () => {
|
|
setLoading(true);
|
|
|
|
information.prev_data ? patchData() : postData();
|
|
};
|
|
const isDisabled = () => {
|
|
return loading || Object.values(errors).some(Boolean) || !information;
|
|
};
|
|
|
|
return (
|
|
<Button
|
|
variant="contained"
|
|
onClick={sendReq}
|
|
disabled={loading || isDisabled()}
|
|
className={`!gap-[4px] !mr-auto !text-nowrap !flex !flex-nowrap !py-[8px]
|
|
!rounded-[4px] !text-[14px] md:!text-[16px] !font-medium
|
|
!mt-[32px] sm:!mt-[38px] md:!mt-[43px] lg:!mt-[48px] !px-[16px] sm:!px-[19px] md:!px-[22px] lg:!px-[24px]
|
|
`}
|
|
>
|
|
{loading ? (
|
|
<CircularProgress size={20} sx={{ color: "#fff" }} />
|
|
) : (
|
|
<>
|
|
ثبت اطلاعات
|
|
<ArrowLeftWhiteD />
|
|
</>
|
|
)}
|
|
</Button>
|
|
);
|
|
}
|
|
|
|
export default ButtonSendData;
|