Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added greengrocers-app-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand All @@ -8,6 +8,7 @@
</head>
<body>
<div id="root"></div>

<script type="module" src="/src/main.jsx"></script>
</body>
</html>
61 changes: 37 additions & 24 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
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 { useState } from "react";
import Header from "./Header";
import Cart from "./Cart";

export default function App() {
const [products, setProducts] = useState(initialStoreItems);
const [cart, setCart] = useState([]);

function handleAddToCartClick(event) {
const { id } = event.target;
const isFound = cart.find((product) => product.id === id);

products.forEach((product) => {
if (product.id === id && !isFound) {
setCart([...cart, { ...product, quantity: 1 }]);
}

if (product.id === id && isFound) {
const index = cart.findIndex((product) => product.id === id);
const item = cart[index];

const newCart = cart.map((product) => {
if (product.id === item.id) {
product.quantity = product.quantity + 1;
}
return product;
});

setCart(newCart);
}
});
}

return (
<>
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
</ul>
</header>
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
</ul>
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">£0.00</span>
</div>
</div>
</main>
<Header products={products} handleAddToCartClick={handleAddToCartClick} />
<Cart cart={cart} setCart={setCart} />
<div>
Icons made by
<a
Expand All @@ -40,5 +53,5 @@ export default function App() {
</a>
</div>
</>
)
);
}
30 changes: 30 additions & 0 deletions src/Cart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import CartList from "./CartList";

export default function Cart(props) {
let total = 0;

props.cart.forEach((product) => {
const productQuantity = product.quantity;
const productPrice = product.price;

const cost = productQuantity * productPrice;
total += cost;
});

return (
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<CartList {...props} />
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">{total.toFixed(2)}</span>
</div>
</div>
</main>
);
}
61 changes: 61 additions & 0 deletions src/CartList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export default function CartList(props) {
function handleIncrementClick(event) {
const { id } = event.target;

const incrementQuantity = props.cart.map((product) => {
if (product.id === id) {
product.quantity = product.quantity + 1;
}
return product;
});

props.setCart(incrementQuantity);
}

function handleDecrementClick(event) {
const { id } = event.target;

const decrementQuantity = props.cart.map((product) => {
if (product.id === id && product.quantity > 0) {
product.quantity = product.quantity - 1;
}
return product;
});

props.setCart(
decrementQuantity.filter((product) => product.quantity !== 0)
);
}

return (
<ul className="item-list cart--item-list">
{props.cart.map((product) => {
return (
<li key={product.id}>
<img
className="cart--item-icon"
src={`assets/icons/${product.id}.svg`}
alt={`foto of ${product.name}`}
/>
<p>{product.name}</p>
<button
className="quantity-btn remove-btn center"
id={product.id}
onClick={handleDecrementClick}
>
-
</button>
<span className="quantity-text center">{product.quantity}</span>
<button
className="quantity-btn add-btn center"
id={product.id}
onClick={handleIncrementClick}
>
+
</button>
</li>
);
})}
</ul>
);
}
21 changes: 21 additions & 0 deletions src/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function Header(props) {
return (
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
{props.products.map((product, index) => {
return (
<li key={index}>
<div className="store--item-icon">
<img src={`./assets/icons/${product.id}.svg`} alt="beetroot" />
</div>
<button id={product.id} onClick={props.handleAddToCartClick}>
Add to cart
</button>
</li>
);
})}
</ul>
</header>
);
}
1 change: 1 addition & 0 deletions src/assets/icons/001-beetroot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/icons/002-carrot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/icons/003-apple.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/icons/004-apricot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/icons/005-avocado.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading