Your Cart
--
-
diff --git a/src/App.jsx b/src/App.jsx
index 03e658b..f3190fd 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1,31 +1,62 @@
-import './styles/reset.css'
-import './styles/index.css'
+import "./styles/reset.css";
+import "./styles/index.css";
-import initialStoreItems from './store-items'
+import initialStoreItems from "./store-items";
+import Header from "./Components/Header";
+import { useState } from "react";
+import MainBody from "./Components/MainBody";
export default function App() {
+ const [cart, setCart] = useState([]);
+ const [store, setStore] = useState(initialStoreItems);
+
+ const removeFromCart = (item) => {
+
+ const checkItem = cart.find((cartItem) => {if(cartItem.quantity === 1 && cartItem.id === item.id) return true})
+
+
+
+ if (checkItem) {
+ const updatedCart = cart.filter((cartItem) => {
+ if(cartItem.id !== checkItem.id)
+ return cartItem
+ })
+ setCart(updatedCart)
+
+ } else {
+ const updatedCart = cart.map((cartItem) => {
+ if (cartItem.id === item.id) cartItem.quantity--;
+ return { ...cartItem };
+ });
+ setCart(updatedCart);
+ }
+ };
+
+ const addToCart = (item) => {
+ const checkItem = cart.find((cartItem) => {
+ if (cartItem.id === item.id) return true;
+ });
+
+ if (!checkItem) {
+ const updatedCart = [...cart, { ...item, quantity: 1 }];
+ setCart(updatedCart);
+ } else {
+ const updatedCart = cart.map((cartItem) => {
+ if (cartItem.id === item.id && cartItem.quantity >= 1) cartItem.quantity++;
+ return { ...cartItem };
+ });
+ setCart(updatedCart);
+ }
+ };
+
return (
<>
- Greengrocers
-
-
- Your Cart
-
-
- Total
-
{cartItem.name}
+ + {cartItem.quantity} + +