another one
This commit is contained in:
@@ -1,32 +1,27 @@
|
||||
"use client"
|
||||
|
||||
import React, { createContext, useContext, useState, useCallback, useEffect } from 'react'
|
||||
import { CartItem, saveCart, getCart, clearCart } from '@/lib/cartStorage'
|
||||
|
||||
type CartItem = {
|
||||
id: number
|
||||
title: string
|
||||
price: number
|
||||
quantity: number
|
||||
}
|
||||
import type React from "react"
|
||||
import { createContext, useContext, useState, useCallback, useEffect } from "react"
|
||||
import { type CartItem, saveCart, getCart, clearCart } from "@/lib/cartStorage"
|
||||
|
||||
type CartContextType = {
|
||||
items: CartItem[]
|
||||
addToCart: (item: Omit<CartItem, 'quantity'>, quantity?: number) => void
|
||||
addToCart: (item: Omit<CartItem, "quantity">, quantity?: number) => void
|
||||
removeFromCart: (id: number) => void
|
||||
removeAllFromCart: (id: number) => void
|
||||
updateQuantity: (id: number, quantity: number) => void
|
||||
clearCart: () => void
|
||||
getTotalItems: () => number
|
||||
getTotalQuantity: () => number
|
||||
getTotalUniqueItems: () => number
|
||||
}
|
||||
|
||||
const CartContext = createContext<CartContextType | undefined>(undefined)
|
||||
|
||||
export const useCart = () => {
|
||||
const context = useContext(CartContext)
|
||||
if (!context) {
|
||||
throw new Error('useCart must be used within a CartProvider')
|
||||
if (context === undefined) {
|
||||
throw new Error("useCart must be used within a CartProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
@@ -45,70 +40,69 @@ export const CartProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
saveCart(items)
|
||||
}, [items])
|
||||
|
||||
const addToCart = useCallback((newItem: Omit<CartItem, 'quantity'>, quantity: number = 1) => {
|
||||
setItems(currentItems => {
|
||||
const existingItem = currentItems.find(item => item.id === newItem.id)
|
||||
const addToCart = useCallback((newItem: Omit<CartItem, "quantity">, quantity = 1) => {
|
||||
setItems((currentItems) => {
|
||||
const existingItem = currentItems.find((item) => item.id === newItem.id)
|
||||
if (existingItem) {
|
||||
// If item exists, only update quantity
|
||||
return currentItems.map(item =>
|
||||
item.id === newItem.id
|
||||
? { ...item, quantity: item.quantity + quantity }
|
||||
: item
|
||||
return currentItems.map((item) =>
|
||||
item.id === newItem.id ? { ...item, quantity: item.quantity + quantity } : item,
|
||||
)
|
||||
}
|
||||
// If item doesn't exist, add it with the specified quantity
|
||||
return [...currentItems, { ...newItem, quantity }]
|
||||
})
|
||||
}, [])
|
||||
|
||||
const removeFromCart = useCallback((id: number) => {
|
||||
setItems(currentItems => {
|
||||
const existingItem = currentItems.find(item => item.id === id)
|
||||
setItems((currentItems) => {
|
||||
const existingItem = currentItems.find((item) => item.id === id)
|
||||
if (existingItem && existingItem.quantity > 1) {
|
||||
return currentItems.map(item =>
|
||||
item.id === id ? { ...item, quantity: item.quantity - 1 } : item
|
||||
)
|
||||
return currentItems.map((item) => (item.id === id ? { ...item, quantity: item.quantity - 1 } : item))
|
||||
}
|
||||
return currentItems.filter(item => item.id !== id)
|
||||
return currentItems.filter((item) => item.id !== id)
|
||||
})
|
||||
}, [])
|
||||
|
||||
const removeAllFromCart = useCallback((id: number) => {
|
||||
setItems(currentItems => currentItems.filter(item => item.id !== id))
|
||||
setItems((currentItems) => currentItems.filter((item) => item.id !== id))
|
||||
}, [])
|
||||
|
||||
const updateQuantity = useCallback((id: number, quantity: number) => {
|
||||
setItems(currentItems =>
|
||||
currentItems.map(item =>
|
||||
item.id === id ? { ...item, quantity: Math.max(1, quantity) } : item
|
||||
)
|
||||
setItems((currentItems) =>
|
||||
currentItems.map((item) => (item.id === id ? { ...item, quantity: Math.max(1, quantity) } : item)),
|
||||
)
|
||||
}, [])
|
||||
|
||||
const clearCart = useCallback(() => {
|
||||
const clearCartItems = useCallback(() => {
|
||||
setItems([])
|
||||
clearCart()
|
||||
}, [])
|
||||
|
||||
const getTotalItems = useCallback(() => {
|
||||
return items.length;
|
||||
}, [items]);
|
||||
return items.length
|
||||
}, [items])
|
||||
|
||||
const getTotalQuantity = useCallback(() => {
|
||||
return items.reduce((total, item) => total + item.quantity, 0);
|
||||
}, [items]);
|
||||
return items.reduce((total, item) => total + item.quantity, 0)
|
||||
}, [items])
|
||||
|
||||
const getTotalUniqueItems = useCallback(() => {
|
||||
return items.length
|
||||
}, [items])
|
||||
|
||||
return (
|
||||
<CartContext.Provider value={{
|
||||
items,
|
||||
addToCart,
|
||||
removeFromCart,
|
||||
removeAllFromCart,
|
||||
updateQuantity,
|
||||
clearCart,
|
||||
getTotalItems,
|
||||
getTotalQuantity
|
||||
}}>
|
||||
<CartContext.Provider
|
||||
value={{
|
||||
items,
|
||||
addToCart,
|
||||
removeFromCart,
|
||||
removeAllFromCart,
|
||||
updateQuantity,
|
||||
clearCart: clearCartItems,
|
||||
getTotalItems,
|
||||
getTotalQuantity,
|
||||
getTotalUniqueItems,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</CartContext.Provider>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user