активация деактивация скидки
This commit is contained in:
parent
81c2582fee
commit
0d2dfe9fcf
@ -133,3 +133,13 @@ export function createDiscountObject({
|
||||
|
||||
return discount;
|
||||
}
|
||||
|
||||
export function changeDiscount (discountId:string, discount:Discount) {
|
||||
return makeRequest<Discount>({
|
||||
url: baseUrl + "/discount/" + discountId,
|
||||
method: "patch",
|
||||
useToken: true,
|
||||
bearer: true,
|
||||
body: discount,
|
||||
});
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
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 { changeDiscount } from "@root/api/discounts";
|
||||
import { findDiscountsById } from "@root/stores/discounts";
|
||||
import { GridSelectionModel, GridRowId } from "@mui/x-data-grid";
|
||||
import { enqueueSnackbar } from "notistack";
|
||||
|
||||
|
||||
interface Props {
|
||||
selectedRows: GridSelectionModel
|
||||
}
|
||||
export default function DiscountDataGrid({selectedRows}:Props) {
|
||||
|
||||
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
|
||||
})
|
||||
if (done) enqueueSnackbar("Успешно изменён статус " + done + " скидок")
|
||||
if (fatal) enqueueSnackbar(fatal + " скидок не изменили статус")
|
||||
}
|
||||
enqueueSnackbar("Скидка не найдена")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<Box width="400px" display="flex" justifyContent="space-between">
|
||||
<Button onClick={() => changeData(false)}>Активировать</Button>
|
||||
<Button onClick={() => changeData(true)}>Деактивировать</Button>
|
||||
</Box>
|
||||
);
|
||||
}
|
@ -6,7 +6,7 @@ import useDiscounts from "@root/utils/hooks/useDiscounts";
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import { deleteDiscount } from "@root/api/discounts";
|
||||
|
||||
import { GridSelectionModel } from "@mui/x-data-grid";
|
||||
|
||||
const columns: GridColDef[] = [
|
||||
// {
|
||||
@ -103,8 +103,10 @@ const layerValue = [
|
||||
"CartPurchasesAmount",
|
||||
"PurchasesAmount",
|
||||
];
|
||||
|
||||
export default function DiscountDataGrid() {
|
||||
interface Props {
|
||||
selectedRowsHC: (array:GridSelectionModel) => void
|
||||
}
|
||||
export default function DiscountDataGrid({selectedRowsHC}:Props) {
|
||||
const theme = useTheme();
|
||||
const selectedDiscountIds = useDiscountStore((state) => state.selectedDiscountIds);
|
||||
const realDiscounts = useDiscountStore(state => state.discounts);
|
||||
@ -133,7 +135,10 @@ export default function DiscountDataGrid() {
|
||||
rows={rowBackDicounts}
|
||||
columns={columns}
|
||||
selectionModel={selectedDiscountIds}
|
||||
onSelectionModelChange={setSelectedDiscountIds}
|
||||
onSelectionModelChange={(array:GridSelectionModel) => {
|
||||
selectedRowsHC(array)
|
||||
setSelectedDiscountIds(array)
|
||||
}}
|
||||
disableSelectionOnClick
|
||||
sx={{
|
||||
color: theme.palette.secondary.main,
|
||||
|
@ -4,10 +4,17 @@ import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
|
||||
import DiscountDataGrid from "./DiscountDataGrid";
|
||||
import CreateDiscount from "./CreateDiscount";
|
||||
import EditDiscountDialog from "./EditDiscountDialog";
|
||||
import ControlPanel from "./ControlPanel";
|
||||
import { useState } from "react";
|
||||
import { GridSelectionModel } from "@mui/x-data-grid";
|
||||
|
||||
|
||||
const DiscountManagement: React.FC = () => {
|
||||
const theme = useTheme();
|
||||
const [selectedRows, setSelectedRows] = useState<GridSelectionModel>([])
|
||||
const selectedRowsHC = (array:GridSelectionModel) => {
|
||||
setSelectedRows(array)
|
||||
}
|
||||
|
||||
return (
|
||||
<LocalizationProvider dateAdapter={AdapterDayjs}>
|
||||
@ -25,8 +32,11 @@ const DiscountManagement: React.FC = () => {
|
||||
СКИДКИ
|
||||
</Typography>
|
||||
<CreateDiscount />
|
||||
<DiscountDataGrid />
|
||||
<DiscountDataGrid
|
||||
selectedRowsHC={selectedRowsHC}
|
||||
/>
|
||||
<EditDiscountDialog />
|
||||
<ControlPanel selectedRows={selectedRows}/>
|
||||
</LocalizationProvider>
|
||||
);
|
||||
};
|
||||
|
@ -29,6 +29,8 @@ export const useDiscountStore = create<DiscountStore>()(
|
||||
|
||||
export const setDiscounts = (discounts: DiscountStore["discounts"]) => useDiscountStore.setState({ discounts });
|
||||
|
||||
export const findDiscountsById = (discountId: string):(Discount| null) => useDiscountStore.getState().discounts.find(discount => discount.ID === discountId) ?? null;
|
||||
|
||||
export const addDiscount = (discount: DiscountStore["discounts"][number]) => useDiscountStore.setState(
|
||||
state => ({ discounts: [...state.discounts, discount] })
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user