Your Cart
--
- {/* Write some code here... */}
-
diff --git a/src/App.jsx b/src/App.jsx
index d28c0b3..067fbf6 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1,61 +1,56 @@
+import { useState, useEffect } from 'react'
import './styles/reset.css'
import './styles/index.css'
-
+import Header from "./Components/Header/Header.jsx"
+import Cart from "./Components/Cart/Cart.jsx"
+import Credit from "./Components/Credit/Credit.jsx"
import initialStoreItems from './store-items'
+
/*
Here's what a store item should look like
{
id: '001-beetroot',
name: 'beetroot',
- price: 0.35
+ price: 0.35,
+ type: "vegetable"
}
What should a cart item look like? 🤔
*/
-console.log(initialStoreItems)
+//console.log(initialStoreItems)
export default function App() {
- // Setup state here...
+ const [cartItems, setCartItems] = useState([])
+
+ useEffect(() => {
+ const initialCart = []
+ initialStoreItems?.forEach((entry) => {
+ initialCart.push({...entry, quantity: 0})
+ })
+ setCartItems(initialCart)
+ },[])
+
+ const addItemToCart = (itemId) => {
+ const items = cartItems
+ const itemIndex = items.findIndex((i) => i.id == itemId)
+ const updatedItem = items.at(itemIndex)
+ updatedItem.quantity += 1
+ setCartItems([...items])
+ }
+
+ const removeItemFromCart = (itemId) => {
+ const updateItem = cartItems
+ updateItem[updateItem.findIndex((i) => i.id === itemId)].quantity -= 1
+ setCartItems([...updateItem])
+ }
return (
<>
- Greengrocers
-
- {/* Write some code here... */}
-
- Your Cart
-
- {/* Write some code here... */}
-
- Total
-
{entry.name}
+ + {entry.quantity} + +{capitalizeString(entry.name)}
+ +Category: {product.type}
+Price: £{product.price}
+Show categories:
+Sort produce:
+