2023-08-02 11:36:50 +00:00
|
|
|
|
import { useEffect, useState } from "react";
|
2023-06-15 14:25:59 +00:00
|
|
|
|
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";
|
2023-07-03 15:06:27 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-08-02 11:36:50 +00:00
|
|
|
|
import { devlog, getMessageFromFetchError } from "@frontend/kitui";
|
|
|
|
|
import { closeEditTariffDialog, useTariffStore } from "@root/stores/tariffs";
|
|
|
|
|
import { putTariff } from "@root/api/tariffs";
|
|
|
|
|
import { requestTariffs } from "@root/services/tariffs.service";
|
2023-06-15 14:25:59 +00:00
|
|
|
|
|
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
export default function EditModal() {
|
|
|
|
|
const [nameField, setNameField] = useState("");
|
|
|
|
|
const [priceField, setPriceField] = useState("");
|
|
|
|
|
const tariffs = useTariffStore((state) => state.tariffs);
|
|
|
|
|
const editTariffId = useTariffStore(state => state.editTariffId);
|
2023-07-12 10:27:21 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
const tariff = tariffs.find(tariff => tariff._id === editTariffId);
|
2023-07-12 10:27:21 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
useEffect(function setCurrentTariffFields() {
|
|
|
|
|
if (!tariff) return;
|
2023-07-12 10:27:21 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
setNameField(tariff.name);
|
|
|
|
|
setPriceField((tariff.price || 0).toString());
|
|
|
|
|
}, [tariff]);
|
2023-06-15 14:25:59 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
async function handleEditClick() {
|
|
|
|
|
if (!tariff) return enqueueSnackbar(`Тариф ${editTariffId} не найден`);
|
2023-07-11 14:42:18 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
const price = parseFloat(priceField);
|
2023-06-15 14:25:59 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
if (!isFinite(price)) return enqueueSnackbar("Поле \"Цена за единицу\" не число");
|
|
|
|
|
if (!nameField) return enqueueSnackbar("Поле \"Имя\" пустое");
|
2023-06-15 14:25:59 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
const updatedTariff = structuredClone(tariff);
|
|
|
|
|
updatedTariff.name = nameField;
|
|
|
|
|
updatedTariff.price = price;
|
|
|
|
|
try {
|
|
|
|
|
await putTariff(updatedTariff);
|
|
|
|
|
closeEditTariffDialog();
|
|
|
|
|
|
|
|
|
|
requestTariffs();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
devlog("Error updating tariff", error);
|
|
|
|
|
const message = getMessageFromFetchError(error);
|
|
|
|
|
if (message) enqueueSnackbar(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-11 14:42:18 +00:00
|
|
|
|
|
2023-08-02 11:36:50 +00:00
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
open={editTariffId !== null}
|
|
|
|
|
onClose={closeEditTariffDialog}
|
|
|
|
|
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,
|
|
|
|
|
}}
|
2023-07-11 14:42:18 +00:00
|
|
|
|
>
|
2023-08-02 11:36:50 +00:00
|
|
|
|
<Typography
|
|
|
|
|
id="modal-modal-title"
|
|
|
|
|
variant="h6"
|
|
|
|
|
component="h2"
|
|
|
|
|
sx={{ whiteSpace: "nowrap" }}
|
|
|
|
|
>
|
|
|
|
|
Редактирование тариффа
|
|
|
|
|
</Typography>
|
|
|
|
|
|
|
|
|
|
{tariff && (
|
|
|
|
|
<Box sx={{ mt: "20px", display: "flex", flexDirection: "column" }}>
|
|
|
|
|
<Typography>Название Тарифа: {tariff.name}</Typography>
|
|
|
|
|
<TextField
|
|
|
|
|
onChange={(event) => setNameField(event.target.value)}
|
|
|
|
|
label="Имя"
|
|
|
|
|
name="name"
|
|
|
|
|
value={nameField}
|
|
|
|
|
sx={{ marginBottom: "10px" }}
|
|
|
|
|
/>
|
|
|
|
|
<Typography>
|
|
|
|
|
Цена за единицу: {tariff.privilegies[0].price}
|
|
|
|
|
</Typography>
|
|
|
|
|
<TextField
|
|
|
|
|
type="number"
|
|
|
|
|
onChange={(event) => setPriceField(event.target.value)}
|
|
|
|
|
label="Цена за единицу"
|
|
|
|
|
name="price"
|
|
|
|
|
value={priceField}
|
|
|
|
|
sx={{ marginBottom: "10px" }}
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleEditClick}
|
|
|
|
|
>
|
|
|
|
|
Редактировать
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
2023-06-15 14:25:59 +00:00
|
|
|
|
}
|