refactor stores
This commit is contained in:
parent
f9f4ce5fb2
commit
da5341b71c
@ -1,25 +1,25 @@
|
|||||||
import create from "zustand";
|
import { create } from "zustand";
|
||||||
import { devtools, persist } from "zustand/middleware";
|
import { devtools, persist } from "zustand/middleware";
|
||||||
import { ArrayProps } from "../model/tariff";
|
import { CartTotal } from "@root/model/cart";
|
||||||
|
|
||||||
|
|
||||||
interface CartStore {
|
interface CartStore {
|
||||||
cartRowsData: Array<ArrayProps>,
|
cartTotal: CartTotal | null;
|
||||||
setCartRowsData: (array: Array<ArrayProps>) => void,
|
setCartTotal: (newCartTotal: CartTotal) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useCartStore = create<CartStore>()(
|
export const useCartStore = create<CartStore>()(
|
||||||
devtools(
|
devtools(
|
||||||
persist(
|
// persist(
|
||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
cartRowsData: [],
|
cartTotal: null,
|
||||||
setCartRowsData: (array: Array<ArrayProps>) => set({ cartRowsData: array }),
|
setCartTotal: newCartTotal => set({ cartTotal: newCartTotal })
|
||||||
}),
|
}),
|
||||||
{
|
// {
|
||||||
name: "cart-storage",
|
// name: "cart",
|
||||||
getStorage: () => localStorage,
|
// getStorage: () => localStorage,
|
||||||
}
|
// }
|
||||||
),
|
// ),
|
||||||
{
|
{
|
||||||
name: "Cart store"
|
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 { devtools, persist } from "zustand/middleware";
|
||||||
import { Discount } from "../model/cart";
|
import { exampleCartValues } from "./mocks/exampleCartValues";
|
||||||
import { testDiscounts } from "./mocks/discounts";
|
|
||||||
|
|
||||||
|
|
||||||
interface DiscountStore {
|
interface DiscountStore {
|
||||||
discountsArray: Array<Discount>,
|
discounts: AnyDiscount[];
|
||||||
setDiscountsArray: (array: Array<Discount>) => void,
|
selectedDiscountIds: GridSelectionModel,
|
||||||
discountsActiveArray: Array<number>,
|
addDiscounts: (newDiscounts: AnyDiscount[]) => void;
|
||||||
setDiscountsActiveArray: (array: Array<number>) => void,
|
deleteDiscounts: (discountIds: string[]) => void;
|
||||||
discountsSelectedRowsData: Array<Discount>,
|
activateDiscounts: (discountIds: string[]) => void;
|
||||||
setDiscountsSelectedRowsData: (array: Array<Discount>) => void,
|
deactivateDiscounts: (discountIds: string[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useDiscountStore = create<DiscountStore>()(
|
export const useDiscountStore = create<DiscountStore>()(
|
||||||
devtools(
|
devtools(
|
||||||
persist(
|
// persist(
|
||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
discountsArray: testDiscounts,
|
discounts: exampleCartValues.discounts,
|
||||||
setDiscountsArray: (array: Array<Discount>) => set({ discountsArray: array }),
|
selectedDiscountIds: [],
|
||||||
discountsActiveArray: [],
|
addDiscounts: newDiscounts => set(state => ({ discounts: [...state.discounts, ...newDiscounts] })),
|
||||||
setDiscountsActiveArray: (array: Array<number>) => set({ discountsActiveArray: array }),
|
deleteDiscounts: discountIdsToDelete => set(state => (
|
||||||
discountsSelectedRowsData: [],
|
{ discounts: state.discounts.filter(discount => !discountIdsToDelete.includes(discount._id)) }
|
||||||
setDiscountsSelectedRowsData: (array: Array<Discount>) => set({ discountsSelectedRowsData: array }),
|
)),
|
||||||
|
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 => {
|
||||||
name: "discount-storage",
|
const filteredDiscounts = state.discounts.filter(discount => discountIds.includes(discount._id));
|
||||||
getStorage: () => localStorage,
|
|
||||||
}
|
// TODO deactivate discounts, use immer js?
|
||||||
),
|
throw new Error("unimplemented");
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
// {
|
||||||
|
// name: "discounts",
|
||||||
|
// getStorage: () => localStorage,
|
||||||
|
// }
|
||||||
|
// ),
|
||||||
{
|
{
|
||||||
name: "Discount store"
|
name: "Discount store"
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,30 @@
|
|||||||
import create from "zustand";
|
import { Privilege } from "@root/model/tariff";
|
||||||
import {Privilege, State} from "@kitUI/types/privileges";
|
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) => ({
|
(set, get) => ({
|
||||||
privileges: {
|
privileges: exampleCartValues.privileges,
|
||||||
p1: {
|
addPrivileges: newPrivileges => set(state => (
|
||||||
"serviceKey": "templategen",
|
{ privileges: [...state.privileges, ...newPrivileges] }
|
||||||
"name": "unlim",
|
)),
|
||||||
"description":"привилегия безлимитного доступа к шаблонизатору на время. в днях",
|
}),
|
||||||
"type":"day",
|
// {
|
||||||
"price": 0.5
|
// name: "privileges",
|
||||||
},
|
// getStorage: () => localStorage,
|
||||||
p2: {
|
// }
|
||||||
"serviceKey": "templategen",
|
// ),
|
||||||
"name": "gencount",
|
{
|
||||||
"description":"привилегия на определённое количество генераций",
|
name: "Privilege store"
|
||||||
"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;
|
|
@ -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 { devtools, persist } from "zustand/middleware";
|
||||||
import { Promocode } from "../model/cart";
|
|
||||||
import { testPromocodes } from "./mocks/promocodes";
|
|
||||||
|
|
||||||
|
|
||||||
interface PromocodeStore {
|
interface PromocodeStore {
|
||||||
promocodeArray: Array<Promocode>,
|
promocodes: Promocode[];
|
||||||
setPromocodeArray: (array: Array<Promocode>) => void,
|
addPromocodes: (newPromocodes: Promocode[]) => void;
|
||||||
|
deletePromocodes: (promocodeIdsToDelete: string[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const usePromocodeStore = create<PromocodeStore>()(
|
export const usePromocodeStore = create<PromocodeStore>()(
|
||||||
devtools(
|
devtools(
|
||||||
persist(
|
// persist(
|
||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
promocodeArray: testPromocodes,
|
promocodes: [],
|
||||||
setPromocodeArray: (array: Array<Promocode>) => set({ promocodeArray: array }),
|
addPromocodes: newPromocodes => set(state => (
|
||||||
|
{ promocodes: [...state.promocodes, ...newPromocodes] }
|
||||||
|
)),
|
||||||
|
deletePromocodes: promocodeIdsToDelete => set(state => (
|
||||||
|
{ promocodes: state.promocodes.filter(promocode => !promocodeIdsToDelete.includes(promocode.id)) }
|
||||||
|
)),
|
||||||
}),
|
}),
|
||||||
{
|
// {
|
||||||
name: "promocode-storage",
|
// name: "promocodes",
|
||||||
getStorage: () => localStorage,
|
// getStorage: () => localStorage,
|
||||||
}
|
// }
|
||||||
),
|
// ),
|
||||||
{
|
{
|
||||||
name: "Promocode store"
|
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 { Tariff } from "@root/model/tariff";
|
||||||
import { persist } from "zustand/middleware"
|
import { create } from "zustand";
|
||||||
|
import { devtools, persist } from "zustand/middleware";
|
||||||
|
import { exampleTariffs } from "./mocks/tariffs";
|
||||||
|
|
||||||
|
|
||||||
const useStore = create<any>()(
|
interface TariffStore {
|
||||||
persist((set, get) => ({
|
tariffs: Tariff[];
|
||||||
tariffs: {},
|
addTariffs: (newTariffs: Tariff[]) => void;
|
||||||
tariffsUpdate: (element:any) => {set((state:any) => ({ tariffs: {...state.tariffs, ...element} }))},
|
deleteTariffs: (tariffsToDelete: Tariff[]) => void;
|
||||||
tariffsClear: () => set({tariffs: {}}),
|
deleteAllTariffs: () => void;
|
||||||
}),
|
|
||||||
{
|
|
||||||
name: "tariffs",
|
|
||||||
getStorage: () => localStorage,
|
|
||||||
}
|
}
|
||||||
));
|
|
||||||
|
|
||||||
|
export const useTariffStore = create<TariffStore>()(
|
||||||
export default useStore;
|
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: "Tariff store"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
Loading…
Reference in New Issue
Block a user