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

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

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