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

118 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as React 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 { useTariffStore } from "@root/stores/tariffsStore";
2023-07-12 13:31:35 +00:00
import { requestTariffs } from "@root/services/tariffs.service";
import { enqueueSnackbar } from "notistack";
import { authStore } from "@root/stores/auth";
type DeleteModalProps = {
open: boolean | string;
handleClose: () => void;
selectedTariffs: any;
};
2023-07-12 10:27:21 +00:00
type DeleteTariffRequest = {
id: string;
};
const baseUrl =
process.env.NODE_ENV === "production"
? "/strator"
: "https://admin.pena.digital/strator";
export default function DeleteModal({
open,
handleClose,
2023-07-11 14:42:18 +00:00
selectedTariffs,
}: DeleteModalProps) {
2023-07-12 10:27:21 +00:00
const { makeRequest } = authStore();
2023-07-11 14:42:18 +00:00
const tariffs = useTariffStore((state) => state.tariffs);
const deleteTariff = async (id: string): Promise<void> => {
2023-07-11 14:42:18 +00:00
const currentTariff = tariffs[id];
if (!currentTariff) {
enqueueSnackbar("Тариф не найден");
2023-07-11 14:42:18 +00:00
return;
}
2023-07-11 14:42:18 +00:00
try {
2023-07-12 10:27:21 +00:00
await makeRequest<DeleteTariffRequest>({
url: baseUrl + "/tariff/",
method: "delete",
bearer: true,
body: { id },
2023-07-11 14:42:18 +00:00
});
} catch {
enqueueSnackbar("Ошибка при удалении тарифа на бэкэнде");
}
};
const onClickTariffDelete = () => {
2023-07-11 14:42:18 +00:00
if (typeof open === "string") {
deleteTariff(open);
requestTariffs();
handleClose();
2023-07-11 14:42:18 +00:00
return;
}
2023-07-11 14:42:18 +00:00
selectedTariffs.forEach((id: string) => {
deleteTariff(id);
});
handleClose();
2023-07-11 14:42:18 +00:00
requestTariffs();
};
return (
<div>
<Modal
open={Boolean(open)}
onClose={handleClose}
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">
2023-07-11 14:42:18 +00:00
Вы уверены, что хотите удалить{" "}
{typeof open === "string" ? "тариф" : "тарифы"} ?
</Typography>
<Box
2023-07-11 14:42:18 +00:00
sx={{
mt: "20px",
display: "flex",
width: "332px",
justifyContent: "space-between",
alignItems: "center",
}}
>
2023-07-11 14:42:18 +00:00
<Button
onClick={() => onClickTariffDelete()}
sx={{ width: "40px", height: "25px" }}
>
Да
</Button>
{/* <Typography>Тариф:</Typography>
{tariffName.map((name, index) => (
<Typography key={index}>{name};</Typography>
))} */}
</Box>
</Box>
</Modal>
</div>
);
}