front-hub/src/stores/BasketStore.ts

71 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-03-20 19:46:28 +00:00
import { create } from "zustand";
import { persist } from "zustand/middleware";
2023-03-21 18:52:19 +00:00
export const basketStore = create<any>()((set, get) => ({
templ: {
id1: {
name: "Шаблонизатор",
desc: "Дисковое хранилище 5 гб",
id: "id1",
privelegeid: "1",
amount: 5,
price: 390,
},
id2: {
name: "Шаблонизатор",
desc: "Подписка на месяц",
id: "id2",
privelegeid: "2",
amount: 30,
price: 290,
},
},
squiz: {
id1: {
name: "Квиз конструктор",
desc: "Дисковое хранилище 5 гб",
id: "id1",
privelegeid: "1",
amount: 5,
price: 300,
},
id2: {
name: "Квиз конструктор",
desc: "Подписка на месяц",
id: "id2",
privelegeid: "2",
amount: 30,
price: 90,
},
},
reducer: {},
2023-03-24 15:30:58 +00:00
openDrawer: false,
2023-03-22 11:51:16 +00:00
add: ({ id, obj, type }: any) => {
const store: any = get()[type] || {};
const newStore = {
2023-03-22 11:51:16 +00:00
[type]: {
...store,
2023-03-22 11:51:16 +00:00
[id]: obj,
},
};
set(() => newStore);
},
2023-03-23 12:03:08 +00:00
remove: (type: string, id: string) => {
2023-03-22 11:51:16 +00:00
const store: any = get()[type] || {};
2023-03-20 22:06:09 +00:00
2023-03-22 11:51:16 +00:00
const newStore = Object.entries(store).reduce<any>((accamulator, [key, value], index, array) => {
if (key !== id) {
accamulator[key] = value;
2023-03-21 18:52:19 +00:00
}
2023-03-22 11:51:16 +00:00
return accamulator;
}, {});
set({ [type]: newStore });
2023-03-21 18:52:19 +00:00
},
2023-03-24 15:30:58 +00:00
open: (boolean: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
set(() => ({ openDrawer: boolean }));
},
2023-03-21 18:52:19 +00:00
}));