31 lines
622 B
TypeScript
31 lines
622 B
TypeScript
import { useStorage } from '@vueuse/core'
|
|
|
|
export interface ICartItem {
|
|
variation_id: number
|
|
}
|
|
|
|
export interface ICart {
|
|
line_items: ICartItem[]
|
|
}
|
|
|
|
export const useCart = () => {
|
|
const cart = useStorage<ICart>('cart', { line_items: [] })
|
|
|
|
const cartAddItem = (item: ICartItem) => {
|
|
cart.value.line_items.push(item)
|
|
}
|
|
|
|
const cartRemoveItem = (item: ICartItem) => {
|
|
cart?.value?.line_items
|
|
?.splice(cart?.value?.line_items
|
|
?.findIndex((cartItem: ICartItem) =>
|
|
cartItem?.variation_id === item?.variation_id), 1)
|
|
}
|
|
|
|
return {
|
|
cart,
|
|
cartAddItem,
|
|
cartRemoveItem,
|
|
}
|
|
}
|