count cart items

This commit is contained in:
Tyrel Souza 2022-09-26 12:30:07 -04:00
parent bd28f11b59
commit 6915852524
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
7 changed files with 70 additions and 16 deletions

View File

@ -1,16 +1,27 @@
import { useState } from "react"
import Header from "./components/Layout/Header" import Header from "./components/Layout/Header"
import Meals from "./components/Meals/Meals" import Meals from "./components/Meals/Meals"
import Cart from "./components/Cart/Cart" import Cart from "./components/Cart/Cart"
import CartProvider from "./store/CartProvider"
function App() { function App() {
const [cartIsShown, setCartIsShown] = useState(false)
const showCartHandler = () => {
setCartIsShown(true)
}
const hideCartHandler = () => {
setCartIsShown(false)
}
return ( return (
<> <CartProvider>
<Cart /> {cartIsShown && <Cart onClose={hideCartHandler} />}
<Header /> <Header onShowCart={showCartHandler} />
<main> <main>
<Meals /> <Meals />
</main> </main>
</> </CartProvider>
) )
} }

View File

@ -14,15 +14,20 @@ function Cart(props) {
) )
return ( return (
<Modal> <Modal onClose={props.onClose}>
{cartItems} {cartItems}
<div className={classes.total}> <div className={classes.total}>
<span>Total Amount</span> <span>Total Amount</span>
<span>35.62</span> <span>35.62</span>
</div> </div>
<div className={classes.actions}> <div className={classes.actions}>
<button className={classes['button--alt']}>Close</button> <button
<button className={classes.button}>Order</button> onClick={props.onClose}
className={classes["button--alt"]}
>
Close
</button>
<button className={classes.button}>Order</button>
</div> </div>
</Modal> </Modal>
) )

View File

@ -8,7 +8,7 @@ function Header(props) {
<> <>
<header className={classes.header}> <header className={classes.header}>
<h1>React Meals</h1> <h1>React Meals</h1>
<HeaderCartButton/> <HeaderCartButton onClick={props.onShowCart}/>
</header> </header>
<div className={classes["main-image"]}> <div className={classes["main-image"]}>
<img src={mealsImage} alt="Table full of delicious food." /> <img src={mealsImage} alt="Table full of delicious food." />

View File

@ -1,16 +1,23 @@
import { useContext } from "react"
import CartIcon from "../Cart/CartIcon" import CartIcon from "../Cart/CartIcon"
import classes from "./HeaderCartButton.module.css" import classes from "./HeaderCartButton.module.css"
import CartContext from "../../store/cart-context"
function HeaderCartButton(props) { function HeaderCartButton(props) {
const cartCtx = useContext(CartContext)
const numberOfCartItems = cartCtx.items.reduce((curNumber, item) => {
return curNumber + item.amount
}, 0)
return ( return (
<button className={classes.button}> <button className={classes.button} onClick={props.onClick}>
<span className={classes.icon}> <span className={classes.icon}>
<CartIcon /> <CartIcon />
</span> </span>
<span>Your Cart</span> <span>Your Cart</span>
<span className={classes.badge}> <span className={classes.badge}>{numberOfCartItems}</span>
3
</span>
</button> </button>
) )
} }

View File

@ -3,7 +3,7 @@ import ReactDOM from "react-dom"
import classes from "./Modal.module.css" import classes from "./Modal.module.css"
const Backdrop = (props) => { const Backdrop = (props) => {
return <div className={classes.backdrop} /> return <div className={classes.backdrop} onClick={props.onClose} />
} }
const ModalOverlay = (props) => { const ModalOverlay = (props) => {
return ( return (
@ -18,7 +18,7 @@ const portalElement = document.getElementById("overlays")
function Modal(props) { function Modal(props) {
return ( return (
<> <>
{ReactDOM.createPortal(<Backdrop />, portalElement)} {ReactDOM.createPortal(<Backdrop onClose={props.onClose}/>, portalElement)}
{ReactDOM.createPortal( {ReactDOM.createPortal(
<ModalOverlay>{props.children}</ModalOverlay>, <ModalOverlay>{props.children}</ModalOverlay>,
portalElement portalElement

View File

@ -0,0 +1,21 @@
import CartContext from "./cart-context"
const CartProvider = (props) => {
const addItemToCartHandler = (item) => {}
const removeItemToCartHandler = (id) => {}
const cartContext = {
items: [],
totalAmount: 0,
addItem: addItemToCartHandler,
removeItem: removeItemToCartHandler,
}
return (
<CartContext.Provider value={cartContext}>
{props.children}
</CartContext.Provider>
)
}
export default CartProvider

View File

@ -0,0 +1,10 @@
import React from "react"
const CartContext = React.createContext({
items: [],
totalAmount: 0,
addItem: (item) => {},
removeItem: (id) => {},
})
export default CartContext