adminFront/src/Components/LoggedIn/Content/Promocode/index.tsx

451 lines
14 KiB
TypeScript
Raw Normal View History

2022-09-19 11:45:58 +00:00
import * as React from "react";
import { Box, Typography, TextField, Checkbox, Button } from "@mui/material";
2022-10-13 06:36:28 +00:00
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";
2022-10-17 08:55:00 +00:00
import { PrivilegesProps, PromocodeProps } from "./types";
import useStore, { StoreState } from "../../../../store";
2022-09-20 14:35:49 +00:00
import theme from "../../../../theme";
2022-09-19 11:45:58 +00:00
2022-10-13 06:36:28 +00:00
const columns: GridColDef[] = [
{
field: "id",
headerName: "ID",
width: 30,
sortable: false,
},
{
field: "name",
headerName: "Название промокода",
width: 200,
sortable: false,
},
{
2022-10-17 08:55:00 +00:00
field: "endless",
headerName: "Бесконечный",
width: 120,
sortable: false,
},
{
field: "from",
headerName: "От",
2022-10-18 09:43:32 +00:00
width: 120,
2022-10-13 06:36:28 +00:00
sortable: false,
},
{
2022-10-17 08:55:00 +00:00
field: "dueTo",
headerName: "До",
2022-10-18 09:43:32 +00:00
width: 120,
2022-10-17 08:55:00 +00:00
sortable: false,
},
{
field: "privileges",
headerName: "Привилегии",
width: 210,
2022-10-13 06:36:28 +00:00
sortable: false,
}
];
2022-10-17 08:55:00 +00:00
const rows:Array<PromocodeProps> = [
2022-10-18 09:43:32 +00:00
{ id: 1, name: "Промокод 1", endless: false, from: "", dueTo: "", privileges: [
2022-10-17 08:55:00 +00:00
{
good: "Товар 1",
2022-10-18 09:43:32 +00:00
discount: 0.3
2022-10-17 08:55:00 +00:00
},
{
good: "Товар 2",
2022-10-18 09:43:32 +00:00
discount: 0.2
2022-10-17 08:55:00 +00:00
}
] },
2022-10-18 09:43:32 +00:00
{ id: 2, name: "Промокод 2", endless: false, from: "", dueTo: "", privileges: [
2022-10-17 08:55:00 +00:00
{
good: "Товар 3",
2022-10-18 09:43:32 +00:00
discount: 0.3
2022-10-17 08:55:00 +00:00
},
{
good: "Товар 4",
2022-10-18 09:43:32 +00:00
discount: 0.2
2022-10-17 08:55:00 +00:00
}
] },
2022-10-18 09:43:32 +00:00
{ id: 3, name: "Промокод 3", endless: false, from: "", dueTo: "", privileges: [
2022-10-17 08:55:00 +00:00
{
good: "Товар 5",
2022-10-18 09:43:32 +00:00
discount: 0.3
2022-10-17 08:55:00 +00:00
},
{
good: "Товар 6",
2022-10-18 09:43:32 +00:00
discount: 0.2
2022-10-17 08:55:00 +00:00
}
] },
2022-10-13 06:36:28 +00:00
];
2022-09-19 11:45:58 +00:00
const Promocode: React.FC = () => {
2022-10-17 08:55:00 +00:00
const [checkboxState, setCheckboxState] = React.useState<boolean>(false);
const toggleCheckbox = () => { setCheckboxState( !checkboxState ); }
2022-09-19 11:45:58 +00:00
2022-10-17 08:55:00 +00:00
const [value1, setValue1] = React.useState<Date>( new Date() );
const [value2, setValue2] = React.useState<Date>( new Date() );
2022-09-19 11:45:58 +00:00
2022-10-13 06:36:28 +00:00
const [service, setService] = React.useState("Шаблонизатор");
const handleChange = (event: SelectChangeEvent) => {
setService(event.target.value as string);
};
2022-10-17 08:55:00 +00:00
const { promocodeArray, promocodeArraySet } = useStore<StoreState>((state) => state);
const promocodeArrayConverted = promocodeArray.map( (item) => {
2022-10-19 05:04:45 +00:00
const dateFrom = item.from ? new Date( Number(item.from) ) : "";
const dateDueTo = item.from ? new Date( Number(item.dueTo) ) : "";
2022-10-18 09:43:32 +00:00
2022-10-19 05:04:45 +00:00
const strFrom = dateFrom
? `${dateFrom.getDate()}.${dateFrom.getMonth()}.${dateFrom.getFullYear()}`
: "-"
const strDueTo = dateDueTo
? `${dateDueTo.getDate()}.${dateDueTo.getMonth()}.${dateDueTo.getFullYear()}`
: "-"
2022-10-18 09:43:32 +00:00
2022-10-17 08:55:00 +00:00
if( item.privileges.length ) {
const result = item.privileges.reduce( (acc, privilege) => {
acc = acc
2022-10-18 09:43:32 +00:00
? `${acc}, ${privilege.good} - ${privilege.discount}%`
2022-10-19 05:04:45 +00:00
: `${privilege.good} - ${privilege.discount * 100}%`;
2022-10-17 08:55:00 +00:00
return acc;
}, "" );
2022-10-18 09:43:32 +00:00
return { ...item, privileges: result, from: strFrom, dueTo: strDueTo }
2022-10-17 08:55:00 +00:00
} else {
2022-10-18 09:43:32 +00:00
return { ...item, from: strFrom, dueTo: strDueTo }
2022-10-17 08:55:00 +00:00
}
} );
2022-10-18 09:43:32 +00:00
const createPromocode = ( name:string, discount: number ) => {
2022-10-17 08:55:00 +00:00
const newPromocode = {
id: new Date().getTime(),
name,
endless: checkboxState,
2022-10-18 09:43:32 +00:00
from: checkboxState ? "" : new Date( value1 ).getTime() +"",
dueTo: checkboxState ? "" : new Date( value2 ).getTime() +"",
privileges: [{
good: service,
2022-10-19 05:04:45 +00:00
discount: discount / 100
2022-10-18 09:43:32 +00:00
}]
2022-10-17 08:55:00 +00:00
}
const promocodeArrayUpdated = [ ...promocodeArray, newPromocode ];
promocodeArraySet( promocodeArrayUpdated );
}
const fieldName = React.useRef<HTMLInputElement | null>(null);
2022-10-18 09:43:32 +00:00
const fieldDiscount = React.useRef<HTMLInputElement | null>(null);
2022-10-17 08:55:00 +00:00
2022-10-18 09:43:32 +00:00
const checkFields = () => {
if( fieldName.current != null && fieldDiscount.current != null ) {
createPromocode( fieldName.current.value, Number(fieldDiscount.current.value) );
2022-10-17 08:55:00 +00:00
}
}
2022-09-19 11:45:58 +00:00
return (
<React.Fragment>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Typography
variant="subtitle1"
sx={{
width: "90%",
height: "60px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
color: theme.palette.secondary.main
}}>
ПРОМОКОД
</Typography>
2022-10-13 06:36:28 +00:00
<Box sx={{
display: "flex",
flexDirection: "column",
justifyContent: "left",
alignItems: "left",
marginTop: "15px",
2022-09-19 11:45:58 +00:00
}}>
2022-10-13 06:36:28 +00:00
<Typography
variant="h4"
sx={{
width: "90%",
2022-09-19 11:45:58 +00:00
height: "40px",
2022-10-13 06:36:28 +00:00
fontWeight: "normal",
color: theme.palette.grayDisabled.main,
marginTop: "35px"
}}>
Название:
</Typography>
<TextField
id = "standard-basic"
label = { "" }
variant = "filled"
color = "secondary"
sx={{
width: "200px",
height: "30px",
marginTop: "-20px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
2022-10-17 08:55:00 +00:00
inputRef={ fieldName }
2022-09-19 11:45:58 +00:00
/>
2022-10-13 06:36:28 +00:00
<Typography
variant="h4"
sx={{
width: "90%",
2022-09-19 11:45:58 +00:00
height: "40px",
2022-10-13 06:36:28 +00:00
fontWeight: "normal",
color: theme.palette.grayDisabled.main,
marginTop: "75px"
}}>
Условия:
</Typography>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={service}
label="Age"
onChange={handleChange}
sx={{
2022-09-19 11:45:58 +00:00
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
2022-10-13 06:36:28 +00:00
"&.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
id = "standard-basic"
label = { "Процент скидки" }
variant = "filled"
color = "secondary"
sx={{
marginTop: "15px"
}}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
2022-10-18 09:43:32 +00:00
inputRef={ fieldDiscount }
2022-09-19 11:45:58 +00:00
/>
2022-10-13 06:36:28 +00:00
<TableContainer component={Paper} sx={{
width: "100%",
marginTop: "35px",
backgroundColor: theme.palette.content.main
}}>
<Table sx={{ minWidth: 650, }} aria-label="simple table">
<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>
2022-09-19 11:45:58 +00:00
2022-10-13 06:36:28 +00:00
<Typography
variant="h4"
sx={{
width: "90%",
height: "40px",
fontWeight: "normal",
color: theme.palette.grayDisabled.main,
marginTop: "55px"
}}>
Дата действия:
</Typography>
2022-09-19 11:45:58 +00:00
<Box sx={{
2022-10-13 06:36:28 +00:00
width: "90%",
display: "flex"
}}>
<Typography sx={{
width: "35px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "left",
}}>С</Typography>
<DesktopDatePicker
2022-10-17 08:55:00 +00:00
inputFormat="DD/MM/YYYY"
value={ value1 }
onChange={ (e) => { if(e) { setValue1(e) } } }
2022-10-13 06:36:28 +00:00
renderInput={(params) => <TextField {...params} />}
InputProps={{ sx: {
height: "40px",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main }
} }}
/>
<Typography sx={{
width: "65px",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
}}>по</Typography>
<DesktopDatePicker
2022-10-17 08:55:00 +00:00
inputFormat="DD/MM/YYYY"
value={ value2 }
onChange={ (e) => { if(e) { setValue2(e) } } }
2022-10-13 06:36:28 +00:00
renderInput={(params) => <TextField {...params} />}
InputProps={{ sx: {
height: "40px",
color: theme.palette.secondary.main,
border: "1px solid",
borderColor: theme.palette.secondary.main,
"& .MuiSvgIcon-root": { color: theme.palette.secondary.main }
} }}
/>
</Box>
<Box sx={{
2022-09-19 11:45:58 +00:00
display: "flex",
2022-10-13 06:36:28 +00:00
width: "90%",
marginTop: theme.spacing(2),
2022-09-19 11:45:58 +00:00
}}>
2022-10-13 06:36:28 +00:00
<Box sx={{
width: "20px",
height: "42px",
display: "flex",
flexDirection: "column",
justifyContent: "left",
alignItems: "left",
marginRight: theme.spacing(1)
}}>
<Checkbox sx={{
2022-09-19 11:45:58 +00:00
color: theme.palette.secondary.main,
2022-10-13 06:36:28 +00:00
"&.Mui-checked": {
color: theme.palette.secondary.main,
},
2022-10-17 08:55:00 +00:00
}} onClick={ () => toggleCheckbox() } />
2022-10-13 06:36:28 +00:00
</Box>
<Box sx={{
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center"
}}>
Бессрочно
</Box>
2022-09-19 11:45:58 +00:00
</Box>
2022-10-13 06:36:28 +00:00
2022-09-19 11:45:58 +00:00
<Box sx={{
2022-10-13 06:36:28 +00:00
width: "90%",
marginTop: "55px",
2022-10-17 08:55:00 +00:00
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center"
2022-09-19 11:45:58 +00:00
}}>
2022-10-13 06:36:28 +00:00
<Button
variant = "contained"
sx={{
backgroundColor: theme.palette.menu.main,
height: "52px",
fontWeight: "normal",
fontSize: "17px",
"&:hover": {
backgroundColor: theme.palette.grayMedium.main
}
2022-10-17 08:55:00 +00:00
}}
2022-10-18 09:43:32 +00:00
onClick={ () => checkFields() } >
2022-10-13 06:36:28 +00:00
Cоздать
</Button>
2022-09-19 11:45:58 +00:00
</Box>
2022-10-13 06:36:28 +00:00
2022-09-19 11:45:58 +00:00
</Box>
2022-10-17 08:55:00 +00:00
<Box style={{ width: "80%", marginTop: "55px" }}>
2022-10-13 06:36:28 +00:00
<Box style={{ height: 400 }}>
<DataGrid
checkboxSelection={true}
2022-10-17 08:55:00 +00:00
rows={ promocodeArrayConverted }
columns={ columns }
2022-10-13 06:36:28 +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 }}
onSelectionModelChange={ (ids) => console.log("datagrid select") }
/>
</Box>
</Box>
2022-09-19 11:45:58 +00:00
</LocalizationProvider>
</React.Fragment>
);
}
export default Promocode;