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
11 changes: 11 additions & 0 deletions Components_Tree_Diagram/Components-Tree_Diagram.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 14 additions & 17 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import './styles/reset.css'
import './styles/index.css'
import { useState } from 'react'

// import initialStoreItems from './store-items'

import Store from './Components/Store'
import Cart from './Components/Cart'
import Total from './Components/Total'

export default function App() {
const [cartProds, setCartProds] = useState([])

import initialStoreItems from './store-items'

export default function App() {
return (
<>
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
</ul>
<Store cartProds={cartProds} setCartProds={setCartProds} />
</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>
<Cart cartProds={cartProds} setCartProds={setCartProds} />
<Total cartProds={cartProds} />
</main>

<div>
Icons made by
<a
Expand Down
20 changes: 20 additions & 0 deletions src/Components/AddToCart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function AddToCart({ setCartProds, cartProds, product}) {

function handleAddToCart() {
const inCart = cartProds.find(p => p.id === product.id);

if (inCart) {
inCart.qty++;
setCartProds([...cartProds]);
} else {
const addProduct = { ...product, qty: 1 };
setCartProds([...cartProds, addProduct]);
}
}

return (
<button style={{ marginTop: '10px' }}
onClick={() => handleAddToCart(product)} >
Add to Cart</button>
)
}
13 changes: 13 additions & 0 deletions src/Components/Cart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CartProducts from "./CartProducts"
export default function Cart({ cartProds, setCartProds }) {
return (
<>
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
<CartProducts cartProds={cartProds} setCartProds={setCartProds} />
</ul>
</div>
</>
)
}
31 changes: 31 additions & 0 deletions src/Components/CartProducts.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function CartProducts({ cartProds, setCartProds }) {

function handleClick(e, prod) {
if (e.target.innerText === '+') {
prod.qty++
}
if (e.target.innerText === '-' && prod.qty > 0) {
prod.qty--
}
setCartProds([...cartProds])
}

return (
cartProds.map((prod, index) => {
if (prod.qty) {
return (
<li key={index}>
<img
className="cart--item-icon"
src={`/assets/icons/${prod.id}.svg`} alt={prod.name}
/>
<p>{prod.name}</p>
<button className="quantity-btn remove-btn center" onClick={(e) => handleClick(e, prod)} >-</button>
<span className="quantity-text center">{prod.qty}</span>
<button className="quantity-btn add-btn center" onClick={(e) => handleClick(e, prod)}>+</button>
</li>
)
}
})
)
}
12 changes: 12 additions & 0 deletions src/Components/Store.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState } from 'react'
import initialStoreItems from '../store-items'
import StoreItem from "./StoreItem"

export default function Store({ cartProds, setCartProds }) {
const [products, setProducts] = useState(initialStoreItems)
return (
<ul className="item-list store--item-list">
<StoreItem products={products} cartProds={cartProds} setCartProds={setCartProds} />
</ul>
)
}
21 changes: 21 additions & 0 deletions src/Components/StoreItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import AddToCart from "./AddToCart"

export default function StoreItem({ products, cartProds, setCartProds }) {


return (
products.map(product => (
<li key={product.id}>
<div className="store--item-icon">
<img width={94} src={`/assets/icons/${product.id}.svg`} alt={product.name} />
</div>
<div className="store--item-details">

<p>Price: ${product.price}</p>
<button style={{marginTop: '10px' }}>Product Info</button>
<AddToCart product={product} cartProds={cartProds} setCartProds={setCartProds} />
</div>
</li>
))
)
}
15 changes: 15 additions & 0 deletions src/Components/Total.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function Total({ cartProds }) {

const total = cartProds.reduce((total, prod) => (total += prod.price * prod.qty),0)

return (
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">£{total.toFixed(2)}</span>
</div>
</div>
)
}