2024-03-26 15:41:22 +00:00
|
|
|
|
import { useState } from "react";
|
2024-03-03 13:30:57 +00:00
|
|
|
|
import { Box, Typography, useTheme } from "@mui/material";
|
|
|
|
|
import { DataGrid, GridLoadingOverlay, GridToolbar } from "@mui/x-data-grid";
|
2024-02-22 14:12:50 +00:00
|
|
|
|
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
|
2024-03-03 07:57:12 +00:00
|
|
|
|
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
|
2024-03-03 13:30:57 +00:00
|
|
|
|
import { usePromocodes } from "@root/api/promocode/swr";
|
|
|
|
|
import { fadeIn } from "@root/utils/style/keyframes";
|
2024-02-22 14:12:50 +00:00
|
|
|
|
import { CreatePromocodeForm } from "./CreatePromocodeForm";
|
2024-03-03 13:30:57 +00:00
|
|
|
|
import { usePromocodeGridColDef } from "./usePromocodeGridColDef";
|
2024-03-26 15:41:22 +00:00
|
|
|
|
import { StatisticsModal } from "./StatisticsModal";
|
2024-03-30 20:19:53 +00:00
|
|
|
|
import DeleteModal from "./DeleteModal";
|
2024-02-22 14:12:50 +00:00
|
|
|
|
|
2024-03-03 07:57:12 +00:00
|
|
|
|
export const PromocodeManagement = () => {
|
2024-03-26 15:41:22 +00:00
|
|
|
|
const theme = useTheme();
|
2024-04-02 13:21:17 +00:00
|
|
|
|
|
|
|
|
|
const [deleteModal, setDeleteModal] = useState<string>("");
|
|
|
|
|
const deleteModalHC = (id: string) => setDeleteModal(id);
|
2024-03-30 20:19:53 +00:00
|
|
|
|
|
2024-03-26 15:41:22 +00:00
|
|
|
|
const [showStatisticsModalId, setShowStatisticsModalId] =
|
|
|
|
|
useState<string>("");
|
|
|
|
|
const [page, setPage] = useState<number>(0);
|
2024-03-29 20:27:07 +00:00
|
|
|
|
const [to, setTo] = useState(0);
|
|
|
|
|
const [from, setFrom] = useState(0);
|
2024-03-26 15:41:22 +00:00
|
|
|
|
const [pageSize, setPageSize] = useState<number>(10);
|
|
|
|
|
const {
|
|
|
|
|
data,
|
|
|
|
|
error,
|
|
|
|
|
isValidating,
|
|
|
|
|
promocodesCount,
|
2024-04-02 14:56:23 +00:00
|
|
|
|
promocodeStatistics,
|
2024-03-26 15:41:22 +00:00
|
|
|
|
deletePromocode,
|
|
|
|
|
createPromocode,
|
2024-04-02 14:56:23 +00:00
|
|
|
|
createFastLink,
|
2024-03-29 20:27:07 +00:00
|
|
|
|
} = usePromocodes(page, pageSize, showStatisticsModalId, to, from);
|
2024-03-26 15:41:22 +00:00
|
|
|
|
const columns = usePromocodeGridColDef(
|
|
|
|
|
setShowStatisticsModalId,
|
2024-03-30 20:19:53 +00:00
|
|
|
|
deleteModalHC
|
2024-03-26 15:41:22 +00:00
|
|
|
|
);
|
|
|
|
|
if (error) return <Typography>Ошибка загрузки промокодов</Typography>;
|
2024-03-03 07:57:12 +00:00
|
|
|
|
|
2024-03-26 15:41:22 +00:00
|
|
|
|
return (
|
|
|
|
|
<LocalizationProvider dateAdapter={AdapterDayjs}>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="subtitle1"
|
|
|
|
|
sx={{
|
|
|
|
|
width: "90%",
|
|
|
|
|
height: "60px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
textTransform: "uppercase",
|
|
|
|
|
color: theme.palette.secondary.main,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Создание промокода
|
|
|
|
|
</Typography>
|
|
|
|
|
<CreatePromocodeForm createPromocode={createPromocode} />
|
|
|
|
|
<Box style={{ width: "80%", marginTop: "55px" }}>
|
|
|
|
|
<DataGrid
|
|
|
|
|
disableSelectionOnClick={true}
|
|
|
|
|
rows={data?.items ?? []}
|
|
|
|
|
columns={columns}
|
|
|
|
|
sx={{
|
|
|
|
|
color: theme.palette.secondary.main,
|
|
|
|
|
"& .MuiDataGrid-iconSeparator": { display: "none" },
|
|
|
|
|
"& .css-levciy-MuiTablePagination-displayedRows": {
|
|
|
|
|
color: theme.palette.secondary.main,
|
|
|
|
|
},
|
|
|
|
|
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main },
|
|
|
|
|
"& .MuiTablePagination-selectLabel": {
|
|
|
|
|
color: theme.palette.secondary.main,
|
|
|
|
|
},
|
|
|
|
|
"& .MuiInputBase-root": { color: theme.palette.secondary.main },
|
|
|
|
|
"& .MuiButton-text": { color: theme.palette.secondary.main },
|
|
|
|
|
"& .MuiDataGrid-overlay": {
|
|
|
|
|
backgroundColor: "rgba(255, 255, 255, 0.1)",
|
|
|
|
|
animation: `${fadeIn} 0.5s ease-out`,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
components={{
|
|
|
|
|
Toolbar: GridToolbar,
|
|
|
|
|
LoadingOverlay: GridLoadingOverlay,
|
|
|
|
|
}}
|
|
|
|
|
loading={isValidating}
|
|
|
|
|
paginationMode="server"
|
|
|
|
|
page={page}
|
|
|
|
|
onPageChange={setPage}
|
|
|
|
|
rowCount={promocodesCount}
|
|
|
|
|
pageSize={pageSize}
|
|
|
|
|
onPageSizeChange={setPageSize}
|
|
|
|
|
rowsPerPageOptions={[10, 25, 50, 100]}
|
|
|
|
|
autoHeight
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<StatisticsModal
|
|
|
|
|
id={showStatisticsModalId}
|
|
|
|
|
setId={setShowStatisticsModalId}
|
2024-04-02 14:56:23 +00:00
|
|
|
|
promocodeStatistics={promocodeStatistics}
|
2024-03-29 20:27:07 +00:00
|
|
|
|
to={to}
|
|
|
|
|
setTo={setTo}
|
|
|
|
|
from={from}
|
|
|
|
|
setFrom={setFrom}
|
2024-04-02 13:21:17 +00:00
|
|
|
|
promocodes={data?.items ?? []}
|
2024-04-02 14:56:23 +00:00
|
|
|
|
createFastLink={createFastLink}
|
2024-04-02 13:21:17 +00:00
|
|
|
|
/>
|
|
|
|
|
<DeleteModal
|
|
|
|
|
id={deleteModal}
|
|
|
|
|
setModal={setDeleteModal}
|
|
|
|
|
deletePromocode={deletePromocode}
|
2024-03-26 15:41:22 +00:00
|
|
|
|
/>
|
|
|
|
|
</LocalizationProvider>
|
|
|
|
|
);
|
2024-03-03 07:57:12 +00:00
|
|
|
|
};
|