From ce1c21da3c2c950bbab3b9bd075b578a08b74229 Mon Sep 17 00:00:00 2001 From: IlyaDoronin Date: Thu, 13 Jul 2023 14:29:55 +0300 Subject: [PATCH] fix: patch discounts --- .../DiscountManagement/ControlPanel.tsx | 80 ++++++++++--------- .../DiscountManagement/DiscountDataGrid.tsx | 2 +- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/pages/dashboard/Content/DiscountManagement/ControlPanel.tsx b/src/pages/dashboard/Content/DiscountManagement/ControlPanel.tsx index 40edca4..69fc5e7 100644 --- a/src/pages/dashboard/Content/DiscountManagement/ControlPanel.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/ControlPanel.tsx @@ -1,47 +1,51 @@ -import { Box, Button, IconButton, useTheme } from "@mui/material"; -import { DataGrid, GridColDef, GridRowsProp, GridToolbar } from "@mui/x-data-grid"; -import { formatDiscountFactor } from "@root/kitUI/Cart/calc"; -import { openEditDiscountDialog, setDiscounts, setSelectedDiscountIds, updateDiscount, useDiscountStore } from "@root/stores/discounts"; +import { enqueueSnackbar } from "notistack"; +import { GridSelectionModel } from "@mui/x-data-grid"; +import { Box, Button } from "@mui/material"; + import { changeDiscount } from "@root/api/discounts"; import { findDiscountsById } from "@root/stores/discounts"; -import { GridSelectionModel, GridRowId } from "@mui/x-data-grid"; -import { enqueueSnackbar } from "notistack"; +import { requestDiscounts } from "@root/services/discounts.service"; interface Props { - selectedRows: GridSelectionModel + selectedRows: GridSelectionModel; } -export default function DiscountDataGrid({selectedRows}:Props) { +export default function DiscountDataGrid({ selectedRows }: Props) { + const changeData = async (isActive: boolean) => { + let done = 0; + let fatal = 0; -const changeData = (isActive:boolean) => { - let done = 0 - let fatal = 0 - selectedRows.forEach((id:GridRowId) => { - const discount = findDiscountsById(String(id)) - if (discount) { - discount.Deprecated = isActive - changeDiscount(String(id), discount) - .then(() => { - done += 1 - }) - .catch(() => { - fatal += 1 - }) - .finally(() => { - if (done) enqueueSnackbar("Успешно изменён статус " + done + " скидок") - if (fatal) enqueueSnackbar(fatal + " скидок не изменили статус") - }) - } else { - enqueueSnackbar("Скидка не найдена") - } - }) - -} + for (const id of selectedRows) { + const discount = findDiscountsById(String(id)); - return ( - - - - - ); + if (!discount) { + return enqueueSnackbar("Скидка не найдена"); + } + + try { + await changeDiscount(String(id), { ...discount, Deprecated: isActive }); + + done += 1; + } catch { + fatal += 1; + } + } + + await requestDiscounts(); + + if (done) { + enqueueSnackbar("Успешно изменён статус " + done + " скидок"); + } + + if (fatal) { + enqueueSnackbar(fatal + " скидок не изменили статус"); + } + }; + + return ( + + + + + ); } diff --git a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx index 8363aa8..5c63ce0 100644 --- a/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx +++ b/src/pages/dashboard/Content/DiscountManagement/DiscountDataGrid.tsx @@ -125,7 +125,7 @@ export default function DiscountDataGrid({ selectedRowsHC }: Props) { }, []); const rowBackDicounts: GridRowsProp = realDiscounts - .filter((e) => e.Layer > 0) + .filter(({ Layer }) => Layer > 0) .map((discount) => { console.log( discount.Condition[