diff --git a/src/app/context/ShoppingCartContext.tsx b/src/app/context/ShoppingCartContext.tsx deleted file mode 100644 index aa1944d8..00000000 --- a/src/app/context/ShoppingCartContext.tsx +++ /dev/null @@ -1,98 +0,0 @@ -import { createContext, ReactNode, useContext, useState, useMemo } from "react" -import { useLocalStorage } from "../hooks/useLocalStorage" - -type ShoppingCartProviderProps = { - children: ReactNode -} - -type CartItem = { - id: number - quantity: number -} - -type ShoppingCartContext = { - openCart: () => void - closeCart: () => void - getItemQuantity: (id: number) => number - increaseCartQuantity: (id: number) => void - decreaseCartQuantity: (id: number) => void - removeFromCart: (id: number) => void - cartQuantity: number - cartItems: CartItem[] -} - -const ShoppingCartContext = createContext({} as ShoppingCartContext) - -export function useShoppingCart() { - return useContext(ShoppingCartContext) -} -export function ShoppingCartProvider({ children }: ShoppingCartProviderProps) { - const [isOpen, setIsOpen] = useState(false) - const [cartItems, setCartItems] = useLocalStorage( - "shopping-cart", - [] - ) - - const cartQuantity = cartItems.reduce( - (quantity, item) => item.quantity + quantity, - 0 - ) - - const openCart = () => setIsOpen(true) - const closeCart = () => setIsOpen(false) - function getItemQuantity(id: number) { - return cartItems.find(item => item.id === id)?.quantity || 0 - } - function increaseCartQuantity(id: number) { - setCartItems(currItems => { - if (currItems.find(item => item.id === id) == null) { - return [...currItems, { id, quantity: 1 }] - } - return currItems.map(item => { - if (item.id === id) { - return { ...item, quantity: item.quantity + 1 } - } - return item - - }) - - }) - } - function decreaseCartQuantity(id: number) { - setCartItems(currItems => { - if (currItems.find(item => item.id === id)?.quantity === 1) { - return currItems.filter(item => item.id !== id) - } - return currItems.map(item => { - if (item.id === id) { - return { ...item, quantity: item.quantity - 1 } - } - return item - - }) - - }) - } - import { useMemo } from "react"; - - function removeFromCart(id: number) { - setCartItems(currItems => currItems.filter(item => item.id !== id)) - } - - const contextValue = useMemo(() => ({ - getItemQuantity, - increaseCartQuantity, - decreaseCartQuantity, - removeFromCart, - openCart, - closeCart, - cartItems, - cartQuantity, - }), [cartItems, cartQuantity]); - - return ( - - {children} - - ) -} diff --git a/src/app/page.tsx b/src/app/page.tsx index 1a885011..d9903403 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ + import Link from 'next/link'; import { testFetchUserData, diff --git a/src/components/ShoppingCart.tsx b/src/components/ShoppingCart.tsx deleted file mode 100644 index 75e69c33..00000000 --- a/src/components/ShoppingCart.tsx +++ /dev/null @@ -1,16 +0,0 @@ -import { Offcanvas, Stack } from "react-bootstrap" -import { useShoppingCart } from "../context/ShoppingCartContext" -import { formatCurrency } from "../utilities/formatCurrency" -import { CartItem } from "./CartItem" -import storeItems from "../data/items.json" - -type ShoppingCartProps = { - isOpen: boolean -} - -export function ShoppingCart({ isOpen }: ShoppingCartProps) { - const { closeCart, cartItems } = useShoppingCart() - return ( - - ) -}