diff --git a/diagram b/diagram new file mode 100644 index 0000000..861b669 --- /dev/null +++ b/diagram @@ -0,0 +1,13 @@ +App +│ +├── Header +│ +├── GroceryStore +│ │ +│ └── GroceryItem (Props: item, addToCart) +│ +└── Cart + │ + ├── CartItem (Props: item, updateQuantity, removeItem) + │ + └── CartTotal (Props: total) \ No newline at end of file diff --git a/src/App.jsx b/src/App.jsx index 03e658b..d569e5a 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,44 +1,74 @@ -import './styles/reset.css' -import './styles/index.css' - -import initialStoreItems from './store-items' +import React, { useState } from 'react'; +import './styles/reset.css'; +import './styles/index.css'; +import initialStoreItems from './store-items'; +import GroceryStore from './components/GroceryStore'; +import Cart from './components/Cart'; export default function App() { + const [storeItems, setStoreItems] = useState(initialStoreItems); + const [cartItems, setCartItems] = useState([]); + + const addToCart = (item) => { + const existingItem = cartItems.find((i) => i.id === item.id); + if (existingItem) { + updateQuantity(item.id, existingItem.quantity + 1); + } else { + setCartItems([...cartItems, { ...item, quantity: 1 }]); + } + }; + + const updateQuantity = (itemId, quantity) => { + setCartItems( + cartItems.map((item) => + item.id === itemId ? { ...item, quantity } : item + ) + ); + }; + + const removeItem = (itemId) => { + setCartItems(cartItems.filter((item) => item.id !== itemId)); + }; + + const total = cartItems.reduce( + (acc, item) => acc + item.price * item.quantity, + 0 + ); + return ( <>

Greengrocers

- +

Your Cart

- +

Total

- £0.00 + £{total.toFixed(2)}
- Icons made by - + Icons made by{' '} + Icongeek26 - - from + {' '} + from{' '} www.flaticon.com
- ) -} + ); +} \ No newline at end of file diff --git a/src/components/App.jsx b/src/components/App.jsx new file mode 100644 index 0000000..d569e5a --- /dev/null +++ b/src/components/App.jsx @@ -0,0 +1,74 @@ +import React, { useState } from 'react'; +import './styles/reset.css'; +import './styles/index.css'; +import initialStoreItems from './store-items'; +import GroceryStore from './components/GroceryStore'; +import Cart from './components/Cart'; + +export default function App() { + const [storeItems, setStoreItems] = useState(initialStoreItems); + const [cartItems, setCartItems] = useState([]); + + const addToCart = (item) => { + const existingItem = cartItems.find((i) => i.id === item.id); + if (existingItem) { + updateQuantity(item.id, existingItem.quantity + 1); + } else { + setCartItems([...cartItems, { ...item, quantity: 1 }]); + } + }; + + const updateQuantity = (itemId, quantity) => { + setCartItems( + cartItems.map((item) => + item.id === itemId ? { ...item, quantity } : item + ) + ); + }; + + const removeItem = (itemId) => { + setCartItems(cartItems.filter((item) => item.id !== itemId)); + }; + + const total = cartItems.reduce( + (acc, item) => acc + item.price * item.quantity, + 0 + ); + + return ( + <> +
+

Greengrocers

+ +
+
+

Your Cart

+
+ +
+
+
+

Total

+
+
+ £{total.toFixed(2)} +
+
+
+
+ Icons made by{' '} + + Icongeek26 + {' '} + from{' '} + + www.flaticon.com + +
+ + ); +} \ No newline at end of file diff --git a/src/components/Cart.jsx b/src/components/Cart.jsx new file mode 100644 index 0000000..244e987 --- /dev/null +++ b/src/components/Cart.jsx @@ -0,0 +1,19 @@ +import React from 'react'; +import CartItem from './CartItem'; + +function Cart({ items, updateQuantity, removeItem }) { + return ( + + ); +} + +export default Cart; diff --git a/src/components/CartItem.jsx b/src/components/CartItem.jsx new file mode 100644 index 0000000..4914215 --- /dev/null +++ b/src/components/CartItem.jsx @@ -0,0 +1,28 @@ +import React from 'react'; + +function CartItem({ item, updateQuantity, removeItem }) { + const handleQuantityChange = (e) => { + const quantity = parseInt(e.target.value); + updateQuantity(item.id, quantity); + + if (quantity === 0) { + removeItem(item.id); + } + }; + + return ( +
  • +
    + {item.name} +
    +

    {item.name}

    + +
  • + ); +} + +export default CartItem; \ No newline at end of file diff --git a/src/components/CartTotal.jsx b/src/components/CartTotal.jsx new file mode 100644 index 0000000..8601750 --- /dev/null +++ b/src/components/CartTotal.jsx @@ -0,0 +1,7 @@ +import React from 'react'; + +function CartTotal({ total }) { + return
    Total: {total.toFixed(2)}
    ; +} + +export default CartTotal; diff --git a/src/components/GroceryItem.jsx b/src/components/GroceryItem.jsx new file mode 100644 index 0000000..39d7998 --- /dev/null +++ b/src/components/GroceryItem.jsx @@ -0,0 +1,14 @@ +import React from 'react'; + +function GroceryItem({ item, addToCart }) { + return ( +
  • +
    + {item.name} +
    + +
  • + ); +} + +export default GroceryItem; diff --git a/src/components/GroceryStore.jsx b/src/components/GroceryStore.jsx new file mode 100644 index 0000000..c6a5fb8 --- /dev/null +++ b/src/components/GroceryStore.jsx @@ -0,0 +1,14 @@ +import React from 'react'; +import GroceryItem from './GroceryItem'; + +function GroceryStore({ items, addToCart }) { + return ( + + ); +} + +export default GroceryStore; diff --git a/src/components/Header.jsx b/src/components/Header.jsx new file mode 100644 index 0000000..69d451f --- /dev/null +++ b/src/components/Header.jsx @@ -0,0 +1,7 @@ +import React from 'react'; + +function Header() { + return

    Greengrocers

    ; +} + +export default Header;