feat: new stuff

This commit is contained in:
Mike 2024-11-18 21:51:46 -05:00
parent 87cfe81d2b
commit 9c6bed1983
5 changed files with 108 additions and 37 deletions

View file

@ -22,16 +22,18 @@ function Bag({ cartKeys, cart, setCart }) {
return ( return (
<> <>
{cartKeys.length > 0 ? ( {cartKeys.length > 0 ? (
<div> <div className={styles.fullwidth}>
<h2>Cart</h2> <h2 className={styles.header}>Cart</h2>
{cartKeys.map((key, index) => ( <div className={styles.cartItems}>
<CartItem {cartKeys.map((key, index) => (
item={cart[key]} <CartItem
cart={cart} item={cart[key]}
setCart={setCart} cart={cart}
key={index} setCart={setCart}
/> key={index}
))} />
))}
</div>
</div> </div>
) : ( ) : (
<h2>Your cart is empty</h2> <h2>Your cart is empty</h2>
@ -68,32 +70,40 @@ function CartItem({ item, cart, setCart }) {
const { title, price, image, qty } = item; const { title, price, image, qty } = item;
return ( return (
<div className={styles.container}> <div className={styles.cartItem}>
<img src={image} alt={title} /> <img className={styles.img} src={image} alt={title} />
<div> <div className={styles.card}>
<h3>{title}</h3> <div>
<p>Price: ${currencyFormat(price)} x </p> <h3>{title}</h3>
<p>Qty: {qty} </p> <p>Price: ${currencyFormat(price)} x </p>
</div> </div>
<div> <div className={styles.qtybtn}>
<button onClick={() => decreaseQty(item, cart, setCart)}> <button onClick={() => decreaseQty(item, cart, setCart)}>-</button>
Decrease <QuantityInput item={item} cart={cart} setCart={setCart} />
</button> <button onClick={() => increaseQty(item, cart, setCart)}>+</button>
<p>{qty}</p> </div>
<button onClick={() => increaseQty(item, cart, setCart)}> <div>
Increase <p>total: ${currencyFormat(qty * price)} </p>
</button> <button onClick={() => removeFromCart(item, cart, setCart)}>
</div> Remove From Cart
<div> </button>
<p>total: ${currencyFormat(qty * price)} </p> </div>
<button onClick={() => removeFromCart(item, cart, setCart)}>
Delete
</button>
</div> </div>
</div> </div>
); );
} }
function QuantityInput({ item, cart, setCart }) {
function handleChange(e) {
const newQty = e.target.value;
let obj = { ...cart };
obj[item.id].qty = newQty;
setCart(obj);
}
return <input value={item.qty} onChange={handleChange} />;
}
function increaseQty(item, cart, setCart) { function increaseQty(item, cart, setCart) {
if (cart[item.id]) { if (cart[item.id]) {
let obj = { ...cart }; let obj = { ...cart };
@ -134,4 +144,10 @@ OrderSummary.propTypes = {
cart: PropTypes.object, cart: PropTypes.object,
}; };
QuantityInput.propTypes = {
item: PropTypes.object,
cart: PropTypes.object,
setCart: PropTypes.func,
};
export default Cart; export default Cart;

View file

@ -15,7 +15,7 @@ export default function Navbar({ cartItems }) {
<h1>{sumCartItems}</h1> <h1>{sumCartItems}</h1>
</Link> </Link>
) : ( ) : (
<button>I&apos;m Ready</button> <img src="/shopping_cart_icon.svg" />
)} )}
</nav> </nav>
); );

View file

@ -4,5 +4,57 @@
} }
.summary { .summary {
margin-right: 1.5rem; margin-right: 3rem;
padding: 1rem;
display: inline-block;
min-width: 250px;
}
.fullwidth {
width: 100%;
}
.card {
display: flex;
flex-direction: column;
margin: 0 2rem;
justify-content: center;
align-items: center;
}
.cartItems {
display: flex;
flex-direction: column;
gap: 2.5rem;
align-items: space-between;
justify-content: space-between;
align-content: flex-start;
}
.cartItem {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
background-color: gray;
}
.qtybtn {
display: flex;
}
.qtyp {
font-size: 1.3rem;
padding: 0.5rem;
margin: 0.5rem;
}
.img {
width: 100%;
max-height: 300px;
vertical-align: middle;
object-fit: contain;
float: left;
}
.header {
margin: 1rem;
} }

View file

@ -1,10 +1,10 @@
:root { :root {
font-family: "Roboto Condensed", Inter, system-ui, sans-serif; font-family: Inter, system-ui, sans-serif;
line-height: 1.5; line-height: 1.5;
font-weight: 400; font-weight: 400;
color-scheme: light dark; /*color-scheme: light dark;*/
color: rgba(255, 255, 255, 0.87); /*color: rgba(255, 255, 255, 0.87);*/
/*background-color: #242424;*/ /*background-color: #242424;*/
font-synthesis: none; font-synthesis: none;

View file

@ -1,3 +1,6 @@
export function currencyFormat(str) { export function currencyFormat(str) {
return (str / 100).toFixed(2); return (str / 100)
.toFixed(2)
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} }