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-05-18 20:31:36 +00:00
|
|
|
import { devtools } from "zustand/middleware";
|
2023-03-06 13:22:12 +00:00
|
|
|
import { exampleCartValues } from "./mocks/exampleCartValues";
|
2023-06-23 14:14:32 +00:00
|
|
|
import { Discount } from "@root/model/discount";
|
2023-02-18 13:59:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
interface DiscountStore {
|
2023-06-23 14:14:32 +00:00
|
|
|
discounts: Discount[];
|
2023-03-06 13:22:12 +00:00
|
|
|
selectedDiscountIds: GridSelectionModel,
|
2023-02-18 13:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useDiscountStore = create<DiscountStore>()(
|
2023-06-23 14:14:32 +00:00
|
|
|
devtools(
|
|
|
|
(set, get) => ({
|
|
|
|
discounts: [],
|
|
|
|
selectedDiscountIds: [],
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "Real discount store",
|
|
|
|
enabled: process.env.NODE_ENV === "development"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-07-01 17:33:26 +00:00
|
|
|
export const addDiscounts = (newDiscounts: DiscountStore["discounts"]) => {
|
|
|
|
console.log(useDiscountStore.getState())
|
|
|
|
console.log(newDiscounts)
|
|
|
|
useDiscountStore.setState(state => ({ discounts: [...state.discounts, ...newDiscounts] }));
|
|
|
|
console.log(useDiscountStore.getState())
|
|
|
|
}
|
2023-06-23 14:14:32 +00:00
|
|
|
|
|
|
|
export const setSelectedDiscountIds = (selectedDiscountIds: DiscountStore["selectedDiscountIds"]) => useDiscountStore.setState({ selectedDiscountIds });
|
|
|
|
|
|
|
|
|
|
|
|
/** @deprecated */
|
|
|
|
interface MockDiscountStore {
|
|
|
|
discounts: AnyDiscount[];
|
|
|
|
selectedDiscountIds: GridSelectionModel,
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated */
|
|
|
|
export const useMockDiscountStore = create<MockDiscountStore>()(
|
2023-02-18 13:59:36 +00:00
|
|
|
devtools(
|
2023-05-18 20:31:36 +00:00
|
|
|
(set, get) => ({
|
|
|
|
discounts: exampleCartValues.discounts,
|
|
|
|
selectedDiscountIds: [],
|
|
|
|
}),
|
2023-02-18 13:59:36 +00:00
|
|
|
{
|
|
|
|
name: "Discount store"
|
|
|
|
}
|
|
|
|
)
|
2023-05-18 20:31:36 +00:00
|
|
|
);
|
|
|
|
|
2023-06-23 14:14:32 +00:00
|
|
|
/** @deprecated */
|
|
|
|
export const addMockDiscounts = (newDiscounts: AnyDiscount[]) => useMockDiscountStore.setState(state => ({ discounts: [...state.discounts, ...newDiscounts] }));
|
2023-05-18 20:31:36 +00:00
|
|
|
|
2023-06-23 14:14:32 +00:00
|
|
|
/** @deprecated */
|
|
|
|
export const setMockSelectedDiscountIds = (selectedDiscountIds: MockDiscountStore["selectedDiscountIds"]) => useMockDiscountStore.setState({ selectedDiscountIds });
|
2023-05-18 20:31:36 +00:00
|
|
|
|
2023-06-23 14:14:32 +00:00
|
|
|
/** @deprecated */
|
|
|
|
export const activateMockDiscounts = () => useMockDiscountStore.setState(state => {
|
2023-05-18 20:31:36 +00:00
|
|
|
const discounts: AnyDiscount[] = [];
|
|
|
|
state.discounts.forEach(discount => {
|
|
|
|
if (!state.selectedDiscountIds.includes(discount._id)) return discounts.push(discount);
|
|
|
|
|
|
|
|
discounts.push({
|
|
|
|
...discount,
|
|
|
|
disabled: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return { discounts };
|
|
|
|
});
|
|
|
|
|
2023-06-23 14:14:32 +00:00
|
|
|
/** @deprecated */
|
|
|
|
export const deactivateMockDiscounts = () => useMockDiscountStore.setState(state => {
|
2023-05-18 20:31:36 +00:00
|
|
|
const discounts: AnyDiscount[] = [];
|
|
|
|
state.discounts.forEach(discount => {
|
|
|
|
if (!state.selectedDiscountIds.includes(discount._id)) return discounts.push(discount);
|
|
|
|
|
|
|
|
discounts.push({
|
|
|
|
...discount,
|
|
|
|
disabled: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return { discounts };
|
2023-06-23 14:14:32 +00:00
|
|
|
});
|