Skip to content

Steven Tong Phung #107

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
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
81 changes: 67 additions & 14 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,90 @@
import './styles/reset.css'
import './styles/index.css'

import initialStoreItems from './store-items'

/*
Here's what a store item should look like
{
id: '001-beetroot',
name: 'beetroot',
price: 0.35
}
import { useState } from 'react'

What should a cart item look like? 🤔
*/

console.log(initialStoreItems)
import initialStoreItems from './store-items'

export default function App() {
// Setup state here...
const [store, setStore] = useState(initialStoreItems)
const [cart, setCart] = useState([])

const handleClick = (target) => {
const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id)

if (itemInCart === -1){
setCart([...cart,{...target, count: 1}])
}
else{
const newCart = [...cart]
newCart[itemInCart].count++
setCart(newCart)
}

}

const incrementCartItem = (target) => {
const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id)

const newCart = [...cart]
newCart[itemInCart].count++
setCart(newCart)
}

const decrementCartItem = (target) => {
const itemInCart = cart.findIndex((cartItem) => cartItem.id === target.id)
// console.log(cart[itemInCart])

if (cart[itemInCart].count === 1) {
const updatedCart = cart.filter((cartItem, index) => index !== itemInCart);
console.log(updatedCart)
setCart(updatedCart);

} else {
const newCart = [...cart]
newCart[itemInCart].count--
setCart(newCart)
}
}



return (
<>
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
{/* Write some code here... */}
{store.map((item, index) => (
<li key={index}>
<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>
</header>
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
{/* Write some code here... */}
{cart.map((item, index) => (
<li key={index}>
<img
className="cart--item-icon"
src={`/assets/icons/${item.id}.svg`} alt={item.name}
/>
<p>{item.name}</p>
<button className="quantity-btn remove-btn center" onClick={() => decrementCartItem(item)}>-</button>
<span className="quantity-text center">{item.count}
</span>
<button className="quantity-btn add-btn center" onClick={() => incrementCartItem(item)}>+</button>
</li>
))}
</ul>
</div>
<div className="total-section">
Expand Down