adminFront/src/pages/dashboard/Content/Tariffs/ModalMini/index.tsx

186 lines
6.0 KiB
TypeScript
Raw Normal View History

2022-09-20 14:35:49 +00:00
import * as React from "react";
import { Box, Modal, Fade, Backdrop, Typography, Button, TextField } from "@mui/material";
2022-10-06 13:47:32 +00:00
import { ArrayProps } from "../types";
import useStore, { StoreState } from "../../../../../store";
2022-09-20 14:35:49 +00:00
import theme from "../../../../../theme";
2022-10-06 13:47:32 +00:00
2022-09-20 14:35:49 +00:00
export interface MWProps {
open: boolean
2022-10-06 13:47:32 +00:00
type: number
2022-09-20 14:35:49 +00:00
variant: number
close: () => void
}
2022-10-06 13:47:32 +00:00
const ModalMini = ({open, type, variant, close}: MWProps ) => {
let tariffsArray:Array<ArrayProps> = useStore((state) => state.tariffsArray);
const { tariffsArraySet } = useStore<StoreState>((state) => state);
2022-10-10 17:17:43 +00:00
const types = [ "", "Шаблонизатор документов", "Опросник", "Сокращатель ссылок" ];
2022-10-06 13:47:32 +00:00
const variants = [ "Количество", "Срок (дней)", "Количество (гб)" ];
const fieldName = React.useRef<HTMLInputElement | null>(null);
const fieldTime = React.useRef<HTMLInputElement | null>(null);
const fieldPrice = React.useRef<HTMLInputElement | null>(null);
const checkTariff = () => {
if( fieldName.current != null && fieldTime.current != null && fieldPrice.current != null ) {
if( fieldName.current.value && fieldTime.current.value && fieldPrice.current.value ) {
const getData = localStorage.getItem("tariffs");
if( getData != null ) { tariffsArray = JSON.parse(getData); }
2022-12-02 09:47:46 +00:00
const data = [ 0, 0, 0 ];
2022-10-06 13:47:32 +00:00
2022-12-02 09:47:46 +00:00
if( variant == 0 ) { data[ 0 ] = parseInt(fieldTime.current.value); }
if( variant == 1 ) { data[ 1 ] = parseInt(fieldTime.current.value); }
if( variant == 2 ) { data[ 2 ] = parseInt(fieldTime.current.value); }
2022-10-06 13:47:32 +00:00
const tariffsArrayNew = [...tariffsArray, {
2022-10-10 17:17:43 +00:00
"id": new Date().getTime(),
2022-10-06 13:47:32 +00:00
"name": fieldName.current.value,
2022-10-10 14:00:53 +00:00
"type": "tariff",
2022-10-06 13:47:32 +00:00
"service": types[ type ],
"disk": data[ 2 ],
"time": data[ 1 ],
"points": data[ 0 ],
2022-10-10 14:00:53 +00:00
"price": +fieldPrice.current.value
2022-10-06 13:47:32 +00:00
} ];
tariffsArraySet( tariffsArrayNew );
localStorage.setItem( "tariffs", JSON.stringify( tariffsArrayNew ) );
close();
2022-10-10 14:00:53 +00:00
console.log( tariffsArrayNew );
2022-10-06 13:47:32 +00:00
}
}
}
2022-09-20 14:35:49 +00:00
return (
<React.Fragment>
<Modal
aria-labelledby="transition-modal-title"
aria-describedby="transition-modal-description"
open={ open }
onClose={ () => close() }
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
}}
>
<Fade in={open}>
<Box sx={{
position: "absolute" as "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
2022-10-01 14:06:54 +00:00
width: "350px",
2022-10-06 13:47:32 +00:00
height: "350px",
2022-09-20 14:35:49 +00:00
bgcolor: theme.palette.menu.main,
boxShadow: 24,
color: theme.palette.secondary.main,
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
}}>
2022-10-06 13:47:32 +00:00
<TextField
id = "standard-basic"
label = { types[ type ] }
disabled={ true }
variant = "filled"
color = "secondary"
sx = {{ width: "80%", marginTop: theme.spacing(1) }}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
/>
<TextField
id = "standard-basic"
label = { "Название тарифа" }
variant = "filled"
color = "secondary"
sx = {{ width: "80%", marginTop: theme.spacing(1) }}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
inputRef={ fieldName }
/>
<TextField
id = "standard-basic"
label = { variants[ variant ] }
variant = "filled"
color = "secondary"
2022-10-06 13:47:32 +00:00
sx = {{ width: "80%", marginTop: theme.spacing(1) }}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
2022-10-06 13:47:32 +00:00
inputRef={ fieldTime }
/>
<TextField
id = "standard-basic"
label = "Цена"
variant = "filled"
color = "secondary"
2022-10-06 13:47:32 +00:00
sx = {{ width: "80%", marginTop: theme.spacing(1) }}
InputProps={{
style: {
backgroundColor: theme.palette.content.main,
color: theme.palette.secondary.main,
} }}
InputLabelProps={{
style: {
color: theme.palette.secondary.main
} }}
2022-10-06 13:47:32 +00:00
inputRef={ fieldPrice }
/>
2022-09-20 14:35:49 +00:00
<Button
variant = "contained"
2022-10-06 13:47:32 +00:00
onClick={ () => checkTariff() }
2022-09-20 14:35:49 +00:00
sx={{
backgroundColor: theme.palette.grayDark.main,
marginTop: "30px",
height: "42px",
fontWeight: "normal",
fontSize: "17px",
"&:hover": {
backgroundColor: theme.palette.grayMedium.main
}
}}>
Применить
</Button>
</Box>
</Fade>
</Modal>
</React.Fragment>
);
}
export default ModalMini;