adminFront/src/pages/dashboard/Content/Discounts/index.tsx

698 lines
21 KiB
TypeScript
Raw Normal View History

2022-10-20 03:05:10 +00:00
import * as React from "react";
import { Box, Typography, TextField, Checkbox, Button } from "@mui/material";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { DesktopDatePicker } from "@mui/x-date-pickers/DesktopDatePicker";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableRow from "@mui/material/TableRow";
import TableContainer from "@mui/material/TableContainer";
import Paper from "@mui/material/Paper";
import { DataGrid, GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid";
import MenuItem from "@mui/material/MenuItem";
import Select, { SelectChangeEvent } from "@mui/material/Select";
import theme from "../../../../theme";
2023-02-18 14:07:11 +00:00
import { styled } from "@mui/material/styles";
import { Discount } from "../../../../model/cart";
import { useDiscountStore } from "../../../../stores/discounts";
const BoxButton = styled('div')(({ theme }) => ({
[theme.breakpoints.down(400)]: {
justifyContent: 'center'
},
}));
2022-10-20 03:05:10 +00:00
const columns: GridColDef[] = [
2023-02-18 14:07:11 +00:00
{
field: "id",
headerName: "ID",
2022-10-20 03:05:10 +00:00
width: 30,
sortable: false,
},
{
field: "name",
headerName: "Название скидки",
width: 200,
sortable: false,
},
{
field: "endless",
headerName: "Бесконечная",
width: 120,
sortable: false,
},
{
field: "from",
headerName: "От",
width: 120,
sortable: false,
},
{
field: "dueTo",
headerName: "До",
width: 120,
sortable: false,
},
{
field: "privileges",
headerName: "Привилегии",
width: 210,
sortable: false,
},
{
field: "active",
headerName: "Активна",
width: 100,
sortable: false,
},
{
field: "basketMore",
headerName: "Корзина больше",
width: 140,
sortable: false,
2022-10-20 03:05:10 +00:00
}
,
{
field: "toTime",
headerName: "На время",
width: 140,
sortable: false,
}
,
{
field: "toCapacity",
headerName: "На объем",
width: 140,
sortable: false,
}
2022-10-20 03:05:10 +00:00
];
2023-02-18 14:07:11 +00:00
const Discounts: React.FC = () => {
2022-10-20 03:05:10 +00:00
const [checkboxState, setCheckboxState] = React.useState<boolean>(false);
2023-02-18 14:07:11 +00:00
const toggleCheckbox = () => { setCheckboxState(!checkboxState); };
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const [value1, setValue1] = React.useState<Date>(new Date());
const [value2, setValue2] = React.useState<Date>(new Date());
2022-10-20 03:05:10 +00:00
const [service, setService] = React.useState("Шаблонизатор");
const handleChange = (event: SelectChangeEvent) => {
setService(event.target.value as string);
};
2023-02-18 14:07:11 +00:00
const discountsArray = useDiscountStore(state => state.discountsArray);
const discountsArraySet = useDiscountStore(state => state.setDiscountsArray);
2022-10-22 06:05:13 +00:00
2023-02-18 14:07:11 +00:00
const discountsActiveArray = useDiscountStore(state => state.discountsActiveArray);
const discountsActiveArraySet = useDiscountStore(state => state.setDiscountsActiveArray);
let discountsActiveArrayUpdated: Array<number>;
const findActiveDiscounts = () => {
const actives: Array<number> = [];
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
discountsArray.forEach((item, i) => {
if (item.active == true) { actives.push(i); }
});
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
discountsActiveArrayUpdated = [...actives];
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
if (JSON.stringify(discountsActiveArray) != JSON.stringify(discountsActiveArrayUpdated)) {
discountsActiveArraySet(discountsActiveArrayUpdated);
2022-10-20 03:05:10 +00:00
}
2023-02-18 14:07:11 +00:00
};
2022-10-20 03:05:10 +00:00
findActiveDiscounts();
2022-10-22 06:05:13 +00:00
2023-02-18 14:07:11 +00:00
const discountsArrayConverted = discountsArray.map((item) => {
2022-11-14 11:40:01 +00:00
const basketMorePerc = Math.round(Number(item.basketMore) * 100) + "%";
const toTimePerc = Math.round(Number(item.toTime) * 100) + "%";
const toCapacityPerc = Math.round(Number(item.toCapacity) * 100) + "%";
2023-02-18 14:07:11 +00:00
const dateFrom = item.from ? new Date(Number(item.from)) : "";
const dateDueTo = item.from ? new Date(Number(item.dueTo)) : "";
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const strFrom = dateFrom
2022-10-20 03:05:10 +00:00
? `${dateFrom.getDate()}.${dateFrom.getMonth()}.${dateFrom.getFullYear()}`
2023-02-18 14:07:11 +00:00
: "-";
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const strDueTo = dateDueTo
2022-10-20 03:05:10 +00:00
? `${dateDueTo.getDate()}.${dateDueTo.getMonth()}.${dateDueTo.getFullYear()}`
2023-02-18 14:07:11 +00:00
: "-";
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
if (item.privileges.length) {
const result = item.privileges.reduce((acc, privilege) => {
acc = acc
? `${acc}, ${privilege.good} - ${privilege.discount}%`
: `${privilege.good} - ${Math.round(privilege.discount * 100)}%`;
2022-10-20 03:05:10 +00:00
return acc;
2023-02-18 14:07:11 +00:00
}, "");
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
return {
...item, privileges: result, from: strFrom, dueTo: strDueTo,
basketMore: basketMorePerc, toTime: toTimePerc, toCapacity: toCapacityPerc
};
2022-10-20 03:05:10 +00:00
} else {
2023-02-18 14:07:11 +00:00
return {
...item, from: strFrom, dueTo: strDueTo,
basketMore: basketMorePerc, toTime: toTimePerc, toCapacity: toCapacityPerc
};
2022-10-20 03:05:10 +00:00
}
2023-02-18 14:07:11 +00:00
});
const createDiscount = (name: string,
discount: number,
addedMore: number,
basketMore: number,
toTime: number,
toCapacity: number,) => {
const newDiscount = {
id: new Date().getTime(),
name,
endless: checkboxState,
2022-12-02 09:47:46 +00:00
incomeMore: addedMore,
2023-02-18 14:07:11 +00:00
from: checkboxState ? "" : new Date(value1).getTime() + "",
dueTo: checkboxState ? "" : new Date(value2).getTime() + "",
privileges: [{
good: service,
discount: discount / 100
}],
active: false,
basketMore: basketMore / 100,
toTime: toTime / 100,
toCapacity: toCapacity / 100
2023-02-18 14:07:11 +00:00
} as Discount;
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const discountsArrayUpdated = [...discountsArray, newDiscount];
discountsArraySet(discountsArrayUpdated);
};
2022-10-20 03:05:10 +00:00
const fieldName = React.useRef<HTMLInputElement | null>(null);
const fieldDiscount = React.useRef<HTMLInputElement | null>(null);
2022-11-01 03:50:58 +00:00
const fieldAddedMore = React.useRef<HTMLInputElement | null>(null);
const basketMore = React.useRef<HTMLInputElement | null>(null);
const toTime = React.useRef<HTMLInputElement | null>(null);
const toCapacity = React.useRef<HTMLInputElement | null>(null);
// const cleraAddedMore = () => {
// if (fieldAddedMore.current) {
// fieldAddedMore.current.value = "";
// }
// }
2022-10-20 03:05:10 +00:00
const checkFields = () => {
2023-02-18 14:07:11 +00:00
if (fieldName.current != null
&& fieldDiscount.current != null
&& fieldAddedMore.current != null
&& basketMore.current != null
&& toTime.current != null
2023-02-18 14:07:11 +00:00
&& toCapacity.current != null) {
2022-11-01 03:50:58 +00:00
2023-02-18 14:07:11 +00:00
createDiscount(fieldName.current.value,
Number(fieldDiscount.current.value),
Number(fieldAddedMore.current.value),
Number(basketMore.current.value),
Number(toTime.current.value),
2023-02-18 14:07:11 +00:00
Number(toCapacity.current.value));
2022-10-20 03:05:10 +00:00
}
2023-02-18 14:07:11 +00:00
};
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const discountsSelectedRowsData = useDiscountStore(state => state.discountsSelectedRowsData);
const discountsSelectedRowsDataSet = useDiscountStore(state => state.setDiscountsSelectedRowsData);
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const onRowsSelectionHandler = (ids: GridSelectionModel) => {
const result: Array<Discount> = [];
ids.forEach((id) => discountsArray.forEach((row) => {
if (row.id === id) result.push(row);
}));
discountsSelectedRowsDataSet([...result]);
2022-10-20 03:05:10 +00:00
};
2023-02-18 14:07:11 +00:00
const activation = (value: boolean) => {
discountsArray.forEach((item, i) => {
discountsSelectedRowsData.forEach((selected) => {
if (item.id == selected.id) {
if (value) {
discountsArray[i].active = true;
2022-10-20 03:05:10 +00:00
} else {
2023-02-18 14:07:11 +00:00
discountsArray[i].active = false;
2022-10-20 03:05:10 +00:00
}
}
2023-02-18 14:07:11 +00:00
});
});
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
discountsArraySet(discountsArray);
};
2022-10-20 03:05:10 +00:00
2023-02-18 14:07:11 +00:00
const PositiveInput = (event: any) => {
const numberInput = parseInt(event.target.value);
2023-02-18 14:07:11 +00:00
if (isNaN(numberInput) || numberInput < 0) { event.target.value = '0'; }
};
2022-10-20 03:05:10 +00:00
return (
<React.Fragment>
2023-02-18 14:07:11 +00:00
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Typography
variant="subtitle1"
2022-10-20 03:05:10 +00:00
sx={{
width: "90%",
height: "60px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}}>
2022-10-20 03:05:10 +00:00
СКИДКИ
</Typography>
<Box
2023-02-18 14:07:11 +00:00
sx={{
display: "flex",
flexDirection: "column",
justifyContent: "left",
alignItems: "left",
marginTop: "15px",
}}
>
2022-10-20 03:05:10 +00:00
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"Название"}
variant="filled"
color="secondary"
2022-10-20 03:05:10 +00:00
sx={{
height: "30px",
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
2022-10-20 03:05:10 +00:00
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={fieldName}
2022-10-20 03:05:10 +00:00
/>
2023-02-18 14:07:11 +00:00
<Typography
variant="h4"
2022-10-20 03:05:10 +00:00
sx={{
width: "90%",
height: "40px",
fontWeight: "normal",
color: theme.palette.grayDisabled.main,
marginTop: "75px",
paddingLeft: '10px',
2023-02-18 14:07:11 +00:00
}}>
2022-10-20 03:05:10 +00:00
Условия:
</Typography>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={service}
label="Age"
onChange={handleChange}
2023-02-18 14:07:11 +00:00
sx={{
2022-10-20 03:05:10 +00:00
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
borderColor: theme.palette.secondary.main
},
".MuiSvgIcon-root ": {
fill: theme.palette.secondary.main,
}
}}
>
<MenuItem value={"Шаблонизатор"}>Шаблонизатор</MenuItem>
<MenuItem value={"Опросник"}>Опросник</MenuItem>
<MenuItem value={"Аналитика сокращателя"}>Аналитика сокращателя</MenuItem>
<MenuItem value={"АБ тесты"}>АБ тесты</MenuItem>
</Select>
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"Процент скидки"}
variant="filled"
color="secondary"
2022-10-20 03:05:10 +00:00
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
2022-10-20 03:05:10 +00:00
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={fieldDiscount}
2022-10-20 03:05:10 +00:00
/>
2022-11-01 03:50:58 +00:00
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"Внесено больше"}
variant="filled"
color="secondary"
type="number"
2022-11-01 03:50:58 +00:00
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
2022-11-01 03:50:58 +00:00
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={fieldAddedMore}
onChange={PositiveInput}
2022-11-01 03:50:58 +00:00
/>
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"Объем в корзине"}
variant="filled"
color="secondary"
type="number"
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={basketMore}
onChange={PositiveInput}
/>
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"На время"}
variant="filled"
color="secondary"
type="number"
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={toTime}
onChange={PositiveInput}
/>
<TextField
2023-02-18 14:07:11 +00:00
id="standard-basic"
label={"На объем"}
variant="filled"
color="secondary"
type="number"
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
2023-02-18 14:07:11 +00:00
}
}}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
2023-02-18 14:07:11 +00:00
}
}}
inputRef={toCapacity}
onChange={PositiveInput}
/>
2023-02-18 14:07:11 +00:00
<TableContainer component={Paper} sx={{
width: "100%",
2022-10-20 03:05:10 +00:00
marginTop: "35px",
backgroundColor: theme.palette.content.main
}}>
<Table aria-label="simple table">
2022-10-20 03:05:10 +00:00
<TableBody>
<TableRow sx={{ border: "1px solid white" }} >
<TableCell component="th" scope="row" sx={{ color: theme.palette.secondary.main }}>
Работает, если заплатите 100500 денег
</TableCell>
</TableRow>
<TableRow sx={{ border: "1px solid white" }} >
<TableCell component="th" scope="row" sx={{ color: theme.palette.secondary.main }}>
Вы должны будете продать душу дьяволу
</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
2023-02-18 14:07:11 +00:00
<Typography
variant="h4"
2022-10-20 03:05:10 +00:00
sx={{
width: "90%",
height: "40px",
fontWeight: "normal",
color: theme.palette.grayDisabled.main,
marginTop: "55px"
2023-02-18 14:07:11 +00:00
}}>
2022-10-20 03:05:10 +00:00
Дата действия:
</Typography>
2023-02-18 14:07:11 +00:00
<Box
2023-02-18 14:07:11 +00:00
sx={{
width: "100%",
display: "flex",
flexWrap: 'wrap'
}}
>
2022-10-20 03:05:10 +00:00
<Typography sx={{
width: "35px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "left",
}}>С</Typography>
<DesktopDatePicker
inputFormat="DD/MM/YYYY"
2023-02-18 14:07:11 +00:00
value={value1}
onChange={(e) => { if (e) { setValue1(e); } }}
2022-10-20 03:05:10 +00:00
renderInput={(params) => <TextField {...params} />}
2023-02-18 14:07:11 +00:00
InputProps={{
sx: {
height: "40px",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main }
}
}}
2022-10-20 03:05:10 +00:00
/>
<Typography sx={{
width: "65px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
}}>по</Typography>
<DesktopDatePicker
inputFormat="DD/MM/YYYY"
2023-02-18 14:07:11 +00:00
value={value2}
onChange={(e) => { if (e) { setValue2(e); } }}
2022-10-20 03:05:10 +00:00
renderInput={(params) => <TextField {...params} />}
2023-02-18 14:07:11 +00:00
InputProps={{
sx: {
height: "40px",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main }
}
}}
2022-10-20 03:05:10 +00:00
/>
</Box>
<Box sx={{
display: "flex",
width: "90%",
marginTop: theme.spacing(2),
}}>
2023-02-18 14:07:11 +00:00
<Box sx={{
2022-10-20 03:05:10 +00:00
width: "20px",
height: "42px",
display: "flex",
flexDirection: "column",
justifyContent: "left",
alignItems: "left",
marginRight: theme.spacing(1)
}}>
2023-02-18 14:07:11 +00:00
<Checkbox sx={{
2022-10-20 03:05:10 +00:00
color: theme.palette.secondary.main,
"&.Mui-checked": {
color: theme.palette.secondary.main,
},
2023-02-18 14:07:11 +00:00
}} onClick={() => toggleCheckbox()} />
2022-10-20 03:05:10 +00:00
</Box>
2023-02-18 14:07:11 +00:00
<Box sx={{
2022-10-20 03:05:10 +00:00
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center"
}}>
Бессрочно
</Box>
</Box>
2023-02-18 14:07:11 +00:00
<Box sx={{
2022-10-20 03:05:10 +00:00
width: "90%",
marginTop: "55px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center"
}}>
2023-02-18 14:07:11 +00:00
<Button
variant="contained"
2022-10-20 03:05:10 +00:00
sx={{
backgroundColor: theme.palette.menu.main,
height: "52px",
fontWeight: "normal",
fontSize: "17px",
"&:hover": {
backgroundColor: theme.palette.grayMedium.main
}
}}
2023-02-18 14:07:11 +00:00
onClick={() => checkFields()} >
Cоздать
2022-10-20 03:05:10 +00:00
</Button>
</Box>
2023-02-18 14:07:11 +00:00
2022-10-20 03:05:10 +00:00
</Box>
<Box style={{ width: "80%", marginTop: "55px" }}>
<Box style={{ height: 400 }}>
2023-02-18 14:07:11 +00:00
<DataGrid
checkboxSelection={true}
rows={discountsArrayConverted}
columns={columns}
2022-10-20 03:05:10 +00:00
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
},
}}
components={{ Toolbar: GridToolbar }}
2023-02-18 14:07:11 +00:00
onSelectionModelChange={(ids) => onRowsSelectionHandler(ids)}
2022-10-20 03:05:10 +00:00
/>
</Box>
</Box>
2023-02-18 14:07:11 +00:00
<Box sx={{
2022-10-20 03:05:10 +00:00
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
width: '100%',
2022-10-20 03:05:10 +00:00
marginTop: "45px"
}}>
<BoxButton sx={{
maxWidth: "420px",
width: '100%',
2022-10-20 03:05:10 +00:00
display: "flex",
justifyContent: "space-between",
flexWrap: 'wrap',
2022-10-20 03:05:10 +00:00
}}>
2023-02-18 14:07:11 +00:00
<Button
variant="contained"
onClick={() => activation(false)}
2022-10-20 03:05:10 +00:00
sx={{
backgroundColor: theme.palette.menu.main,
width: "200px",
height: "48px",
fontWeight: "normal",
fontSize: "17px",
marginBottom: '10px',
2022-10-20 03:05:10 +00:00
"&:hover": {
backgroundColor: theme.palette.grayMedium.main
}
}}>
2023-02-18 14:07:11 +00:00
Деактивировать
2022-10-20 03:05:10 +00:00
</Button>
2023-02-18 14:07:11 +00:00
<Button
variant="contained"
onClick={() => activation(true)}
2022-10-20 03:05:10 +00:00
sx={{
backgroundColor: theme.palette.menu.main,
width: "200px",
height: "48px",
fontWeight: "normal",
fontSize: "17px",
"&:hover": {
backgroundColor: theme.palette.grayMedium.main
}
}}>
2023-02-18 14:07:11 +00:00
Применить
2022-10-20 03:05:10 +00:00
</Button>
</BoxButton>
2022-10-20 03:05:10 +00:00
</Box>
2023-02-18 14:07:11 +00:00
2022-10-20 03:05:10 +00:00
</LocalizationProvider>
</React.Fragment>
);
2023-02-18 14:07:11 +00:00
};
2022-10-20 03:05:10 +00:00
2022-12-02 09:47:46 +00:00
export default Discounts;