refactor stores

This commit is contained in:
nflnkr 2023-03-06 16:22:12 +03:00
parent f9f4ce5fb2
commit da5341b71c
6 changed files with 124 additions and 183 deletions

@ -1,25 +1,25 @@
import create from "zustand";
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { ArrayProps } from "../model/tariff";
import { CartTotal } from "@root/model/cart";
interface CartStore {
cartRowsData: Array<ArrayProps>,
setCartRowsData: (array: Array<ArrayProps>) => void,
cartTotal: CartTotal | null;
setCartTotal: (newCartTotal: CartTotal) => void;
}
export const useCartStore = create<CartStore>()(
devtools(
persist(
// persist(
(set, get) => ({
cartRowsData: [],
setCartRowsData: (array: Array<ArrayProps>) => set({ cartRowsData: array }),
cartTotal: null,
setCartTotal: newCartTotal => set({ cartTotal: newCartTotal })
}),
{
name: "cart-storage",
getStorage: () => localStorage,
}
),
// {
// name: "cart",
// getStorage: () => localStorage,
// }
// ),
{
name: "Cart store"
}

@ -1,34 +1,47 @@
import create from "zustand";
import { GridSelectionModel } from "@mui/x-data-grid";
import { AnyDiscount } from "@root/model/cart";
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { Discount } from "../model/cart";
import { testDiscounts } from "./mocks/discounts";
import { exampleCartValues } from "./mocks/exampleCartValues";
interface DiscountStore {
discountsArray: Array<Discount>,
setDiscountsArray: (array: Array<Discount>) => void,
discountsActiveArray: Array<number>,
setDiscountsActiveArray: (array: Array<number>) => void,
discountsSelectedRowsData: Array<Discount>,
setDiscountsSelectedRowsData: (array: Array<Discount>) => void,
discounts: AnyDiscount[];
selectedDiscountIds: GridSelectionModel,
addDiscounts: (newDiscounts: AnyDiscount[]) => void;
deleteDiscounts: (discountIds: string[]) => void;
activateDiscounts: (discountIds: string[]) => void;
deactivateDiscounts: (discountIds: string[]) => void;
}
export const useDiscountStore = create<DiscountStore>()(
devtools(
persist(
// persist(
(set, get) => ({
discountsArray: testDiscounts,
setDiscountsArray: (array: Array<Discount>) => set({ discountsArray: array }),
discountsActiveArray: [],
setDiscountsActiveArray: (array: Array<number>) => set({ discountsActiveArray: array }),
discountsSelectedRowsData: [],
setDiscountsSelectedRowsData: (array: Array<Discount>) => set({ discountsSelectedRowsData: array }),
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");
}),
{
name: "discount-storage",
getStorage: () => localStorage,
}
),
deactivateDiscounts: discountIds => set(state => {
const filteredDiscounts = state.discounts.filter(discount => discountIds.includes(discount._id));
// TODO deactivate discounts, use immer js?
throw new Error("unimplemented");
}),
}),
// {
// name: "discounts",
// getStorage: () => localStorage,
// }
// ),
{
name: "Discount store"
}

@ -1,60 +1,30 @@
import create from "zustand";
import {Privilege, State} from "@kitUI/types/privileges";
import { Privilege } from "@root/model/tariff";
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { exampleCartValues } from "./mocks/exampleCartValues";
const useStore = create<State>()(
interface PrivilegeStore {
privileges: Privilege[];
addPrivileges: (newPrivileges: Privilege[]) => void;
}
export const usePrivilegeStore = create<PrivilegeStore>()(
devtools(
// persist(
(set, get) => ({
privileges: {
p1: {
"serviceKey": "templategen",
"name": "unlim",
"description":"привилегия безлимитного доступа к шаблонизатору на время. в днях",
"type":"day",
"price": 0.5
},
p2: {
"serviceKey": "templategen",
"name": "gencount",
"description":"привилегия на определённое количество генераций",
"type":"count",
"price": 0.1
},
p3: {
"serviceKey": "squiz",
"name": "unlim",
"description":"привилегия безлимитного доступа к опроснику. в днях",
"type":"day",
"price": 3.0
},
p4: {
"serviceKey": "squiz",
"name": "activequiz",
"description":"привилегия создания ограниченного количества опросов",
"type":"count",
"price": 1.0
},
p5: {
"serviceKey": "dwarfener",
"name": "unlim",
"description":"привилегия безлимитного доступа к сокращателю на время. в днях",
"type":"day",
"price": 0.1
},
p6: {
"serviceKey": "dwarfener",
"name": "abcount",
"description":"привилегия на количество активных ссылок в абтестах",
"type":"count",
"price": 0.7
},
p7: {
"serviceKey": "dwarfener",
"name": "extended",
"description":"привилегия расширенной статистики, в днях",
"type":"day",
"price": 2
},
},
tariffsUpdate: (element:Privilege) => {set((state:State) => ({ privileges: {...state.privileges, ...element} }))}
})
);
export default useStore;
privileges: exampleCartValues.privileges,
addPrivileges: newPrivileges => set(state => (
{ privileges: [...state.privileges, ...newPrivileges] }
)),
}),
// {
// name: "privileges",
// getStorage: () => localStorage,
// }
// ),
{
name: "Privilege store"
}
)
);

@ -1,26 +1,31 @@
import create from "zustand";
import { Promocode } from "@root/model/cart";
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { Promocode } from "../model/cart";
import { testPromocodes } from "./mocks/promocodes";
interface PromocodeStore {
promocodeArray: Array<Promocode>,
setPromocodeArray: (array: Array<Promocode>) => void,
promocodes: Promocode[];
addPromocodes: (newPromocodes: Promocode[]) => void;
deletePromocodes: (promocodeIdsToDelete: string[]) => void;
}
export const usePromocodeStore = create<PromocodeStore>()(
devtools(
persist(
// persist(
(set, get) => ({
promocodeArray: testPromocodes,
setPromocodeArray: (array: Array<Promocode>) => set({ promocodeArray: array }),
promocodes: [],
addPromocodes: newPromocodes => set(state => (
{ promocodes: [...state.promocodes, ...newPromocodes] }
)),
deletePromocodes: promocodeIdsToDelete => set(state => (
{ promocodes: state.promocodes.filter(promocode => !promocodeIdsToDelete.includes(promocode.id)) }
)),
}),
{
name: "promocode-storage",
getStorage: () => localStorage,
}
),
// {
// name: "promocodes",
// getStorage: () => localStorage,
// }
// ),
{
name: "Promocode store"
}

@ -1,63 +0,0 @@
import create from "zustand";
import { persist } from "zustand/middleware"
import { ArrayProps } from "../pages/dashboard/Content/Tariffs/types";
import { PromocodeProps } from "../pages/dashboard/Content/Promocode/types";
import { DiscountProps } from "../pages/dashboard/Content/Discounts/types";
const useStore = create<StoreState>()(
persist(
(set, get) => ({
tariffsArray: [],
tariffsArraySet: (array:Array<ArrayProps>) => set({ tariffsArray: array }),
tariffsSelectedRowsData: [],
tariffsSelectedRowsDataSet: (array:Array<ArrayProps>) => set({ tariffsSelectedRowsData: array }),
cartRowsData: [],
cartRowsDataSet: (array:Array<ArrayProps>) => set({ cartRowsData: array }),
promocodeArray: [],
promocodeArraySet: (array:Array<PromocodeProps>) => set({ promocodeArray: array }),
discountsArray: [],
discountsArraySet: (array:Array<DiscountProps>) => set({ discountsArray: array }),
discountsActiveArray: [],
discountsActiveArraySet: (array:Array<number>) => set({ discountsActiveArray: array }),
discountsSelectedRowsData: [],
discountsSelectedRowsDataSet: (array:Array<DiscountProps>) => set({ discountsSelectedRowsData: array }),
}),
{
name: "arrays-storage",
getStorage: () => localStorage,
}
)
);
export interface StoreState {
tariffsArray: Array<ArrayProps>,
tariffsArraySet: (array:Array<ArrayProps>) => void,
tariffsSelectedRowsData: Array<ArrayProps>,
tariffsSelectedRowsDataSet: (array:Array<ArrayProps>) => void,
cartRowsData: Array<ArrayProps>,
cartRowsDataSet: (array:Array<ArrayProps>) => void,
promocodeArray: Array<PromocodeProps>,
promocodeArraySet: (array:Array<PromocodeProps>) => void,
discountsArray: Array<DiscountProps>,
discountsArraySet: (array:Array<DiscountProps>) => void,
discountsActiveArray: Array<number>,
discountsActiveArraySet: (array:Array<number>) => void,
discountsSelectedRowsData: Array<DiscountProps>,
discountsSelectedRowsDataSet: (array:Array<DiscountProps>) => void,
}
export default useStore;

@ -1,18 +1,34 @@
import create from "zustand";
import { persist } from "zustand/middleware"
import { Tariff } from "@root/model/tariff";
import { create } from "zustand";
import { devtools, persist } from "zustand/middleware";
import { exampleTariffs } from "./mocks/tariffs";
const useStore = create<any>()(
persist((set, get) => ({
tariffs: {},
tariffsUpdate: (element:any) => {set((state:any) => ({ tariffs: {...state.tariffs, ...element} }))},
tariffsClear: () => set({tariffs: {}}),
interface TariffStore {
tariffs: Tariff[];
addTariffs: (newTariffs: Tariff[]) => void;
deleteTariffs: (tariffsToDelete: Tariff[]) => void;
deleteAllTariffs: () => void;
}
export const useTariffStore = create<TariffStore>()(
devtools(
// persist(
(set, get) => ({
tariffs: exampleTariffs,
addTariffs: (newTariffs: Tariff[]) => set(state => ({ tariffs: [...state.tariffs, ...newTariffs] })),
deleteTariffs: tariffsToDelete => set(state => (
{ tariffs: state.tariffs.filter(tariff => !tariffsToDelete.includes(tariff)) }
)),
deleteAllTariffs: () => set({ tariffs: [] }),
}),
// {
// name: "tariffs",
// getStorage: () => localStorage,
// }
// ),
{
name: "tariffs",
getStorage: () => localStorage,
name: "Tariff store"
}
));
export default useStore;
)
);