push rasad front on new repo

This commit is contained in:
2026-01-18 14:32:49 +03:30
commit 4fe6e70525
2139 changed files with 303150 additions and 0 deletions

View File

@@ -0,0 +1,249 @@
import React, { useEffect, useState, useRef } from "react";
import { Box, Button, Grid, TextField, Chip } from "@mui/material";
import { useDispatch, useSelector } from "react-redux";
import { BackButton } from "../../../../components/back-button/BackButton";
import { OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { SlaughterManageDelegatesForm } from "./SlaughterManageDelegatesForm";
import { SlaughterManageDelegatesOperations } from "./SlaughterManageDelegatesOperations";
import { slaughterGetDelegatesInfoService } from "../../services/slaughter-get-delegates-info";
import ResponsiveTable from "../../../../components/responsive-table/ResponsiveTable";
import { SPACING } from "../../../../data/spacing";
import { RiSearchLine } from "react-icons/ri";
import { formatJustDate } from "../../../../utils/formatTime";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
import { checkPathStartsWith } from "../../../../utils/checkPathStartsWith";
const SlaughterManageDelegates = () => {
const dispatch = useDispatch();
const [data, setData] = useState([]);
const [totalRows, setTotalRows] = useState(0);
const [perPage, setPerPage] = useState(10);
const [textValue, setTextValue] = useState("");
const [page, setPage] = useState(1);
const [tableData, setTableData] = useState([]);
const isFirstMount = useRef(true);
const isSteward = getRoleFromUrl() === "Steward";
const selectedSubUser = useSelector(
(state) => state.userSlice.selectedSubUser
);
const handleTextChange = (e) => setTextValue(e.target.value);
const fetchApiData = async (pageNum) => {
const response = await dispatch(
slaughterGetDelegatesInfoService({
search: "filter",
value: textValue,
page: pageNum,
page_size: perPage,
role_key:
checkPathStartsWith("slaughter") || checkPathStartsWith("steward")
? selectedSubUser?.key || ""
: "",
})
);
if (response.payload.error) {
console.error("Error fetching data:", response.payload.error);
setData([]);
setTotalRows(0);
} else {
setData(response.payload.data?.results || []);
const count = Number(response.payload.data?.count) || 0;
setTotalRows(count);
}
};
const handlePageChange = (pageNum) => {
fetchApiData(pageNum);
setPage(pageNum);
};
const handlePerRowsChange = (perRows) => {
setPerPage(Number(perRows));
setPage(1);
};
const updateTableData = () => {
fetchApiData(page !== 0 ? page : 1);
};
useEffect(() => {
if (!data || !Array.isArray(data)) {
setTableData([]);
return;
}
const d = data.map((item, i) => {
const isActive = item?.trash !== undefined ? !item.trash : null;
const statusBadge =
isActive === null ? (
"-"
) : (
<Chip
label={isActive ? "فعال" : "غیرفعال"}
color={isActive ? "success" : "error"}
size="small"
sx={{ minWidth: 80 }}
/>
);
const hasLimitation = item?.limitation;
const limitationBadge = (
<Chip
label={hasLimitation ? "دارد" : "ندارد"}
color={hasLimitation ? "warning" : "default"}
size="small"
sx={{ minWidth: 60 }}
/>
);
const killhouseDisplay =
item?.killHouse?.name && item?.killHouse?.mobile
? `${item.killHouse.name} (${item.killHouse.mobile})`
: item?.killHouse?.name
? item.killHouse.name
: "-";
const stewardDisplay =
item?.steward?.name && item?.steward?.user?.mobile
? `${item.steward.name} (${item.steward.user.mobile})`
: item?.steward?.name
? item.steward.name
: "-";
const displayValue = isSteward ? stewardDisplay : killhouseDisplay;
return [
page === 1 ? i + 1 : i + perPage * (page - 1) + 1,
item?.createDate ? formatJustDate(item.createDate) : "-",
item?.firstName || "-",
item?.lastName || "-",
item?.mobile || "-",
item?.city || "-",
displayValue,
limitationBadge,
item?.governmentalLimitationWeight || 0,
item?.freeLimitationWeight || 0,
statusBadge,
<SlaughterManageDelegatesOperations
key={`operations-${item?.key || i}`}
item={item}
updateTable={updateTableData}
/>,
];
});
setTableData(d);
}, [data, page, perPage]);
useEffect(() => {
fetchApiData(1);
}, [selectedSubUser?.key]);
useEffect(() => {
if (isFirstMount.current) {
isFirstMount.current = false;
return;
}
fetchApiData(1);
setPage(1);
}, [perPage]);
const handleSubmit = async (e) => {
e.preventDefault();
setPage(1);
const response = await dispatch(
slaughterGetDelegatesInfoService({
search: "filter",
value: textValue,
page: 1,
page_size: perPage,
role_key: checkPathStartsWith("slaughter")
? selectedSubUser?.key || ""
: "",
})
);
if (response.payload.error) {
console.error("Error fetching data:", response.payload.error);
setData([]);
setTotalRows(0);
} else {
setData(response.payload.data?.results || []);
const count = Number(response.payload.data?.count) || 0;
setTotalRows(count);
}
};
const handleOpenModal = () => {
dispatch(
OPEN_MODAL({
title: "ثبت نماینده جدید",
content: <SlaughterManageDelegatesForm updateTable={updateTableData} />,
size: 300,
})
);
};
return (
<Box display="flex" justifyContent="center">
<Grid container xs={12} sm={12} md={10} lg={10}>
<Grid item xs={12}>
<BackButton />
</Grid>
<Grid container item gap={SPACING.SMALL}>
<Button variant="contained" color="primary" onClick={handleOpenModal}>
ثبت نماینده جدید
</Button>
<form onSubmit={handleSubmit}>
<Grid container alignItems="center" gap={SPACING.SMALL}>
<TextField
size="small"
autoComplete="off"
label="جستجو"
variant="outlined"
style={{ width: 200 }}
value={textValue}
onChange={handleTextChange}
/>
<Button
type="submit"
onClick={handleSubmit}
endIcon={<RiSearchLine />}
>
جستجو
</Button>
</Grid>
</form>
</Grid>
<Grid item xs={12} mt={2}>
<ResponsiveTable
title="نمایندگان"
columns={[
"ردیف",
"تاریخ ایجاد",
"نام",
"نام خانوادگی",
"شماره همراه",
"شهر",
isSteward ? "مباشر" : "کشتارگاه",
"محدودیت فروش",
"حداکثر فروش دولتی",
"حداکثر فروش آزاد",
"وضعیت",
"عملیات",
]}
customWidth={"100%"}
data={tableData}
handlePageChange={handlePageChange}
totalRows={totalRows}
page={page}
perPage={perPage}
handlePerRowsChange={handlePerRowsChange}
/>
</Grid>
</Grid>
</Box>
);
};
export default SlaughterManageDelegates;

View File

@@ -0,0 +1,165 @@
import React, { useContext } from "react";
import { useFormik } from "formik";
import * as yup from "yup";
import { useDispatch } from "react-redux";
import { Button, TextField } from "@mui/material";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { CLOSE_MODAL } from "../../../../lib/redux/slices/appSlice";
import { AppContext } from "../../../../contexts/AppContext";
import {
slaughterSubmitRepresentativeService,
slaughterEditRepresentativeService,
} from "../../services/slaughter-submit-delegate";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";
const getValidationSchema = () =>
yup.object({
first_name: yup.string().required("نام الزامی است"),
last_name: yup.string().required("نام خانوادگی الزامی است"),
mobile: yup
.string()
.required("شماره همراه الزامی است")
.matches(/^09\d{9}$/, "شماره تلفن باید با 09 شروع شود و 11 رقم باشد"),
city: yup.string().required("شهر الزامی است"),
});
export const SlaughterManageDelegatesForm = ({ updateTable, item, isEdit }) => {
const dispatch = useDispatch();
const [openNotif] = useContext(AppContext);
const formik = useFormik({
initialValues: {
first_name: item?.first_name || item?.firstName || "",
last_name: item?.last_name || item?.lastName || "",
mobile: item?.mobile || "",
city: item?.city || "",
},
enableReinitialize: true,
validationSchema: getValidationSchema(),
onSubmit: (values) => {
const submitData = isEdit
? {
key: item?.key,
first_name: values.first_name,
last_name: values.last_name,
mobile: values.mobile,
city: values.city,
}
: {
first_name: values.first_name,
last_name: values.last_name,
mobile: values.mobile,
city: values.city,
role: getRoleFromUrl(),
};
const service = isEdit
? slaughterEditRepresentativeService
: slaughterSubmitRepresentativeService;
dispatch(service(submitData)).then((r) => {
if (r.payload.error) {
openNotif({
vertical: "top",
horizontal: "center",
msg: r.payload.error,
severity: "error",
});
} else {
openNotif({
vertical: "top",
horizontal: "center",
msg: "عملیات با موفقیت انجام شد.",
severity: "success",
});
if (updateTable) {
updateTable();
}
dispatch(CLOSE_MODAL());
}
});
},
});
return (
<form onSubmit={formik.handleSubmit}>
<Grid container gap={SPACING.SMALL} p={2}>
<Grid item xs={12}>
<TextField
label="نام"
variant="outlined"
fullWidth
id="first_name"
name="first_name"
value={formik.values.first_name}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
error={
formik.touched.first_name && Boolean(formik.errors.first_name)
}
helperText={formik.touched.first_name && formik.errors.first_name}
/>
</Grid>
<Grid item xs={12}>
<TextField
label="نام خانوادگی"
variant="outlined"
fullWidth
id="last_name"
name="last_name"
value={formik.values.last_name}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
error={formik.touched.last_name && Boolean(formik.errors.last_name)}
helperText={formik.touched.last_name && formik.errors.last_name}
/>
</Grid>
<Grid item xs={12}>
<TextField
label="شماره همراه"
variant="outlined"
fullWidth
id="mobile"
name="mobile"
value={formik.values.mobile}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
error={formik.touched.mobile && Boolean(formik.errors.mobile)}
helperText={formik.touched.mobile && formik.errors.mobile}
inputProps={{ maxLength: 11 }}
/>
</Grid>
<Grid item xs={12}>
<TextField
label="شهر"
variant="outlined"
fullWidth
id="city"
name="city"
value={formik.values.city}
onChange={formik.handleChange}
onBlur={formik.handleBlur}
error={formik.touched.city && Boolean(formik.errors.city)}
helperText={formik.touched.city && formik.errors.city}
/>
</Grid>
<Grid item xs={12} mt={2}>
<Button
type="submit"
variant="contained"
color="primary"
fullWidth
disabled={!formik.isValid}
>
{isEdit ? "ویرایش" : "ثبت"}
</Button>
</Grid>
</Grid>
</form>
);
};

View File

@@ -0,0 +1,133 @@
import React, { useContext, useState } from "react";
import {
Button,
TextField,
Typography,
Checkbox,
FormControlLabel,
} from "@mui/material";
import { useDispatch } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { CLOSE_MODAL } from "../../../../lib/redux/slices/appSlice";
import { AppContext } from "../../../../contexts/AppContext";
import { slaughterEditRepresentativeService } from "../../services/slaughter-submit-delegate";
export const SlaughterManageDelegatesLimitationForm = ({
item,
updateTable,
}) => {
const dispatch = useDispatch();
const [openNotif] = useContext(AppContext);
const [hasLimitation, setHasLimitation] = useState(item?.limitation || false);
const [governmentalValue, setGovernmentalValue] = useState(
item?.governmentalLimitationWeight || 0
);
const [freeValue, setFreeValue] = useState(item?.freeLimitationWeight || 0);
const handleSubmit = (e) => {
e.preventDefault();
const submitData = {
key: item?.key,
limitation: hasLimitation,
governmental_limitation_weight: hasLimitation
? Number(governmentalValue)
: 0,
free_limitation_weight: hasLimitation ? Number(freeValue) : 0,
};
dispatch(slaughterEditRepresentativeService(submitData)).then((r) => {
if (r.payload?.error) {
openNotif({
vertical: "top",
horizontal: "center",
msg: r.payload.error,
severity: "error",
});
} else {
openNotif({
vertical: "top",
horizontal: "center",
msg: "عملیات با موفقیت انجام شد.",
severity: "success",
});
if (updateTable) {
updateTable();
}
dispatch(CLOSE_MODAL());
}
});
};
return (
<form onSubmit={handleSubmit}>
<Grid container gap={SPACING.SMALL} p={2}>
<Grid container item xs={12} alignItems="center" gap={1}>
<Typography variant="body2" color="text.secondary">
اطلاعات نماینده:
</Typography>
<Typography variant="h6" mb={0.75}>
{item?.firstName} {item?.lastName}
</Typography>
</Grid>
<Grid item xs={12} mb={1}>
<FormControlLabel
control={
<Checkbox
checked={hasLimitation}
onChange={(e) => setHasLimitation(e.target.checked)}
color="primary"
/>
}
label="محدودیت فروش روزانه"
/>
</Grid>
{hasLimitation && (
<>
<Grid item xs={12}>
<TextField
label="حداکثر فروش دولتی (کیلوگرم)"
variant="outlined"
fullWidth
type="number"
value={governmentalValue}
onChange={(e) => setGovernmentalValue(e.target.value)}
inputProps={{ min: 0 }}
/>
</Grid>
<Grid item xs={12}>
<TextField
label="حداکثر فروش آزاد (کیلوگرم)"
variant="outlined"
fullWidth
type="number"
value={freeValue}
onChange={(e) => setFreeValue(e.target.value)}
inputProps={{ min: 0 }}
/>
</Grid>
</>
)}
<Grid item xs={12} mt={2}>
<Button
type="submit"
variant="contained"
color="primary"
fullWidth
disabled={
hasLimitation && governmentalValue === 0 && freeValue === 0
}
>
ثبت
</Button>
</Grid>
</Grid>
</form>
);
};

View File

@@ -0,0 +1,114 @@
import { IconButton, Popover, Typography, Button, Box } from "@mui/material";
import React, { useState } from "react";
import EditIcon from "@mui/icons-material/Edit";
import BlockIcon from "@mui/icons-material/Block";
import TuneIcon from "@mui/icons-material/Tune";
import { useDispatch } from "react-redux";
import { OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { SlaughterManageDelegatesForm } from "./SlaughterManageDelegatesForm";
import { SlaughterManageDelegatesLimitationForm } from "./SlaughterManageDelegatesLimitationForm";
export const SlaughterManageDelegatesOperations = ({ item, updateTable }) => {
const [anchorEl, setAnchorEl] = useState(null);
const dispatch = useDispatch();
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const open = Boolean(anchorEl);
const id = open ? "popover" : undefined;
return (
<div>
<IconButton
aria-describedby={id}
variant="contained"
color="primary"
onClick={handleClick}
size="small"
>
<TuneIcon fontSize="small" />
</IconButton>
<Popover
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "left",
}}
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
>
<Box
style={{
padding: "10px",
display: "flex",
flexDirection: "column",
gap: "8px",
}}
>
<Button
color="primary"
size="small"
onClick={() => {
handleClose();
dispatch(
OPEN_MODAL({
title: "ویرایش نماینده",
content: (
<SlaughterManageDelegatesForm
item={item}
updateTable={updateTable}
isEdit
/>
),
size: 300,
})
);
}}
startIcon={<EditIcon fontSize="small" />}
sx={{ textTransform: "none", userSelect: "text" }}
>
<Typography variant="body2" sx={{ userSelect: "text" }}>
ویرایش
</Typography>
</Button>
<Button
color="primary"
size="small"
onClick={() => {
handleClose();
dispatch(
OPEN_MODAL({
title: "تنظیم محدودیت فروش",
content: (
<SlaughterManageDelegatesLimitationForm
item={item}
updateTable={updateTable}
/>
),
size: 400,
})
);
}}
startIcon={<BlockIcon fontSize="small" />}
sx={{ textTransform: "none", userSelect: "text" }}
>
<Typography variant="body2" sx={{ userSelect: "text" }}>
تنظیم محدودیت
</Typography>
</Button>
</Box>
</Popover>
</div>
);
};