From da5341b71c1db1db049c2e65b2d175931dc03f1b Mon Sep 17 00:00:00 2001 From: nflnkr Date: Mon, 6 Mar 2023 16:22:12 +0300 Subject: [PATCH] refactor stores --- src/stores/cart.ts | 24 +++++------ src/stores/discounts.ts | 55 +++++++++++++++---------- src/stores/privileges.ts | 88 +++++++++++++--------------------------- src/stores/promocodes.ts | 31 ++++++++------ src/stores/store.ts | 63 ---------------------------- src/stores/tariffs.ts | 46 ++++++++++++++------- 6 files changed, 124 insertions(+), 183 deletions(-) delete mode 100644 src/stores/store.ts diff --git a/src/stores/cart.ts b/src/stores/cart.ts index de9f3a8..9673ca8 100644 --- a/src/stores/cart.ts +++ b/src/stores/cart.ts @@ -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, - setCartRowsData: (array: Array) => void, + cartTotal: CartTotal | null; + setCartTotal: (newCartTotal: CartTotal) => void; } export const useCartStore = create()( devtools( - persist( + // persist( (set, get) => ({ - cartRowsData: [], - setCartRowsData: (array: Array) => set({ cartRowsData: array }), + cartTotal: null, + setCartTotal: newCartTotal => set({ cartTotal: newCartTotal }) }), - { - name: "cart-storage", - getStorage: () => localStorage, - } - ), + // { + // name: "cart", + // getStorage: () => localStorage, + // } + // ), { name: "Cart store" } diff --git a/src/stores/discounts.ts b/src/stores/discounts.ts index d946a74..f1450bf 100644 --- a/src/stores/discounts.ts +++ b/src/stores/discounts.ts @@ -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, - setDiscountsArray: (array: Array) => void, - discountsActiveArray: Array, - setDiscountsActiveArray: (array: Array) => void, - discountsSelectedRowsData: Array, - setDiscountsSelectedRowsData: (array: Array) => 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()( devtools( - persist( + // persist( (set, get) => ({ - discountsArray: testDiscounts, - setDiscountsArray: (array: Array) => set({ discountsArray: array }), - discountsActiveArray: [], - setDiscountsActiveArray: (array: Array) => set({ discountsActiveArray: array }), - discountsSelectedRowsData: [], - setDiscountsSelectedRowsData: (array: Array) => 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"); + }), + 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: "discount-storage", - getStorage: () => localStorage, - } - ), + // { + // name: "discounts", + // getStorage: () => localStorage, + // } + // ), { name: "Discount store" } diff --git a/src/stores/privileges.ts b/src/stores/privileges.ts index 1443f56..fe2ab5a 100644 --- a/src/stores/privileges.ts +++ b/src/stores/privileges.ts @@ -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()( - (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; \ No newline at end of file + +interface PrivilegeStore { + privileges: Privilege[]; + addPrivileges: (newPrivileges: Privilege[]) => void; +} + +export const usePrivilegeStore = create()( + devtools( + // persist( + (set, get) => ({ + privileges: exampleCartValues.privileges, + addPrivileges: newPrivileges => set(state => ( + { privileges: [...state.privileges, ...newPrivileges] } + )), + }), + // { + // name: "privileges", + // getStorage: () => localStorage, + // } + // ), + { + name: "Privilege store" + } + ) +); \ No newline at end of file diff --git a/src/stores/promocodes.ts b/src/stores/promocodes.ts index 303d881..2b15680 100644 --- a/src/stores/promocodes.ts +++ b/src/stores/promocodes.ts @@ -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, - setPromocodeArray: (array: Array) => void, + promocodes: Promocode[]; + addPromocodes: (newPromocodes: Promocode[]) => void; + deletePromocodes: (promocodeIdsToDelete: string[]) => void; } export const usePromocodeStore = create()( devtools( - persist( + // persist( (set, get) => ({ - promocodeArray: testPromocodes, - setPromocodeArray: (array: Array) => 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" } diff --git a/src/stores/store.ts b/src/stores/store.ts deleted file mode 100644 index 1b166fa..0000000 --- a/src/stores/store.ts +++ /dev/null @@ -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()( - persist( - (set, get) => ({ - tariffsArray: [], - tariffsArraySet: (array:Array) => set({ tariffsArray: array }), - - tariffsSelectedRowsData: [], - tariffsSelectedRowsDataSet: (array:Array) => set({ tariffsSelectedRowsData: array }), - - cartRowsData: [], - cartRowsDataSet: (array:Array) => set({ cartRowsData: array }), - - promocodeArray: [], - promocodeArraySet: (array:Array) => set({ promocodeArray: array }), - - discountsArray: [], - discountsArraySet: (array:Array) => set({ discountsArray: array }), - - discountsActiveArray: [], - discountsActiveArraySet: (array:Array) => set({ discountsActiveArray: array }), - - discountsSelectedRowsData: [], - discountsSelectedRowsDataSet: (array:Array) => set({ discountsSelectedRowsData: array }), - }), - { - name: "arrays-storage", - getStorage: () => localStorage, - } - ) -); - -export interface StoreState { - tariffsArray: Array, - tariffsArraySet: (array:Array) => void, - - tariffsSelectedRowsData: Array, - tariffsSelectedRowsDataSet: (array:Array) => void, - - cartRowsData: Array, - cartRowsDataSet: (array:Array) => void, - - promocodeArray: Array, - promocodeArraySet: (array:Array) => void, - - discountsArray: Array, - discountsArraySet: (array:Array) => void, - - discountsActiveArray: Array, - discountsActiveArraySet: (array:Array) => void, - - discountsSelectedRowsData: Array, - discountsSelectedRowsDataSet: (array:Array) => void, -} - - -export default useStore; \ No newline at end of file diff --git a/src/stores/tariffs.ts b/src/stores/tariffs.ts index df8d0ea..81e6420 100644 --- a/src/stores/tariffs.ts +++ b/src/stores/tariffs.ts @@ -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()( - persist((set, get) => ({ - tariffs: {}, - tariffsUpdate: (element:any) => {set((state:any) => ({ tariffs: {...state.tariffs, ...element} }))}, - tariffsClear: () => set({tariffs: {}}), - }), - { - name: "tariffs", - getStorage: () => localStorage, - } -)); +interface TariffStore { + tariffs: Tariff[]; + addTariffs: (newTariffs: Tariff[]) => void; + deleteTariffs: (tariffsToDelete: Tariff[]) => void; + deleteAllTariffs: () => void; +} - -export default useStore; \ No newline at end of file +export const useTariffStore = create()( + 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" + } + ) +); \ No newline at end of file