adminFront/src/pages/dashboard/Content/Users.tsx

476 lines
13 KiB
TypeScript
Raw Normal View History

2022-09-08 17:21:17 +00:00
import * as React from "react";
import { useState, useEffect } from "react";
2023-08-14 13:08:28 +00:00
import { useNavigate, useParams } from "react-router-dom";
import {
AccordionDetails,
AccordionSummary,
Accordion,
Skeleton,
Radio,
Box,
Typography,
TextField,
Button,
Table,
TableHead,
TableBody,
TableCell,
TableRow,
} from "@mui/material";
import { GridSelectionModel } from "@mui/x-data-grid";
2023-04-20 15:42:18 +00:00
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import ConditionalRender from "@root/pages/Setting/ConditionalRender";
2023-08-14 11:58:12 +00:00
import ModalUser from "@root/pages/dashboard/ModalUser";
import ServiceUsersDG from "./ServiceUsersDG";
2024-03-07 12:44:44 +00:00
import { useUsers, useManagers, useAdmins } from "@root/api/user/swr";
2023-09-01 13:17:24 +00:00
import { getRoles } from "@root/api/privilegies";
import { getRoles_mock, TMockData } from "../../../api/roles";
2023-04-20 15:42:18 +00:00
import theme from "../../../theme";
2024-03-07 12:44:44 +00:00
type Pages = {
adminPage: number;
managerPage: number;
userPage: number;
};
type PagesSize = {
adminPageSize: number;
managerPageSize: number;
userPageSize: number;
};
2023-07-12 10:27:21 +00:00
2023-04-20 15:42:18 +00:00
const Users: React.FC = () => {
const radioboxes = ["admin", "manager", "user"];
2022-09-26 12:35:56 +00:00
const [selectedValue, setSelectedValue] = React.useState("admin");
2022-09-09 13:54:49 +00:00
2022-09-14 10:24:02 +00:00
const navigate = useNavigate();
2024-03-07 12:44:44 +00:00
const [mockData, setMockData] = React.useState<TMockData>([]);
2023-04-20 15:42:18 +00:00
2022-09-24 15:37:24 +00:00
const handleChangeData = () => {
2022-09-26 12:35:56 +00:00
getRoles_mock().then((mockdata) => {
2024-03-07 12:44:44 +00:00
setMockData(mockdata);
2023-09-08 10:17:27 +00:00
setAccordionText(mockdata[0].desc || "");
2023-04-20 15:42:18 +00:00
});
2022-09-24 15:37:24 +00:00
};
2022-09-27 20:42:56 +00:00
const [accordionState, setAccordionState] = React.useState<boolean>(true);
const [accordionText, setAccordionText] = React.useState<string>("");
2023-09-08 10:17:27 +00:00
const handleChange = (value: string) => {
setSelectedValue(value);
2024-03-07 12:44:44 +00:00
setAccordionText(mockData.find(({ name }) => name === value)?.desc || "");
2023-09-08 10:17:27 +00:00
if (selectedValue === "manager") {
}
2023-04-20 15:42:18 +00:00
};
2022-09-27 20:42:56 +00:00
const handleToggleAccordion = () => {
2023-04-20 15:42:18 +00:00
setAccordionState(!accordionState);
};
2022-09-27 20:42:56 +00:00
2023-04-20 15:42:18 +00:00
const [roles, setRoles] = React.useState<TMockData>([]);
2024-03-07 12:44:44 +00:00
const [page, setPage] = useState<Pages>({
adminPage: 0,
managerPage: 0,
userPage: 0,
});
const [pageSize, setPageSize] = useState<PagesSize>({
adminPageSize: 10,
managerPageSize: 10,
userPageSize: 10,
});
2023-08-14 11:58:12 +00:00
const [openUserModal, setOpenUserModal] = useState<boolean>(false);
const [activeUserId, setActiveUserId] = useState<string>("");
2023-04-20 15:42:18 +00:00
2023-08-14 13:08:28 +00:00
const { userId } = useParams();
2024-03-07 12:44:44 +00:00
const { data: adminData, adminPages } = useAdmins(
page.adminPage + 1,
pageSize.adminPageSize
);
const { data: managerData, managerPages } = useManagers(
page.managerPage + 1,
pageSize.managerPageSize
);
const { data: userData, userPagesCount } = useUsers(
page.userPage + 1,
pageSize.userPageSize
);
2023-08-14 13:08:28 +00:00
2023-09-08 10:17:27 +00:00
useEffect(() => {
handleChangeData();
}, []);
2023-08-14 13:08:28 +00:00
useEffect(() => {
if (userId) {
setActiveUserId(userId);
setOpenUserModal(true);
return;
}
setActiveUserId("");
setOpenUserModal(false);
}, [userId]);
useEffect(() => {
2023-09-01 13:17:24 +00:00
getRoles().then(([rolesResponse]) => {
if (rolesResponse) {
setRoles(rolesResponse);
}
});
}, [selectedValue]);
2023-04-20 15:42:18 +00:00
2024-02-21 09:00:40 +00:00
const [selectedTariffs, setSelectedTariffs] = useState<GridSelectionModel>(
[]
);
2022-09-08 17:21:17 +00:00
return (
<React.Fragment>
2024-04-03 21:53:48 +00:00
{/* <Button
2023-04-20 15:42:18 +00:00
variant="text"
onClick={() => navigate("/modalAdmin")}
sx={{
width: "84%",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
marginBottom: "35px",
border: "2px solid",
fontWeight: "normal",
borderColor: theme.palette.golden.main,
color: theme.palette.secondary.main,
"&:hover": {
backgroundColor: theme.palette.menu.main,
},
}}
>
ИНФОРМАЦИЯ О ПРОЕКТЕ
2024-04-03 21:53:48 +00:00
</Button> */}
2022-09-08 17:21:17 +00:00
2023-04-20 15:42:18 +00:00
<Accordion
sx={{
width: "84%",
2023-04-20 15:42:18 +00:00
backgroundColor: theme.palette.content.main,
}}
expanded={accordionState}
>
<AccordionSummary
2023-09-08 10:17:27 +00:00
sx={{ display: "flex" }}
onClick={handleToggleAccordion}
2024-02-21 09:00:40 +00:00
expandIcon={
<ExpandMoreIcon sx={{ color: theme.palette.secondary.main }} />
}
2023-04-20 15:42:18 +00:00
aria-controls="panel1a-content"
id="panel1a-header"
>
<Typography
sx={{
width: "100%",
color: theme.palette.secondary.main,
}}
2022-09-27 20:42:56 +00:00
>
2023-04-20 15:42:18 +00:00
{accordionText}
</Typography>
</AccordionSummary>
2024-03-10 00:10:40 +00:00
<AccordionDetails sx={{overflowX: "auto"}}>
2023-04-20 15:42:18 +00:00
<Table
sx={{
width: "100%",
border: "2px solid",
borderColor: theme.palette.secondary.main,
}}
aria-label="simple table"
>
<TableHead>
<TableRow
sx={{
borderBottom: "2px solid",
borderColor: theme.palette.grayLight.main,
height: "100px",
}}
>
<TableCell>
<Typography
variant="h4"
sx={{
color: theme.palette.secondary.main,
}}
>
Имя
</Typography>
</TableCell>
<TableCell>
<Typography
variant="h4"
sx={{
color: theme.palette.secondary.main,
}}
>
Описание
</Typography>
</TableCell>
<TableCell>
<Typography
variant="h4"
sx={{
color: theme.palette.secondary.main,
}}
>
Отобразить
</Typography>
</TableCell>
</TableRow>
</TableHead>
2022-09-09 13:54:49 +00:00
2023-04-20 15:42:18 +00:00
<TableBody>
2024-03-07 12:44:44 +00:00
{mockData.length ? (
mockData.map(function (item, index) {
2023-04-20 15:42:18 +00:00
return (
<TableRow
sx={{
borderTop: "2px solid",
borderColor: theme.palette.grayLight.main,
height: "100px",
cursor: "pointer",
}}
key={item.key}
2023-09-08 10:17:27 +00:00
onClick={() => handleChange(radioboxes[index])}
2023-04-20 15:42:18 +00:00
>
2022-09-26 12:35:56 +00:00
<TableCell>
2023-04-20 15:42:18 +00:00
<Typography
sx={{
color: theme.palette.secondary.main,
}}
>
{item.name}
2022-09-26 12:35:56 +00:00
</Typography>
</TableCell>
<TableCell>
2023-04-20 15:42:18 +00:00
<Typography
sx={{
color: theme.palette.secondary.main,
}}
>
{item.desc}
2022-09-26 12:35:56 +00:00
</Typography>
</TableCell>
<TableCell>
2023-04-20 15:42:18 +00:00
<Radio
checked={selectedValue === radioboxes[index]}
value={radioboxes[index]}
2022-09-26 12:35:56 +00:00
sx={{
2023-04-20 15:42:18 +00:00
"&, &.Mui-checked": {
color: theme.palette.secondary.main,
},
}}
/>
2022-09-26 12:35:56 +00:00
</TableCell>
</TableRow>
);
})
2023-04-20 15:42:18 +00:00
) : (
<TableRow
sx={{
borderTop: "2px solid",
borderColor: theme.palette.grayLight.main,
height: "100px",
}}
>
2022-09-27 20:42:56 +00:00
<TableCell colSpan={3}>
2023-04-20 15:42:18 +00:00
<Skeleton
variant="rectangular"
2022-09-27 20:42:56 +00:00
sx={{
width: "100%",
minWidth: "100%",
minHeight: "200px",
marginTop: "15px",
2023-04-20 15:42:18 +00:00
marginBottom: "15px",
2022-09-27 20:42:56 +00:00
}}
/>
</TableCell>
</TableRow>
2023-04-20 15:42:18 +00:00
)}
</TableBody>
</Table>
2022-09-27 20:42:56 +00:00
</AccordionDetails>
</Accordion>
2023-04-20 15:42:18 +00:00
<Box
sx={{
width: "90%",
marginTop: "35px",
}}
>
2022-09-09 13:54:49 +00:00
<Box sx={{ display: "flex" }}>
<TextField
2023-04-20 15:42:18 +00:00
id="standard-basic"
label="id"
variant="filled"
color="secondary"
sx={{
width: "80%",
2022-09-09 13:54:49 +00:00
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
borderBottom: "1px solid",
borderColor: theme.palette.grayLight.main,
2023-04-20 15:42:18 +00:00
},
}}
2022-09-09 13:54:49 +00:00
InputLabelProps={{
style: {
2023-04-20 15:42:18 +00:00
color: theme.palette.secondary.main,
},
}}
2022-09-09 13:54:49 +00:00
/>
<Box
2022-09-09 13:54:49 +00:00
sx={{
2023-04-20 15:42:18 +00:00
display: "flex",
justifyContent: "center",
maxWidth: "317px",
width: "100%",
}}
>
<Button
2023-04-20 15:42:18 +00:00
variant="text"
sx={{
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
// width: "20%",
"&:hover": {
2023-04-20 15:42:18 +00:00
backgroundColor: theme.palette.content.main,
},
}}
>
НАЙТИ
</Button>
</Box>
2022-09-09 13:54:49 +00:00
</Box>
<Box sx={{ display: "flex" }}>
<TextField
2023-04-20 15:42:18 +00:00
id="standard-basic"
label="mail"
variant="filled"
color="secondary"
sx={{
width: "80%",
2022-09-09 13:54:49 +00:00
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
borderBottom: "1px solid",
borderColor: theme.palette.grayLight.main,
2023-04-20 15:42:18 +00:00
},
}}
2022-09-09 13:54:49 +00:00
InputLabelProps={{
style: {
2023-04-20 15:42:18 +00:00
color: theme.palette.secondary.main,
},
}}
2022-09-09 13:54:49 +00:00
/>
<Box
2023-04-20 15:42:18 +00:00
sx={{
display: "flex",
justifyContent: "center",
maxWidth: "317px",
width: "100%",
}}
>
<Button
2023-04-20 15:42:18 +00:00
variant="text"
sx={{
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
width: "20%",
"&:hover": {
2023-04-20 15:42:18 +00:00
backgroundColor: theme.palette.content.main,
},
}}
>
СБРОСИТЬ
</Button>
</Box>
2022-09-09 13:54:49 +00:00
</Box>
</Box>
2023-07-12 10:27:21 +00:00
<Box
component="section"
sx={{
width: "90%",
mt: "45px",
display: "flex",
justifyContent: "center",
}}
>
<ConditionalRender
isLoading={false}
role={selectedValue}
2024-03-07 12:44:44 +00:00
childrenAdmin={
<ServiceUsersDG
users={adminData?.users.length ? adminData.users : []}
page={page.adminPage}
setPage={(adminPage) =>
setPage((pages) => ({ ...pages, adminPage }))
}
pagesCount={adminPages}
pageSize={pageSize.adminPageSize}
handleSelectionChange={setSelectedTariffs}
onPageSizeChange={(adminPageSize) =>
setPageSize((pageSize) => ({ ...pageSize, adminPageSize }))
}
/>
}
2024-02-21 09:00:40 +00:00
childrenManager={
<ServiceUsersDG
2024-03-07 12:44:44 +00:00
users={managerData?.users.length ? managerData.users : []}
page={page.managerPage}
setPage={(managerPage) =>
setPage((pages) => ({ ...pages, managerPage }))
}
pagesCount={managerPages}
pageSize={pageSize.managerPageSize}
2024-02-21 09:00:40 +00:00
handleSelectionChange={setSelectedTariffs}
2024-03-07 12:44:44 +00:00
onPageSizeChange={(managerPageSize) =>
setPageSize((pageSize) => ({ ...pageSize, managerPageSize }))
}
2024-02-21 09:00:40 +00:00
/>
}
childrenUser={
<ServiceUsersDG
2024-03-07 12:44:44 +00:00
users={userData?.users.length ? userData.users : []}
page={page.userPage}
setPage={(userPage) =>
setPage((pages) => ({ ...pages, userPage }))
}
pagesCount={userPagesCount}
pageSize={pageSize.userPageSize}
2024-02-21 09:00:40 +00:00
handleSelectionChange={setSelectedTariffs}
2024-03-07 12:44:44 +00:00
onPageSizeChange={(userPageSize) =>
setPageSize((pageSize) => ({ ...pageSize, userPageSize }))
}
2024-02-21 09:00:40 +00:00
/>
}
/>
</Box>
2024-02-21 09:00:40 +00:00
<ModalUser
open={openUserModal}
userId={activeUserId}
onClose={() => {
setOpenUserModal(false);
navigate(-1);
}}
/>
2022-09-08 17:21:17 +00:00
</React.Fragment>
);
2023-04-20 15:42:18 +00:00
};
2022-09-08 17:21:17 +00:00
2023-04-20 15:42:18 +00:00
export default Users;