Реализация запроса на получение тарифов, отображение в таблице
This commit is contained in:
parent
e18b086b3c
commit
db300443f2
@ -1,20 +1,64 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
import { GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid";
|
import { GridColDef, GridSelectionModel, GridToolbar } from "@mui/x-data-grid";
|
||||||
|
import { IconButton } from "@mui/material";
|
||||||
|
import BackspaceIcon from "@mui/icons-material/Backspace";
|
||||||
|
import { enqueueSnackbar } from "notistack";
|
||||||
|
|
||||||
import DataGrid from "@kitUI/datagrid";
|
import DataGrid from "@kitUI/datagrid";
|
||||||
|
|
||||||
import { deleteTariffs, useTariffStore } from "@root/stores/tariffs";
|
import { deleteTariffs, useTariffStore } from "@root/stores/tariffs";
|
||||||
import { SERVICE_LIST } from "@root/model/tariff";
|
import { SERVICE_LIST } from "@root/model/tariff";
|
||||||
import { findPrivilegeById } from "@root/stores/privileges";
|
import { findPrivilegeById } from "@root/stores/privileges";
|
||||||
import { IconButton } from "@mui/material";
|
|
||||||
|
|
||||||
import BackspaceIcon from "@mui/icons-material/Backspace";
|
import axios from "axios";
|
||||||
|
import { authStore } from "@root/stores/auth";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
handleSelectionChange: (selectionModel: GridSelectionModel) => void;
|
handleSelectionChange: (selectionModel: GridSelectionModel) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TariffsDG({ handleSelectionChange }: Props) {
|
export default function TariffsDG({ handleSelectionChange }: Props) {
|
||||||
const tariffs = useTariffStore((state) => state.tariffs);
|
const exampleTariffs = useTariffStore((state) => state.tariffs);
|
||||||
|
const [tariffs, setTariffs] = useState<any>();
|
||||||
|
const [deletedRows, setDeletedRows] = useState<string[]>([]);
|
||||||
|
const { token } = authStore();
|
||||||
|
|
||||||
|
const mergeTariffs = [...exampleTariffs, ...(tariffs?.tariffs ?? [])];
|
||||||
|
|
||||||
|
console.log(mergeTariffs);
|
||||||
|
|
||||||
|
const tariffsDelete = async (tarifIid: string) => {
|
||||||
|
try {
|
||||||
|
await axios({
|
||||||
|
method: "delete",
|
||||||
|
url: "https://admin.pena.digital/strator/tariff",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
data: { id: tarifIid },
|
||||||
|
});
|
||||||
|
setDeletedRows((prevDeletedRows) => [...prevDeletedRows, tarifIid]);
|
||||||
|
} catch (error) {
|
||||||
|
enqueueSnackbar("Ошибка удаления тарифов");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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]);
|
||||||
|
|
||||||
const columns: GridColDef[] = [
|
const columns: GridColDef[] = [
|
||||||
{ field: "id", headerName: "ID", width: 100 },
|
{ field: "id", headerName: "ID", width: 100 },
|
||||||
@ -26,6 +70,7 @@ export default function TariffsDG({ handleSelectionChange }: Props) {
|
|||||||
{ field: "pricePerUnit", headerName: "Цена за ед.", width: 100 },
|
{ field: "pricePerUnit", headerName: "Цена за ед.", width: 100 },
|
||||||
{ field: "isCustomPrice", headerName: "Кастомная цена", width: 130 },
|
{ field: "isCustomPrice", headerName: "Кастомная цена", width: 130 },
|
||||||
{ field: "total", headerName: "Сумма", width: 60 },
|
{ field: "total", headerName: "Сумма", width: 60 },
|
||||||
|
{ field: "isDeleted", headerName: "isDeleted", width: 60 },
|
||||||
{
|
{
|
||||||
field: "delete",
|
field: "delete",
|
||||||
headerName: "Удаление",
|
headerName: "Удаление",
|
||||||
@ -34,7 +79,7 @@ export default function TariffsDG({ handleSelectionChange }: Props) {
|
|||||||
return (
|
return (
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
deleteTariffs(row.id);
|
tariffsDelete(row.id);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<BackspaceIcon />
|
<BackspaceIcon />
|
||||||
@ -44,8 +89,18 @@ export default function TariffsDG({ handleSelectionChange }: Props) {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
const gridData = tariffs.map((tariff) => ({
|
const gridData = mergeTariffs.map(
|
||||||
id: tariff.id,
|
(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,
|
name: tariff.name,
|
||||||
serviceName: SERVICE_LIST.find(
|
serviceName: SERVICE_LIST.find(
|
||||||
(service) => service.serviceKey === findPrivilegeById(tariff.privilegeId)?.serviceKey
|
(service) => service.serviceKey === findPrivilegeById(tariff.privilegeId)?.serviceKey
|
||||||
@ -55,11 +110,16 @@ export default function TariffsDG({ handleSelectionChange }: Props) {
|
|||||||
}`,
|
}`,
|
||||||
amount: tariff.amount,
|
amount: tariff.amount,
|
||||||
type: findPrivilegeById(tariff.privilegeId)?.type === "count" ? "день" : "шт.",
|
type: findPrivilegeById(tariff.privilegeId)?.type === "count" ? "день" : "шт.",
|
||||||
pricePerUnit: tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price,
|
pricePerUnit: tariff.customPricePerUnit
|
||||||
|
? tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price
|
||||||
|
: tariff.price,
|
||||||
isCustomPrice: tariff.customPricePerUnit === undefined ? "Нет" : "Да",
|
isCustomPrice: tariff.customPricePerUnit === undefined ? "Нет" : "Да",
|
||||||
total: tariff.amount * (tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price ?? 0),
|
isDeleted: tariff.isDeleted,
|
||||||
}));
|
total: tariff.amount
|
||||||
|
? tariff.amount * (tariff.customPricePerUnit ?? findPrivilegeById(tariff.privilegeId)?.price ?? 0)
|
||||||
|
: 0,
|
||||||
|
})
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<DataGrid
|
<DataGrid
|
||||||
checkboxSelection={true}
|
checkboxSelection={true}
|
||||||
|
Loading…
Reference in New Issue
Block a user