Skip to content

Timi Akande #126

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 7 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
13 changes: 13 additions & 0 deletions diagram
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
App
├── Header
├── GroceryStore
│ │
│ └── GroceryItem (Props: item, addToCart)
└── Cart
├── CartItem (Props: item, updateQuantity, removeItem)
└── CartTotal (Props: total)
66 changes: 48 additions & 18 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,74 @@
import './styles/reset.css'
import './styles/index.css'

import initialStoreItems from './store-items'
import React, { useState } from 'react';
import './styles/reset.css';
import './styles/index.css';
import initialStoreItems from './store-items';
import GroceryStore from './components/GroceryStore';
import Cart from './components/Cart';

export default function App() {
const [storeItems, setStoreItems] = useState(initialStoreItems);
const [cartItems, setCartItems] = useState([]);

const addToCart = (item) => {
const existingItem = cartItems.find((i) => i.id === item.id);
if (existingItem) {
updateQuantity(item.id, existingItem.quantity + 1);
} else {
setCartItems([...cartItems, { ...item, quantity: 1 }]);
}
};

const updateQuantity = (itemId, quantity) => {
setCartItems(
cartItems.map((item) =>
item.id === itemId ? { ...item, quantity } : item
)
);
};

const removeItem = (itemId) => {
setCartItems(cartItems.filter((item) => item.id !== itemId));
};

const total = cartItems.reduce(
(acc, item) => acc + item.price * item.quantity,
0
);

return (
<>
<header id="store">
<h1>Greengrocers</h1>
<ul className="item-list store--item-list">
</ul>
<GroceryStore items={storeItems} addToCart={addToCart} />
</header>
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<ul className="item-list cart--item-list">
</ul>
<Cart
items={cartItems}
updateQuantity={updateQuantity}
removeItem={removeItem}
/>
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">£0.00</span>
<span className="total-number">£{total.toFixed(2)}</span>
</div>
</div>
</main>
<div>
Icons made by
<a
href="https://www.flaticon.com/authors/icongeek26"
title="Icongeek26"
>
Icons made by{' '}
<a href="https://www.flaticon.com/authors/icongeek26" title="Icongeek26">
Icongeek26
</a>
from
</a>{' '}
from{' '}
<a href="https://www.flaticon.com/" title="Flaticon">
www.flaticon.com
</a>
</div>
</>
)
}
);
}
74 changes: 74 additions & 0 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useState } from 'react';
import './styles/reset.css';
import './styles/index.css';
import initialStoreItems from './store-items';
import GroceryStore from './components/GroceryStore';
import Cart from './components/Cart';

export default function App() {
const [storeItems, setStoreItems] = useState(initialStoreItems);
const [cartItems, setCartItems] = useState([]);

const addToCart = (item) => {
const existingItem = cartItems.find((i) => i.id === item.id);
if (existingItem) {
updateQuantity(item.id, existingItem.quantity + 1);
} else {
setCartItems([...cartItems, { ...item, quantity: 1 }]);
}
};

const updateQuantity = (itemId, quantity) => {
setCartItems(
cartItems.map((item) =>
item.id === itemId ? { ...item, quantity } : item
)
);
};

const removeItem = (itemId) => {
setCartItems(cartItems.filter((item) => item.id !== itemId));
};

const total = cartItems.reduce(
(acc, item) => acc + item.price * item.quantity,
0
);

return (
<>
<header id="store">
<h1>Greengrocers</h1>
<GroceryStore items={storeItems} addToCart={addToCart} />
</header>
<main id="cart">
<h2>Your Cart</h2>
<div className="cart--item-list-container">
<Cart
items={cartItems}
updateQuantity={updateQuantity}
removeItem={removeItem}
/>
</div>
<div className="total-section">
<div>
<h3>Total</h3>
</div>
<div>
<span className="total-number">£{total.toFixed(2)}</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>
</>
);
}
19 changes: 19 additions & 0 deletions src/components/Cart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import CartItem from './CartItem';

function Cart({ items, updateQuantity, removeItem }) {
return (
<ul className="item-list cart--item-list">
{items.map((item) => (
<CartItem
key={item.id}
item={item}
updateQuantity={updateQuantity}
removeItem={removeItem}
/>
))}
</ul>
);
}

export default Cart;
28 changes: 28 additions & 0 deletions src/components/CartItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';

function CartItem({ item, updateQuantity, removeItem }) {
const handleQuantityChange = (e) => {
const quantity = parseInt(e.target.value);
updateQuantity(item.id, quantity);

if (quantity === 0) {
removeItem(item.id);
}
};

return (
<li>
<div className="cart--item-icon">
<img src={`icons/${item.id}.svg`} alt={item.name} />
</div>
<p>{item.name}</p>
<input
type="number"
value={item.quantity}
onChange={handleQuantityChange}
/>
</li>
);
}

export default CartItem;
7 changes: 7 additions & 0 deletions src/components/CartTotal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

function CartTotal({ total }) {
return <div>Total: {total.toFixed(2)}</div>;
}

export default CartTotal;
14 changes: 14 additions & 0 deletions src/components/GroceryItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

function GroceryItem({ item, addToCart }) {
return (
<li>
<div className="store--item-icon">
<img src={`icons/${item.id}.svg`} alt={item.name} />
</div>
<button onClick={() => addToCart(item)}>Add to cart</button>
</li>
);
}

export default GroceryItem;
14 changes: 14 additions & 0 deletions src/components/GroceryStore.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import GroceryItem from './GroceryItem';

function GroceryStore({ items, addToCart }) {
return (
<ul className="item-list store--item-list">
{items.map((item) => (
<GroceryItem key={item.id} item={item} addToCart={addToCart} />
))}
</ul>
);
}

export default GroceryStore;
7 changes: 7 additions & 0 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

function Header() {
return <h1>Greengrocers</h1>;
}

export default Header;