Исправил удаление тарифов

This commit is contained in:
ArtChaos189 2023-06-14 13:04:04 +03:00
parent 4904775397
commit 0eea854b15
2 changed files with 105 additions and 53 deletions

@ -5,41 +5,64 @@ import Typography from "@mui/material/Typography";
import Modal from "@mui/material/Modal";
import TextField from "@mui/material/TextField";
interface Privilege {
name: string;
privilegeId: string;
serviceKey: string;
amount: number;
description: string;
price: number;
type: string;
value: string;
updatedAt: string;
_id: string;
}
type EditModalProps = {
open: boolean;
handleClose: () => void;
errorEdit: boolean;
tariffEdit: (tarifIid: string, tariffName: string, tariffPrice: string) => Promise<void>;
tariff:
tariffEdit: (tarifIid: string, tariffName: string, tariffPrice: string, privilege: Privilege) => Promise<void>;
selectedTariffPrivilege: string | Privilege[];
tariff?:
| {
id: string;
name: string;
serviceName: "Шаблонизатор документов" | "Опросник" | "Аналитика сокращателя" | undefined;
privilege: string;
amount: number;
type: string;
pricePerUnit: number;
isCustomPrice: string;
isDeleted: boolean;
privilege: string | Privilege[];
amount: number | undefined;
type?: string;
value?: string;
pricePerUnit: number | undefined;
total: number;
}
| undefined;
};
export default function EditModal({ open, handleClose, errorEdit, tariffEdit, tariff }: EditModalProps) {
export default function EditModal({
open,
handleClose,
errorEdit,
tariffEdit,
tariff,
selectedTariffPrivilege,
}: EditModalProps) {
const [editOpen, setEditOpen] = useState(false);
const [name, setName] = useState("");
const [price, setPrice] = useState("");
const [privilege] = selectedTariffPrivilege as Privilege[];
const updateTariffDetails = () => {
if (!tariff || errorEdit) {
return;
}
tariffEdit(tariff.id, name, price);
tariffEdit(tariff.id, name, price, privilege);
handleClose();
};
return (
<div>
<Modal

@ -21,6 +21,34 @@ interface Props {
handleSelectionChange: (selectionModel: GridSelectionModel) => void;
}
interface Privilege {
name: string;
privilegeId: string;
serviceKey: string;
amount: number;
description: string;
price: number;
type: string;
value: string;
updatedAt: string;
_id: string;
}
interface MergedTariff {
_id: string;
id: string;
name: string;
privilegeId: string;
serviceName: string;
price: number;
isCustom: boolean;
createdAt: string;
isDeleted: boolean;
amount?: number;
customPricePerUnit?: number;
privilegies: Privilege[];
}
export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Props) {
const exampleTariffs = useTariffStore((state) => state.tariffs);
const [tariffs, setTariffs] = useState<any>();
@ -32,9 +60,9 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
const [errorEdit, setErrorEdit] = useState(false);
const [errorDelete, setErrorDelete] = useState(false);
const mergeTariffs = [...exampleTariffs, ...(tariffs?.tariffs ?? [])];
const mergeTariffs: MergedTariff[] = [...exampleTariffs, ...(tariffs?.tariffs ?? [])];
const tariffEdit = async (tarifIid: string, tariffName: string, tariffPrice: string) => {
const tariffEdit = async (tarifIid: string, tariffName: string, tariffPrice: string, privilege: Privilege) => {
try {
await axios({
method: "put",
@ -48,14 +76,14 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
isCustom: true,
privilegies: [
{
name: "Количество попыток использования",
privilegeId: "507f1f77bcf86cd799439011",
serviceKey: "docx-templater-service",
description: "Количество попыток использования",
type: "count",
value: "200",
price: 12300,
amount: 300,
name: privilege.name,
privilegeId: privilege.privilegeId,
serviceKey: privilege.serviceKey,
description: privilege.description,
type: privilege.type,
value: privilege.value,
price: privilege.price,
amount: privilege.amount,
},
],
},
@ -67,6 +95,11 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
};
const tariffDelete = async (tarifIid: string) => {
if (exampleTariffs.find((tariff) => tariff.id === tarifIid)) {
deleteTariffs(tarifIid);
return;
}
try {
await axios({
method: "delete",
@ -104,7 +137,7 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
{ field: "id", headerName: "ID", width: 100 },
{ field: "name", headerName: "Название тарифа", width: 150 },
{ field: "serviceName", headerName: "Сервис", width: 150 }, //инфо из гитлаба.
{ field: "privilege", headerName: "Привелегия", width: 150 },
{ field: "privilegeName", headerName: "Привелегия", width: 150 },
{ field: "amount", headerName: "Количество", width: 110 },
{ field: "type", headerName: "Единица", width: 100 },
{ field: "pricePerUnit", headerName: "Цена за ед.", width: 100 },
@ -129,42 +162,37 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
},
];
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,
})
);
const privilegiesName = (array: Privilege[]) => {
const name = array.map(({ name }) => name);
return name[0];
};
const gridData = mergeTariffs.map((tariff) => ({
id: tariff._id ? tariff._id : tariff.id,
name: tariff.name,
serviceName: SERVICE_LIST.find(
(service) => service.serviceKey === findPrivilegeById(tariff.privilegeId)?.serviceKey
)?.displayName,
privilegeName: tariff.privilegies
? privilegiesName(tariff.privilegies)
: `(${tariff.privilegeId}) ${findPrivilegeById(tariff.privilegeId)?.description ?? "Привилегия не найдена"}`,
privilege: tariff.privilegies ? tariff.privilegies : [],
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,
}));
const selectedTariff = gridData.find((tariff) => tariff.id === selectedTariffs[0]);
const selectedTariffName = selectedTariff ? selectedTariff.name : "";
console.log(selectedTariff);
const selectedTariffPrivilege = selectedTariff ? selectedTariff.privilege : [];
return (
<>
@ -198,6 +226,7 @@ export default function TariffsDG({ selectedTariffs, handleSelectionChange }: Pr
tariffEdit={tariffEdit}
errorEdit={errorEdit}
tariff={selectedTariff}
selectedTariffPrivilege={selectedTariffPrivilege}
open={openEditModal}
handleClose={() => setOpenEditModal(false)}
/>