55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { GridSelectionModel } from "@mui/x-data-grid";
|
|
import { AnyDiscount } from "@root/model/cart";
|
|
import { create } from "zustand";
|
|
import { devtools } from "zustand/middleware";
|
|
import { exampleCartValues } from "./mocks/exampleCartValues";
|
|
|
|
|
|
interface DiscountStore {
|
|
discounts: AnyDiscount[];
|
|
selectedDiscountIds: GridSelectionModel,
|
|
}
|
|
|
|
export const useDiscountStore = create<DiscountStore>()(
|
|
devtools(
|
|
(set, get) => ({
|
|
discounts: exampleCartValues.discounts,
|
|
selectedDiscountIds: [],
|
|
}),
|
|
{
|
|
name: "Discount store"
|
|
}
|
|
)
|
|
);
|
|
|
|
export const addDiscounts = (newDiscounts: AnyDiscount[]) => useDiscountStore.setState(state => ({ discounts: [...state.discounts, ...newDiscounts] }));
|
|
|
|
export const setSelectedDiscountIds = (selectedDiscountIds: DiscountStore["selectedDiscountIds"]) => useDiscountStore.setState({ selectedDiscountIds });
|
|
|
|
export const activateDiscounts = () => useDiscountStore.setState(state => {
|
|
const discounts: AnyDiscount[] = [];
|
|
state.discounts.forEach(discount => {
|
|
if (!state.selectedDiscountIds.includes(discount._id)) return discounts.push(discount);
|
|
|
|
discounts.push({
|
|
...discount,
|
|
disabled: false,
|
|
});
|
|
});
|
|
|
|
return { discounts };
|
|
});
|
|
|
|
export const deactivateDiscounts = () => useDiscountStore.setState(state => {
|
|
const discounts: AnyDiscount[] = [];
|
|
state.discounts.forEach(discount => {
|
|
if (!state.selectedDiscountIds.includes(discount._id)) return discounts.push(discount);
|
|
|
|
discounts.push({
|
|
...discount,
|
|
disabled: true,
|
|
});
|
|
});
|
|
|
|
return { discounts };
|
|
}); |