adminFront/src/pages/dashboard/Content/Tariffs/EditModal.tsx

154 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-06-20 18:21:44 +00:00
// @ts-nocheck
2023-06-15 14:25:59 +00:00
import React, { useEffect, useState } from "react";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import Modal from "@mui/material/Modal";
import TextField from "@mui/material/TextField";
import axios from "axios";
import { authStore } from "@root/stores/auth";
import { Privilege, Tariff } from "@root/model/tariff";
import { useTariffStore, updateTariff } from "@root/stores/tariffsStore";
2023-06-15 14:25:59 +00:00
import { arrayBuffer } from "stream/consumers";
import { useGetTariffs } from "@root/hooks/useGetTariffs.hook";
import { findPrivilegeById } from "@root/stores/privilegesStore";
import { enqueueSnackbar } from "notistack";
2023-06-15 14:25:59 +00:00
interface EditProps {
tarifIid: string,
tariffName: string,
tariffPrice: number,
privilege: Privilege,
token: string
}
const editTariff = ({
tarifIid,
tariffName,
tariffPrice,
privilege,
token
}:EditProps): Promise<unknown> => {
return axios({
method: "put",
url: `https://admin.pena.digital/strator/tariff/${tarifIid}`,
headers: {
Authorization: `Bearer ${token}`,
},
data: {
name: tariffName,
price: tariffPrice,
isCustom: true,
privilegies: [privilege]
},
});
}
interface Props {
tariff: Tariff
2023-06-15 14:25:59 +00:00
}
export default function EditModal({tariff = undefined}:Props) {
console.log(tariff)
2023-06-15 14:25:59 +00:00
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [price, setPrice] = useState("");
const { token } = authStore();
const tariffs = useTariffStore((state) => state.tariffs);
const currentTariff = tariff ? tariffs[tariff.id] : undefined
const { requestTariffs } = useGetTariffs()
2023-06-15 14:25:59 +00:00
useEffect(() => {
setOpen(tariff !== undefined)
}, [tariff])
return <Modal
open={open}
onClose={() => setOpen(false)}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 400,
bgcolor: "background.paper",
border: "2px solid gray",
borderRadius: "6px",
boxShadow: 24,
p: 4,
}}
>
<Typography id="modal-modal-title" variant="h6" component="h2" sx={{ whiteSpace: "nowrap" }}>
Редактирование тариффа
</Typography>
{currentTariff !== undefined && (
2023-06-15 14:25:59 +00:00
<Box sx={{ mt: "20px", display: "flex", flexDirection: "column" }}>
<Typography>Название Тарифа: {currentTariff.name}</Typography>
2023-06-15 14:25:59 +00:00
<TextField
onChange={(event) => setName(event.target.value)}
label="Имя"
name="name"
value={name}
sx={{ marginBottom: "10px" }}
/>
<Typography>Цена за единицу: {currentTariff.pricePerUnit}</Typography>
2023-06-15 14:25:59 +00:00
<TextField
onChange={(event) => setPrice(event.target.value)}
label="Цена за единицу"
name="price"
value={price}
sx={{ marginBottom: "10px" }}
/>
<Button onClick={() => {
if (!currentTariff.isFront) {
console.log(currentTariff)
const privilege = findPrivilegeById(currentTariff.privilegeId);
privilege.privilegeId = privilege.id
2023-06-15 14:25:59 +00:00
//back
if (privilege !== null) {editTariff({
tarifIid: currentTariff.id,
tariffName: name ? name : currentTariff.name,
tariffPrice: price ? Number(price) : currentTariff.price ? currentTariff.price : privilege.price,
isDeleted: currentTariff.isDeleted,
customPricePerUnit: currentTariff.price,
privilege: privilege,
2023-06-15 14:25:59 +00:00
token: token
})
.then(() => {
setOpen(false)
requestTariffs()
})} else {
enqueueSnackbar(`Привилегия с id ${currentTariff.privilegeId} не найдена в тарифе ${currentTariff.name} с id ${currentTariff.id}`)
}
2023-06-15 14:25:59 +00:00
} else {//front/store
let array = tariffs
let index
tariffs.forEach((p:any, i:number) => {
if (currentTariff.id == p.id) index = i
2023-06-15 14:25:59 +00:00
})
if (index !== undefined) {
array[index].name = name
array[index].amount = Number(price)
updateTariffs(array)
setOpen(false)
} else {
console.log("не нашел такой тариф в сторе")
}
}
}}>Редактировать</Button>
</Box>
)}
</Box>
</Modal>
}