feat(spa): add wishlist functionality with localStorage and UI integration
This commit is contained in:
1 parent
ca31564ee7
commit
091eeef7d8
5 files changed
+155
-5
No files matched your search
@@ -0,0 +1,33 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
import { Product } from '../types'
|
||||
|
||||
interface WishlistState {
|
||||
items: Product[]
|
||||
addItem: (product: Product) => void
|
||||
removeItem: (productId: number) => void
|
||||
isInWishlist: (productId: number) => boolean
|
||||
}
|
||||
|
||||
export const useWishlistStore = create<WishlistState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
items: [],
|
||||
addItem: (product) => {
|
||||
const { items } = get()
|
||||
if (!items.find(item => item.id === product.id)) {
|
||||
set({ items: [...items, product] })
|
||||
}
|
||||
},
|
||||
removeItem: (productId) => {
|
||||
set({ items: get().items.filter(item => item.id !== productId) })
|
||||
},
|
||||
isInWishlist: (productId) => {
|
||||
return get().items.some(item => item.id === productId)
|
||||
}
|
||||
}),
|
||||
{
|
||||
name: 'homaroma-wishlist',
|
||||
}
|
||||
)
|
||||
)
|
||||
Reference in new issue
Block a user