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

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-09-20 14:35:49 +00:00
import * as React from "react";
import { Box } from "@mui/material";
import Templater from "./Templater";
import Quiz from "./Quiz";
import Contractor from "./Contractor";
2022-10-03 15:52:43 +00:00
import DataGridElement from "./DataGridElement";
2022-09-20 14:35:49 +00:00
import ModalMini from "./ModalMini";
2022-10-10 14:00:53 +00:00
import ModalPackage from "./ModalPackage";
2022-10-10 17:17:43 +00:00
import { ArrayProps, Tariff } from "./types";
2022-10-06 13:47:32 +00:00
import useStore from "../../../../store";
2022-09-20 14:35:49 +00:00
import theme from "../../../../theme";
const Tariffs: React.FC = () => {
2022-10-10 14:00:53 +00:00
const [openModalMini, setOpenModalMini] = React.useState(false);
2022-09-20 14:35:49 +00:00
2022-10-10 14:00:53 +00:00
const handleOpenModalMini = () => { setOpenModalMini(true); };
const handleCloseModalMini = () => { setOpenModalMini(false); };
2022-09-20 14:35:49 +00:00
2022-10-06 13:47:32 +00:00
const [type, setType] = React.useState( 100 );
2022-09-20 14:35:49 +00:00
const [variant, setVariant] = React.useState( 100 );
2022-10-10 14:00:53 +00:00
const setUpModalMini = (type:number, num:number) => {
2022-10-06 13:47:32 +00:00
setType( type );
2022-09-20 14:35:49 +00:00
setVariant( num );
2022-10-10 14:00:53 +00:00
handleOpenModalMini();
2022-09-20 14:35:49 +00:00
}
2022-10-06 13:47:32 +00:00
const getData = localStorage.getItem("tariffs");
const store = useStore( (state) => state );
if( getData && !store.tariffsArray.length ) {
2022-10-10 14:00:53 +00:00
const rows:Array<ArrayProps> = JSON.parse(getData);
2022-10-10 17:17:43 +00:00
if( rows.length ) { store.tariffsArraySet( rows ); };
2022-10-06 13:47:32 +00:00
}
2022-10-10 14:00:53 +00:00
const [openModalPackage, setOpenModalPackage] = React.useState(false);
2022-10-10 17:17:43 +00:00
const handleOpenModalPackage = () => { setOpenModalPackage(true); };
const handleCloseModalPackage = () => { setOpenModalPackage(false); };
const newPackage = ( name:string ) => {
const tariffs:Array<Tariff> = [];
store.selectedRowsData.forEach( (item) => {
if( item.type === "package" && item.tariffs ) {
tariffs.push( ...item.tariffs );
} else {
tariffs.push( item );
}
} );
const uniqueArray:Array<Tariff> = [];
tariffs.forEach( (tariff) => {
if( uniqueArray.findIndex( (a) => a.id === tariff.id ) < 0 ) {
uniqueArray.push( tariff );
}
} );
const packageCreated:ArrayProps = {
name,
id: new Date().getTime(),
type: "package",
tariffs: uniqueArray,
service: "",
disk: "",
time: "",
points: "",
price: 0
}
store.tariffsArraySet( [...store.tariffsArray, packageCreated] );
handleCloseModalPackage();
}
2022-10-10 14:00:53 +00:00
2022-09-20 14:35:49 +00:00
return (
<React.Fragment>
<Box sx={{
width: "90%",
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center"
}}>
2022-10-10 14:00:53 +00:00
<Templater openModal={ setUpModalMini } />
<Quiz openModal={ setUpModalMini } />
<Contractor openModal={ setUpModalMini } />
<DataGridElement openModal={ handleOpenModalPackage } />
2022-09-20 14:35:49 +00:00
</Box>
2022-10-10 14:00:53 +00:00
<ModalMini open={ openModalMini } type={ type } variant={ variant } close={ handleCloseModalMini } />
2022-10-10 17:17:43 +00:00
<ModalPackage open={ openModalPackage } newPackage={ newPackage } close={ handleCloseModalPackage } />
2022-09-20 14:35:49 +00:00
</React.Fragment>
);
}
export default Tariffs;