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

183 lines
5.7 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 { authStore } from "@root/stores/auth";
import { Privilege, Tariff } from "@root/model/tariff";
2023-07-12 10:27:21 +00:00
import { useTariffStore } from "@root/stores/tariffsStore";
import { findPrivilegeById } from "@root/stores/privilegesStore";
2023-07-12 13:31:35 +00:00
import { requestTariffs } from "@root/services/tariffs.service";
import { enqueueSnackbar } from "notistack";
2023-06-15 14:25:59 +00:00
interface EditProps {
2023-07-11 14:42:18 +00:00
tarifIid: string;
tariffName: string;
tariffPrice: number;
privilege: Privilege;
2023-06-15 14:25:59 +00:00
}
2023-07-12 10:27:21 +00:00
type EditTariffBackendRequest = {
name: string;
price: number;
isCustom: boolean;
privilegies: Omit<Privilege_BACKEND, "_id" | "updatedAt">[];
};
const baseUrl =
process.env.NODE_ENV === "production"
? "/strator"
: "https://admin.pena.digital/strator";
2023-06-15 14:25:59 +00:00
const editTariff = ({
tarifIid,
tariffName,
tariffPrice,
privilege,
2023-07-11 14:42:18 +00:00
}: EditProps): Promise<unknown> => {
2023-07-12 10:27:21 +00:00
const { makeRequest } = authStore.getState();
return makeRequest<EditTariffBackendRequest>({
2023-06-15 14:25:59 +00:00
method: "put",
2023-07-12 10:27:21 +00:00
url: baseUrl + `/tariff/${tarifIid}`,
bearer: true,
body: {
2023-06-15 14:25:59 +00:00
name: tariffName,
price: tariffPrice,
isCustom: false,
2023-07-11 14:42:18 +00:00
privilegies: [privilege],
2023-06-15 14:25:59 +00:00
},
});
2023-07-11 14:42:18 +00:00
};
2023-06-15 14:25:59 +00:00
interface Props {
2023-07-11 14:42:18 +00:00
tariff: Tariff;
2023-06-15 14:25:59 +00:00
}
2023-07-12 13:31:35 +00:00
export default function EditModal({ tariff = undefined }: Props) {
2023-06-15 14:25:59 +00:00
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [price, setPrice] = useState("");
const tariffs = useTariffStore((state) => state.tariffs);
2023-07-11 14:42:18 +00:00
const currentTariff = tariff ? tariffs[tariff.id] : undefined;
2023-06-15 14:25:59 +00:00
useEffect(() => {
2023-07-11 14:42:18 +00:00
setOpen(tariff !== undefined);
}, [tariff]);
2023-06-15 14:25:59 +00:00
2023-07-11 14:42:18 +00:00
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,
}}
2023-06-15 14:25:59 +00:00
>
2023-07-11 14:42:18 +00:00
<Typography
id="modal-modal-title"
variant="h6"
component="h2"
sx={{ whiteSpace: "nowrap" }}
2023-06-15 14:25:59 +00:00
>
2023-07-11 14:42:18 +00:00
Редактирование тариффа
</Typography>
2023-06-15 14:25:59 +00:00
2023-07-11 14:42:18 +00:00
{currentTariff !== undefined && (
<Box sx={{ mt: "20px", display: "flex", flexDirection: "column" }}>
<Typography>Название Тарифа: {currentTariff.name}</Typography>
<TextField
onChange={(event) => setName(event.target.value)}
label="Имя"
name="name"
value={name}
sx={{ marginBottom: "10px" }}
/>
<Typography>
Цена за единицу: {currentTariff.pricePerUnit}
</Typography>
<TextField
onChange={(event) => setPrice(event.target.value)}
label="Цена за единицу"
name="price"
value={price}
sx={{ marginBottom: "10px" }}
/>
<Button
onClick={() => {
if (!currentTariff.isFront) {
2023-07-11 14:42:18 +00:00
const privilege = findPrivilegeById(
currentTariff.privilegeId
);
privilege.privilegeId = privilege.id;
console.log(privilege);
//back
if (privilege !== null) {
privilege.amount = tariff.amount;
privilege.privilegeName = privilege.name;
privilege.privilegeId = privilege.id;
privilege.serviceName = privilege.serviceKey;
console.log(privilege);
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,
}).then(() => {
setOpen(false);
requestTariffs();
});
} else {
enqueueSnackbar(
`Привилегия с id ${currentTariff.privilegeId} не найдена в тарифе ${currentTariff.name} с id ${currentTariff.id}`
);
}
2023-07-11 14:42:18 +00:00
} else {
//front/store
2023-06-15 14:25:59 +00:00
2023-07-11 14:42:18 +00:00
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) {
2023-07-11 14:42:18 +00:00
array[index].name = name;
array[index].amount = Number(price);
updateTariffs(array);
setOpen(false);
2023-06-15 14:25:59 +00:00
} else {
2023-07-11 14:42:18 +00:00
console.log("не нашел такой тариф в сторе");
2023-06-15 14:25:59 +00:00
}
}
2023-07-11 14:42:18 +00:00
}}
>
Редактировать
</Button>
</Box>
)}
</Box>
</Modal>
);
2023-06-15 14:25:59 +00:00
}