diff --git a/src/App.jsx b/src/App.jsx index d28c0b3..5edcfca 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,37 +1,90 @@ import './styles/reset.css' import './styles/index.css' -import initialStoreItems from './store-items' - -/* - Here's what a store item should look like - { - id: '001-beetroot', - name: 'beetroot', - price: 0.35 - } +import { useState } from 'react' - What should a cart item look like? 🤔 - */ -console.log(initialStoreItems) +import initialStoreItems from './store-items' export default function App() { // Setup state here... + const [store, setStore] = useState(initialStoreItems) + const [cart, setCart] = useState([]) + + const handleClick = (target) => { + const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id) + + if (itemInCart === -1){ + setCart([...cart,{...target, count: 1}]) + } + else{ + const newCart = [...cart] + newCart[itemInCart].count++ + setCart(newCart) + } + + } + const incrementCartItem = (target) => { + const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id) + + const newCart = [...cart] + newCart[itemInCart].count++ + setCart(newCart) + } + + const decrementCartItem = (target) => { + const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id) + // console.log(cart[itemInCart]) + + if (cart[itemInCart].count === 1) { + const updatedCart = cart.filter((cartItem, index) => index !== itemInCart); + console.log(updatedCart) + setCart(updatedCart); + + } else { + const newCart = [...cart] + newCart[itemInCart].count-- + setCart(newCart) + } + } + + + return ( <>

Greengrocers

Your Cart