diff --git a/ordering/src/App.js b/ordering/src/App.js index abc6b69..13ac30f 100644 --- a/ordering/src/App.js +++ b/ordering/src/App.js @@ -1,16 +1,27 @@ +import { useState } from "react" import Header from "./components/Layout/Header" import Meals from "./components/Meals/Meals" import Cart from "./components/Cart/Cart" +import CartProvider from "./store/CartProvider" function App() { + const [cartIsShown, setCartIsShown] = useState(false) + + const showCartHandler = () => { + setCartIsShown(true) + } + const hideCartHandler = () => { + setCartIsShown(false) + } + return ( - <> - -
+ + {cartIsShown && } +
- + ) } diff --git a/ordering/src/components/Cart/Cart.js b/ordering/src/components/Cart/Cart.js index 1bf845b..6eab667 100644 --- a/ordering/src/components/Cart/Cart.js +++ b/ordering/src/components/Cart/Cart.js @@ -14,15 +14,20 @@ function Cart(props) { ) return ( - + {cartItems}
- Total Amount - 35.62 + Total Amount + 35.62
- - + +
) diff --git a/ordering/src/components/Layout/Header.js b/ordering/src/components/Layout/Header.js index 5f2ab5a..3d8042f 100644 --- a/ordering/src/components/Layout/Header.js +++ b/ordering/src/components/Layout/Header.js @@ -8,7 +8,7 @@ function Header(props) { <>

React Meals

- +
Table full of delicious food. diff --git a/ordering/src/components/Layout/HeaderCartButton.js b/ordering/src/components/Layout/HeaderCartButton.js index 6724eae..0b41983 100644 --- a/ordering/src/components/Layout/HeaderCartButton.js +++ b/ordering/src/components/Layout/HeaderCartButton.js @@ -1,16 +1,23 @@ +import { useContext } from "react" import CartIcon from "../Cart/CartIcon" import classes from "./HeaderCartButton.module.css" +import CartContext from "../../store/cart-context" + function HeaderCartButton(props) { + const cartCtx = useContext(CartContext) + + const numberOfCartItems = cartCtx.items.reduce((curNumber, item) => { + return curNumber + item.amount + }, 0) + return ( - ) } diff --git a/ordering/src/components/UI/Modal.js b/ordering/src/components/UI/Modal.js index 8f979f7..931be6f 100644 --- a/ordering/src/components/UI/Modal.js +++ b/ordering/src/components/UI/Modal.js @@ -3,7 +3,7 @@ import ReactDOM from "react-dom" import classes from "./Modal.module.css" const Backdrop = (props) => { - return
+ return
} const ModalOverlay = (props) => { return ( @@ -18,7 +18,7 @@ const portalElement = document.getElementById("overlays") function Modal(props) { return ( <> - {ReactDOM.createPortal(, portalElement)} + {ReactDOM.createPortal(, portalElement)} {ReactDOM.createPortal( {props.children}, portalElement diff --git a/ordering/src/store/CartProvider.js b/ordering/src/store/CartProvider.js new file mode 100644 index 0000000..328644c --- /dev/null +++ b/ordering/src/store/CartProvider.js @@ -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 ( + + {props.children} + + ) +} + +export default CartProvider diff --git a/ordering/src/store/cart-context.js b/ordering/src/store/cart-context.js new file mode 100644 index 0000000..8bda1cc --- /dev/null +++ b/ordering/src/store/cart-context.js @@ -0,0 +1,10 @@ +import React from "react" + +const CartContext = React.createContext({ + items: [], + totalAmount: 0, + addItem: (item) => {}, + removeItem: (id) => {}, +}) + +export default CartContext