36 lines
845 B
TypeScript
36 lines
845 B
TypeScript
import { create } from 'zustand';
|
|
|
|
interface CompareItem {
|
|
slug: string;
|
|
name: string;
|
|
brand: string;
|
|
price: number | null;
|
|
image: string;
|
|
specs: Record<string, string>;
|
|
}
|
|
|
|
interface CompareState {
|
|
items: CompareItem[];
|
|
addItem: (item: CompareItem) => void;
|
|
removeItem: (slug: string) => void;
|
|
clearAll: () => void;
|
|
isAdded: (slug: string) => boolean;
|
|
}
|
|
|
|
export const useCompareStore = create<CompareState>()((set, get) => ({
|
|
items: [],
|
|
|
|
addItem: (item) => {
|
|
const { items } = get();
|
|
if (items.length >= 3 || items.find((i) => i.slug === item.slug)) return;
|
|
set({ items: [...items, item] });
|
|
},
|
|
|
|
removeItem: (slug) =>
|
|
set((state) => ({ items: state.items.filter((i) => i.slug !== slug) })),
|
|
|
|
clearAll: () => set({ items: [] }),
|
|
|
|
isAdded: (slug) => get().items.some((i) => i.slug === slug),
|
|
}));
|