97 lines
2.4 KiB
TypeScript
97 lines
2.4 KiB
TypeScript
import { create } from "zustand";
|
||
import { persist } from "zustand/middleware";
|
||
|
||
interface Templ {
|
||
name: string;
|
||
desc: string;
|
||
id: string;
|
||
privelegeid: string;
|
||
amount: number;
|
||
price: number;
|
||
}
|
||
|
||
interface BasketStore {
|
||
templ: Record<string, Templ>;
|
||
squiz: Record<string, Templ>;
|
||
reducer: Record<string, Templ>;
|
||
openDrawer: boolean;
|
||
add: (params: { id: string; obj: Templ; type: "templ" | "squiz" | "reducer" }) => void;
|
||
remove: (type: "templ" | "squiz" | "reducer", id: string) => void;
|
||
open: (boolean: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => void;
|
||
}
|
||
|
||
export const basketStore = create<BasketStore>()(
|
||
persist(
|
||
(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: 390,
|
||
},
|
||
id2: {
|
||
name: "Шаблонизатор",
|
||
desc: "Подписка на месяц",
|
||
id: "id2",
|
||
privelegeid: "2",
|
||
amount: 30,
|
||
price: 290,
|
||
},
|
||
},
|
||
reducer: {},
|
||
openDrawer: false,
|
||
add: ({ id, obj, type }) => {
|
||
const store: Record<string, Templ> = get()[type] || {};
|
||
const newStore = {
|
||
[type]: {
|
||
...store,
|
||
[id]: obj,
|
||
},
|
||
};
|
||
set(() => newStore);
|
||
},
|
||
remove: (type, id: string) => {
|
||
const store: Record<string, Templ> = get()[type] || {};
|
||
|
||
const newStore = Object.entries(store).reduce<Record<string, Templ>>((accamulator, [key, value]) => {
|
||
if (key !== id) {
|
||
accamulator[key] = value;
|
||
}
|
||
|
||
return accamulator;
|
||
}, {});
|
||
|
||
set({ [type]: newStore });
|
||
},
|
||
open: (boolean: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
|
||
set(() => ({ openDrawer: boolean }));
|
||
},
|
||
}),
|
||
{
|
||
name: "basket",
|
||
}
|
||
)
|
||
);
|