adminFront/src/pages/dashboard/Content/Tariffs/EditModal.tsx
2023-07-25 21:48:52 +03:00

162 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @ts-nocheck
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";
import { useTariffStore } from "@root/stores/tariffsStore";
import { findPrivilegeById } from "@root/stores/privilegesStore";
import { requestTariffs } from "@root/services/tariffs.service";
import { enqueueSnackbar } from "notistack";
interface EditProps {
tarifIid: string;
tariffName: string;
tariffPrice: number;
privilege: Privilege;
}
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";
const editTariff = ({ tarifIid, tariffName, tariffPrice, privilege }: EditProps): Promise<unknown> => {
const { makeRequest } = authStore.getState();
return makeRequest<EditTariffBackendRequest>({
method: "put",
url: baseUrl + `/tariff/${tarifIid}`,
bearer: true,
body: {
name: tariffName,
price: tariffPrice,
isCustom: false,
privilegies: [privilege],
},
});
};
interface Props {
tariff: Tariff;
}
export default function EditModal({ tariff = undefined }: Props) {
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [price, setPrice] = useState("");
const tariffs = useTariffStore((state) => state.tariffs);
const currentTariff = tariff ? tariffs[tariff.id] : undefined;
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 && (
<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) {
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}`
);
}
} else {
//front/store
let array = tariffs;
let index;
tariffs.forEach((p: any, i: number) => {
if (currentTariff.id == p.id) index = i;
});
if (index !== undefined) {
array[index].name = name;
array[index].amount = Number(price);
updateTariffs(array);
setOpen(false);
} else {
console.log("не нашел такой тариф в сторе");
}
}
}}
>
Редактировать
</Button>
</Box>
)}
</Box>
</Modal>
);
}