2023-06-12 13:48:06 +00:00
|
|
|
|
import React from "react";
|
2023-06-11 11:20:58 +00:00
|
|
|
|
import { useEffect, useState } from "react";
|
2023-02-28 08:30:57 +00:00
|
|
|
|
import { GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid";
|
2023-06-12 13:48:06 +00:00
|
|
|
|
import { Box, Button, IconButton } from "@mui/material";
|
2023-06-11 11:20:58 +00:00
|
|
|
|
import BackspaceIcon from "@mui/icons-material/Backspace";
|
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-02-28 08:30:57 +00:00
|
|
|
|
import DataGrid from "@kitUI/datagrid";
|
2023-05-16 16:57:44 +00:00
|
|
|
|
|
2023-06-06 11:27:49 +00:00
|
|
|
|
import { deleteTariffs, useTariffStore } from "@root/stores/tariffs";
|
2023-03-06 13:26:55 +00:00
|
|
|
|
import { SERVICE_LIST } from "@root/model/tariff";
|
2023-05-23 12:20:37 +00:00
|
|
|
|
import { findPrivilegeById } from "@root/stores/privileges";
|
2023-02-28 08:30:57 +00:00
|
|
|
|
|
2023-06-11 11:20:58 +00:00
|
|
|
|
import axios from "axios";
|
|
|
|
|
import { authStore } from "@root/stores/auth";
|
2023-06-12 13:48:06 +00:00
|
|
|
|
import DeleteModal from "@root/kitUI/DeleteModal";
|
|
|
|
|
import EditModal from "@root/kitUI/EditModal";
|
2023-02-28 08:30:57 +00:00
|
|
|
|
|
2023-03-06 13:26:55 +00:00
|
|
|
|
interface Props {
|
2023-06-12 13:48:06 +00:00
|
|
|
|
selectedTariffs: GridSelectionModel;
|
2023-05-16 16:57:44 +00:00
|
|
|
|
handleSelectionChange: (selectionModel: GridSelectionModel) => void;
|
2023-03-06 13:26:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 13:48:06 +00:00
|
|
|
|
export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Props) {
|
2023-06-11 11:20:58 +00:00
|
|
|
|
const exampleTariffs = useTariffStore((state) => state.tariffs);
|
|
|
|
|
const [tariffs, setTariffs] = useState<any>();
|
|
|
|
|
const [deletedRows, setDeletedRows] = useState<string[]>([]);
|
2023-06-12 13:48:06 +00:00
|
|
|
|
const [openDeleteModal, setOpenDeleteModal] = useState(false);
|
|
|
|
|
const [openEditModal, setOpenEditModal] = useState(false);
|
2023-06-11 11:20:58 +00:00
|
|
|
|
const { token } = authStore();
|
|
|
|
|
|
2023-06-12 13:48:06 +00:00
|
|
|
|
const [errorEdit, setErrorEdit] = useState(false);
|
|
|
|
|
const [errorDelete, setErrorDelete] = useState(false);
|
|
|
|
|
|
2023-06-11 11:20:58 +00:00
|
|
|
|
const mergeTariffs = [...exampleTariffs, ...(tariffs?.tariffs ?? [])];
|
|
|
|
|
|
2023-06-12 13:48:06 +00:00
|
|
|
|
const tariffEdit = async (tarifIid: string, tariffName: string, tariffPrice: string) => {
|
|
|
|
|
try {
|
|
|
|
|
await axios({
|
|
|
|
|
method: "put",
|
|
|
|
|
url: `https://admin.pena.digital/strator/tariff/${tarifIid}`,
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
name: tariffName,
|
|
|
|
|
price: tariffPrice,
|
|
|
|
|
isCustom: true,
|
|
|
|
|
privilegies: [
|
|
|
|
|
{
|
|
|
|
|
name: "Количество попыток использования",
|
|
|
|
|
privilegeId: "507f1f77bcf86cd799439011",
|
|
|
|
|
serviceKey: "docx-templater-service",
|
|
|
|
|
description: "Количество попыток использования",
|
|
|
|
|
type: "count",
|
|
|
|
|
value: "200",
|
|
|
|
|
price: 12300,
|
|
|
|
|
amount: 300,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
setErrorEdit(true);
|
|
|
|
|
enqueueSnackbar(error.message);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-06-11 11:20:58 +00:00
|
|
|
|
|
2023-06-12 13:48:06 +00:00
|
|
|
|
const tariffDelete = async (tarifIid: string) => {
|
2023-06-11 11:20:58 +00:00
|
|
|
|
try {
|
|
|
|
|
await axios({
|
|
|
|
|
method: "delete",
|
|
|
|
|
url: "https://admin.pena.digital/strator/tariff",
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
data: { id: tarifIid },
|
|
|
|
|
});
|
|
|
|
|
setDeletedRows((prevDeletedRows) => [...prevDeletedRows, tarifIid]);
|
2023-06-12 13:48:06 +00:00
|
|
|
|
} catch (error: any) {
|
|
|
|
|
setErrorDelete(true);
|
|
|
|
|
enqueueSnackbar(error.message);
|
2023-06-11 11:20:58 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const axiosTariffs = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await axios({
|
|
|
|
|
method: "get",
|
|
|
|
|
url: "https://admin.pena.digital/strator/tariff",
|
|
|
|
|
});
|
|
|
|
|
setTariffs(data);
|
|
|
|
|
console.log(data);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
enqueueSnackbar("Ошибка получения тарифов");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
axiosTariffs();
|
|
|
|
|
}, [deletedRows]);
|
2023-03-06 13:26:55 +00:00
|
|
|
|
|
2023-06-06 11:27:49 +00:00
|
|
|
|
const columns: GridColDef[] = [
|
|
|
|
|
{ field: "id", headerName: "ID", width: 100 },
|
|
|
|
|
{ field: "name", headerName: "Название тарифа", width: 150 },
|
|
|
|
|
{ field: "serviceName", headerName: "Сервис", width: 150 }, //инфо из гитлаба.
|
|
|
|
|
{ field: "privilege", headerName: "Привелегия", width: 150 },
|
|
|
|
|
{ field: "amount", headerName: "Количество", width: 110 },
|
|
|
|
|
{ field: "type", headerName: "Единица", width: 100 },
|
|
|
|
|
{ field: "pricePerUnit", headerName: "Цена за ед.", width: 100 },
|
|
|
|
|
{ field: "isCustomPrice", headerName: "Кастомная цена", width: 130 },
|
|
|
|
|
{ field: "total", headerName: "Сумма", width: 60 },
|
2023-06-11 11:20:58 +00:00
|
|
|
|
{ field: "isDeleted", headerName: "isDeleted", width: 60 },
|
2023-06-06 11:27:49 +00:00
|
|
|
|
{
|
|
|
|
|
field: "delete",
|
|
|
|
|
headerName: "Удаление",
|
|
|
|
|
width: 60,
|
|
|
|
|
renderCell: ({ row }) => {
|
|
|
|
|
return (
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={() => {
|
2023-06-12 13:48:06 +00:00
|
|
|
|
tariffDelete(row.id);
|
2023-06-06 11:27:49 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<BackspaceIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-06-11 11:20:58 +00:00
|
|
|
|
const gridData = mergeTariffs.map(
|
|
|
|
|
(tariff: {
|
|
|
|
|
_id: string;
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
privilegeId: string;
|
|
|
|
|
amount: number;
|
|
|
|
|
price: number;
|
|
|
|
|
isDeleted: boolean;
|
|
|
|
|
customPricePerUnit: number;
|
|
|
|
|
}) => ({
|
|
|
|
|
id: tariff._id ? tariff._id : tariff.id,
|
|
|
|
|
name: tariff.name,
|
|
|
|
|
serviceName: SERVICE_LIST.find(
|
|
|
|
|
(service) => service.serviceKey === findPrivilegeById(tariff.privilegeId)?.serviceKey
|
|
|
|
|
)?.displayName,
|
|
|
|
|
privilege: `(${tariff.privilegeId}) ${
|
|
|
|
|
findPrivilegeById(tariff.privilegeId)?.description ?? "Привилегия не найдена"
|
|
|
|
|
}`,
|
|
|
|
|
amount: tariff.amount,
|
|
|
|
|
type: findPrivilegeById(tariff.privilegeId)?.type === "count" ? "день" : "шт.",
|
|
|
|
|
pricePerUnit: tariff.customPricePerUnit
|
|
|
|
|
? tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price
|
|
|
|
|
: tariff.price,
|
|
|
|
|
isCustomPrice: tariff.customPricePerUnit === undefined ? "Нет" : "Да",
|
|
|
|
|
isDeleted: tariff.isDeleted,
|
|
|
|
|
total: tariff.amount
|
|
|
|
|
? tariff.amount * (tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price ?? 0)
|
|
|
|
|
: 0,
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-06-12 13:48:06 +00:00
|
|
|
|
|
|
|
|
|
const selectedTariff = gridData.find((tariff) => tariff.id === selectedTariffs[0]);
|
|
|
|
|
const selectedTariffName = selectedTariff ? selectedTariff.name : "";
|
|
|
|
|
|
|
|
|
|
console.log(selectedTariff);
|
|
|
|
|
|
2023-05-16 16:57:44 +00:00
|
|
|
|
return (
|
2023-06-12 13:48:06 +00:00
|
|
|
|
<>
|
|
|
|
|
<DataGrid
|
|
|
|
|
checkboxSelection={true}
|
|
|
|
|
rows={gridData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
getRowId={(row) => row.id}
|
|
|
|
|
components={{ Toolbar: GridToolbar }}
|
|
|
|
|
onSelectionModelChange={handleSelectionChange}
|
|
|
|
|
/>
|
|
|
|
|
{selectedTariffs.length ? (
|
|
|
|
|
<Box component="section" sx={{ width: "100%", display: "flex", justifyContent: "start", mb: "30px" }}>
|
|
|
|
|
<Button onClick={() => setOpenDeleteModal(true)} sx={{ mr: "20px" }}>
|
|
|
|
|
Удаление
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={() => setOpenEditModal(true)}>Редактирование</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
) : (
|
|
|
|
|
<React.Fragment />
|
|
|
|
|
)}
|
|
|
|
|
<DeleteModal
|
|
|
|
|
tariffDelete={tariffDelete}
|
|
|
|
|
errorDelete={errorDelete}
|
|
|
|
|
tariffId={String(selectedTariffs[0])}
|
|
|
|
|
tariffName={selectedTariffName}
|
|
|
|
|
open={openDeleteModal}
|
|
|
|
|
handleClose={() => setOpenDeleteModal(false)}
|
|
|
|
|
/>
|
|
|
|
|
<EditModal
|
|
|
|
|
tariffEdit={tariffEdit}
|
|
|
|
|
errorEdit={errorEdit}
|
|
|
|
|
tariff={selectedTariff}
|
|
|
|
|
open={openEditModal}
|
|
|
|
|
handleClose={() => setOpenEditModal(false)}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2023-05-16 16:57:44 +00:00
|
|
|
|
);
|
2023-02-28 08:30:57 +00:00
|
|
|
|
}
|