Skip to content

Shaun Harris #127

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 2 commits 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 Componenets:Hierarchy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 39 additions & 22 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import './styles/reset.css'
import './styles/index.css'

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

export default function App() {

const [storeItems] = useState(initialStoreItems)
const[cartItems, setCartItems] = useState([])


const addToCart = (targetItem) => {
const exisitingItem = cartItems.find(item => item.id === targetItem.id)
if(exisitingItem) {
const updateCart = cartItems.map(item =>
item.id === targetItem.id ? {...item, quantity: item.quantity + 1 } : item
)
setCartItems(updateCart)
} else {
setCartItems([...cartItems, targetItem])
}
}

const removeItem = (targetItem) => {
const exisitingItem = cartItems.find(item => item.id === targetItem.id)
if (exisitingItem.quantity > 1) {
const updateCart = cartItems.map(item =>
item.id === targetItem.id ? {...item, quantity: item.quantity - 1 } : item
)
setCartItems(updateCart)
} else {
setCartItems(cartItems.filter(item => item != targetItem))
}
}

const calculateTotal = () => {
return cartItems.reduce((acc, item) => acc + (item.quantity * item.price), 0).toFixed(2);
}

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 storeItems={storeItems} addToCart={addToCart} />
<MainSection cartItems={cartItems} addToCart={addToCart} removeItem={removeItem} calculateTotal={calculateTotal} />
<div>
Icons made by
<a
Expand Down
11 changes: 11 additions & 0 deletions src/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function CartItem({ item, addToCart, removeItem }) {
return (
<li>
<img className='cart--item-icon' src={`./assets/icons/${item.id}.svg`}alt={item.name}/>
<p>{item.name}</p>
<button onClick={() => removeItem(item)} className='quantity-btn remove-btn center'>-</button>
<span className='quantity-text center'>{item.quantity}</span>
<button onClick={() => addToCart(item)} className='quantity-btn add-btn center'>+</button>
</li>
)
}
14 changes: 14 additions & 0 deletions src/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import StoreItem from './StoreItem'

export default function Header({ storeItems, addToCart}) {
return (
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
{storeItems.map((item) => (
<StoreItem key={item.id} item={item} addToCart={addToCart} />
))}
</ul>
</header>
)
}
29 changes: 29 additions & 0 deletions src/MainSection.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import CartItem from './CartItem'

export default function MainSection({ cartItems, addToCart, removeItem, calculateTotal}) {
return (
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
{cartItems.map((item) => (
<CartItem key={item.id} item={item} addToCart={addToCart} removeItem={removeItem} />
))}
</ul>
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">£{calculateTotal()}</span>
</div>
</div>
</main>

)
}




10 changes: 10 additions & 0 deletions src/StoreItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function StoreItem({ item, addToCart }) {
return (
<li>
<div className='store--item-icon'>
<img src={`/assets/icons/${item.id}.svg`} alt={item.name} />
</div>
<button onClick={() => addToCart({...item, quantity : 1})}>Add to Cart</button>
</li>
)
}