diff --git a/src/features/province/components/province-mandatory-allocations-register-code/ProvinceMandatoryAllocationsRegisterCode.js b/src/features/province/components/province-mandatory-allocations-register-code/ProvinceMandatoryAllocationsRegisterCode.js new file mode 100644 index 0000000..152c181 --- /dev/null +++ b/src/features/province/components/province-mandatory-allocations-register-code/ProvinceMandatoryAllocationsRegisterCode.js @@ -0,0 +1,119 @@ +import { FormControlLabel, Radio, Typography } from "@mui/material"; +import { useContext, useEffect, useState } from "react"; +import { useDispatch } from "react-redux"; +import axios from "axios"; +import { Grid } from "../../../../components/grid/Grid"; +import { + LOADING_END, + LOADING_START, +} from "../../../../lib/redux/slices/appSlice"; +import { AppContext } from "../../../../contexts/AppContext"; + +export const ProvinceMandatoryAllocationsRegisterCode = () => { + const [isActive, setIsActive] = useState(false); + const [dataId, setDataId] = useState(null); + const dispatch = useDispatch(); + const [openNotif] = useContext(AppContext); + + useEffect(() => { + const fetchData = async () => { + try { + dispatch(LOADING_START()); + const response = await axios.get("/allocations-register-code", { + params: { + id: "1", + key: "423gfafsg4324t", + active: false, + time: "10", + }, + }); + if (response.data) { + setIsActive(response.data.active); + setDataId(response.data.id); + } + dispatch(LOADING_END()); + } catch (error) { + console.error("Error fetching data:", error); + dispatch(LOADING_END()); + } + }; + + fetchData(); + }, [dispatch]); + + const handleUpdate = async (newValue) => { + if (dataId === null) return; + + try { + dispatch(LOADING_START()); + await axios.put(`/allocations-register-code/${dataId}/`, { + active: newValue, + }); + setIsActive(newValue); + openNotif({ + vertical: "top", + horizontal: "center", + msg: "تغییرات با موفقیت ثبت شد", + severity: "success", + }); + dispatch(LOADING_END()); + } catch (error) { + console.error("Error updating data:", error); + openNotif({ + vertical: "top", + horizontal: "center", + msg: "خطا در ثبت تغییرات", + severity: "error", + }); + dispatch(LOADING_END()); + } + }; + + return ( + + + اجباری بودن کد احراز توزیع لاشه داخل استان + + + handleUpdate(true)} + /> + } + label="فعال" + /> + handleUpdate(false)} + /> + } + label="غیر فعال" + /> + + + ); +};