2023-03-06 13:22:12 +00:00
|
|
|
import { GridSelectionModel } from "@mui/x-data-grid";
|
|
|
|
import { AnyDiscount } from "@root/model/cart";
|
|
|
|
import { create } from "zustand";
|
2023-02-18 13:59:36 +00:00
|
|
|
import { devtools, persist } from "zustand/middleware";
|
2023-03-06 13:22:12 +00:00
|
|
|
import { exampleCartValues } from "./mocks/exampleCartValues";
|
2023-02-18 13:59:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
interface DiscountStore {
|
2023-03-06 13:22:12 +00:00
|
|
|
discounts: AnyDiscount[];
|
|
|
|
selectedDiscountIds: GridSelectionModel,
|
|
|
|
addDiscounts: (newDiscounts: AnyDiscount[]) => void;
|
|
|
|
deleteDiscounts: (discountIds: string[]) => void;
|
|
|
|
activateDiscounts: (discountIds: string[]) => void;
|
|
|
|
deactivateDiscounts: (discountIds: string[]) => void;
|
2023-02-18 13:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useDiscountStore = create<DiscountStore>()(
|
|
|
|
devtools(
|
2023-03-06 13:22:12 +00:00
|
|
|
// persist(
|
2023-02-18 13:59:36 +00:00
|
|
|
(set, get) => ({
|
2023-03-06 13:22:12 +00:00
|
|
|
discounts: exampleCartValues.discounts,
|
|
|
|
selectedDiscountIds: [],
|
|
|
|
addDiscounts: newDiscounts => set(state => ({ discounts: [...state.discounts, ...newDiscounts] })),
|
|
|
|
deleteDiscounts: discountIdsToDelete => set(state => (
|
|
|
|
{ discounts: state.discounts.filter(discount => !discountIdsToDelete.includes(discount._id)) }
|
|
|
|
)),
|
|
|
|
activateDiscounts: discountIds => set(state => {
|
|
|
|
const filteredDiscounts = state.discounts.filter(discount => discountIds.includes(discount._id));
|
|
|
|
|
|
|
|
// TODO activate discounts, use immer js?
|
|
|
|
throw new Error("unimplemented");
|
|
|
|
}),
|
|
|
|
deactivateDiscounts: discountIds => set(state => {
|
|
|
|
const filteredDiscounts = state.discounts.filter(discount => discountIds.includes(discount._id));
|
|
|
|
|
|
|
|
// TODO deactivate discounts, use immer js?
|
|
|
|
throw new Error("unimplemented");
|
|
|
|
}),
|
2023-02-18 13:59:36 +00:00
|
|
|
}),
|
2023-03-06 13:22:12 +00:00
|
|
|
// {
|
|
|
|
// name: "discounts",
|
|
|
|
// getStorage: () => localStorage,
|
|
|
|
// }
|
|
|
|
// ),
|
2023-02-18 13:59:36 +00:00
|
|
|
{
|
|
|
|
name: "Discount store"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|