Skip to content

Leonardo Lodi #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 Tree-diagram.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 18 additions & 35 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import './styles/reset.css'
import './styles/index.css'

import initialStoreItems from './store-items'
import Header from './Header'
import Footer from './Footer'
import MainContent from './MainContent'
import { useState } from 'react'

export default function App() {
const [cartList, setCartList] = useState([])

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>
<div>
Icons made by
<a
href="https://www.flaticon.com/authors/icongeek26"
title="Icongeek26"
>
Icongeek26
</a>
from
<a href="https://www.flaticon.com/" title="Flaticon">
www.flaticon.com
</a>
</div>
<Header
cartList={cartList}
setCartList={setCartList}
/>

<MainContent
cartList={cartList}
setCartList={setCartList}
/>

<Footer />
</>
)
}
}
38 changes: 38 additions & 0 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable react/prop-types */
export default function CartItem({ cartList, setCartList }) {
const increment = (item) => {
setCartList(prevCartList => prevCartList.map(cartItem =>
cartItem.id === item.id ? { ...cartItem, quantity: cartItem.quantity + 1 } : cartItem
))
}

const decrement = (item) => {
if (item.quantity > 1) {
setCartList(prevCartList => prevCartList.map(cartItem =>
cartItem.id === item.id ? { ...cartItem, quantity: cartItem.quantity - 1 } : cartItem
))
} else {
setCartList(prevCartList => prevCartList.filter(cartItem => cartItem.id !== item.id))
}
}

return (
<div className="cart--item-list-container">
{cartList.map((item) => (
<ul key={item.id} className="item-list cart--item-list">
<li>
<img
className="cart--item-icon"
src={`/assets/icons/${item.id}.svg`}
alt={item.name}
/>
<p>{item.name}</p>
<button onClick={() => decrement(item)} className="quantity-btn remove-btn center">-</button>
<span className="quantity-text center">{item.quantity}</span>
<button onClick={() => increment(item)} className="quantity-btn add-btn center">+</button>
</li>
</ul>
))}
</div>
)
}
20 changes: 20 additions & 0 deletions src/Footer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default function Footer() {
return (
<div>
Icons made by
<a
href="https://www.flaticon.com/authors/icongeek26"
title="Icongeek26"
>
Icongeek26
</a>
from
<a
href="https://www.flaticon.com/"
title="Flaticon"
>
www.flaticon.com
</a>
</div>
)
}
15 changes: 15 additions & 0 deletions src/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import StoreItem from "./StoreItem";

// eslint-disable-next-line react/prop-types
export default function Header({ cartList, setCartList }) {
return (
<header id="store">
<h1>Greengrocers</h1>

<StoreItem
cartList={cartList}
setCartList={setCartList}
/>
</header>
)
}
18 changes: 18 additions & 0 deletions src/MainContent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable react/prop-types */
import CartItem from "./CartItem";
import Price from "./Price";

export default function MainContent({ cartList, setCartList }) {
return (
<main id="cart">
<h2>Your Cart</h2>

<CartItem
cartList={cartList}
setCartList={setCartList}
/>

<Price cartList={cartList} />
</main>
)
}
16 changes: 16 additions & 0 deletions src/Price.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable react/prop-types */
export default function Price({ cartList }) {
const totalPrice = cartList.reduce((total, item) => total + (item.quantity * item.price), 0)

return (
<div className="total-section">
<div>
<h3>Total</h3>
</div>

<div>
<span className="total-number">{`£${totalPrice.toFixed(2)}`}</span>
</div>
</div>
)
}
31 changes: 31 additions & 0 deletions src/StoreItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import initialStoreItems from './store-items'

/* eslint-disable react/prop-types */
export default function StoreItem({ cartList, setCartList }) {
const handleClick = (item) => {
if (cartList.find(cartItem => cartItem.id === item.id)) {
setCartList(prevCartList => prevCartList.map(cartItem =>
cartItem.id === item.id ? { ...cartItem, quantity: cartItem.quantity + 1 } : cartItem
))
} else {
setCartList(prevCartList => [...prevCartList, { ...item, quantity: 1 }])
}
}

return (
<ul className="item-list store--item-list">
{initialStoreItems.map((item) => (
<li key={item.id}>
<div className="store--item-icon">
<img
src={`/assets/icons/${item.id}.svg`}
alt={item.name}
/>
</div>

<button onClick={() => handleClick(item)}>Add to cart</button>
</li>
))}
</ul>
)
}