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
79 changes: 55 additions & 24 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
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 Header from "./Components/Header";
import { useState } from "react";
import MainBody from "./Components/MainBody";

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

const removeFromCart = (item) => {

const checkItem = cart.find((cartItem) => {if(cartItem.quantity === 1 && cartItem.id === item.id) return true})



if (checkItem) {
const updatedCart = cart.filter((cartItem) => {
if(cartItem.id !== checkItem.id)
return cartItem
})
setCart(updatedCart)

} else {
const updatedCart = cart.map((cartItem) => {
if (cartItem.id === item.id) cartItem.quantity--;
return { ...cartItem };
});
setCart(updatedCart);
}
};

const addToCart = (item) => {
const checkItem = cart.find((cartItem) => {
if (cartItem.id === item.id) return true;
});

if (!checkItem) {
const updatedCart = [...cart, { ...item, quantity: 1 }];
setCart(updatedCart);
} else {
const updatedCart = cart.map((cartItem) => {
if (cartItem.id === item.id && cartItem.quantity >= 1) cartItem.quantity++;
return { ...cartItem };
});
setCart(updatedCart);
}
};

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 store={store} addToCart={addToCart} />
<MainBody
cart={cart}
addToCart={addToCart}
removeFromCart={removeFromCart}
/>
<div>
Icons made by
<a
Expand All @@ -40,5 +71,5 @@ export default function App() {
</a>
</div>
</>
)
);
}
18 changes: 18 additions & 0 deletions src/Components/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable react/prop-types */

export default function CartItem({cartItem, addToCart, removeFromCart}) {

return (
<li key={cartItem.id}>
<img
className="cart--item-icon"
src={`../../assets/icons/${cartItem.id}.svg`}
alt={cartItem.name}
/>
<p>{cartItem.name}</p>
<button className="quantity-btn remove-btn center" onClick={() => removeFromCart(cartItem)}>-</button>
<span className="quantity-text center">{cartItem.quantity}</span>
<button className="quantity-btn add-btn center" onClick={() => addToCart(cartItem)}>+</button>
</li>
);
}
15 changes: 15 additions & 0 deletions src/Components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable react/prop-types */
import StoreItem from "./StoreItem";

export default function Header({store, addToCart}) {
return (
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
{store.map((storeItem, index) => {
return <StoreItem storeItem={storeItem} key={index} addToCart={addToCart}/>
})}
</ul>
</header>
);
}
32 changes: 32 additions & 0 deletions src/Components/MainBody.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable react/prop-types */

import CartItem from "./CartItem";

export default function MainBody({cart, addToCart, removeFromCart}) {

const calculateTotal = () => {
const calculateCartTotal = cart.reduce((cartTotal, item) => cartTotal += item.quantity * item.price, 0)
return calculateCartTotal.toFixed(2)
}

return (
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
{cart.map((cartItem) => {
return <CartItem cartItem={cartItem} key={cartItem.id} addToCart={addToCart} removeFromCart={removeFromCart}/>
})}
</ul>
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">{`£${calculateTotal()}`}</span>
</div>
</div>
</main>
)
}
11 changes: 11 additions & 0 deletions src/Components/StoreItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable react/prop-types */
export default function StoreItem({storeItem, index, addToCart}) {
return (
<li>
<div className="store--item-icon" key={index}>
<img src={`../../assets/icons/${storeItem.id}.svg`} alt={storeItem.name}/>
</div>
<button onClick={() => addToCart(storeItem)}>Add to cart</button>
</li>
)
}