From 135e4a9a1dc09f1e38f3593db2b106c697c94b3e Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Mon, 26 Sep 2022 13:18:59 -0400 Subject: [PATCH] add to cart --- .../src/components/Meals/MealItem/MealItem.js | 18 ++++++++-- .../components/Meals/MealItem/MealItemForm.js | 27 ++++++++++++-- ordering/src/components/UI/Input.js | 7 ++-- ordering/src/store/CartProvider.js | 36 ++++++++++++++++--- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/ordering/src/components/Meals/MealItem/MealItem.js b/ordering/src/components/Meals/MealItem/MealItem.js index f60c452..fbcaeea 100644 --- a/ordering/src/components/Meals/MealItem/MealItem.js +++ b/ordering/src/components/Meals/MealItem/MealItem.js @@ -1,8 +1,19 @@ +import { useContext } from "react" import classes from "./MealItem.module.css" import MealItemForm from "./MealItemForm" +import CartContext from "../../../store/cart-context" function MealItem(props) { - const price = `$${props.price.toFixed(2)}` + const cartContext = useContext(CartContext) + const price = `$${props.price.toFixed(2)}` + const addItemToCartHandler = (amount) => { + cartContext.addItem({ + id: props.id, + name: props.name, + amount: amount, + price: props.price, + }) + } return (
  • @@ -11,7 +22,10 @@ function MealItem(props) {
    {price}
    - +
  • ) diff --git a/ordering/src/components/Meals/MealItem/MealItemForm.js b/ordering/src/components/Meals/MealItem/MealItemForm.js index ff6b833..77f34a9 100644 --- a/ordering/src/components/Meals/MealItem/MealItemForm.js +++ b/ordering/src/components/Meals/MealItem/MealItemForm.js @@ -1,13 +1,35 @@ +import { useRef, useState } from "react" import classes from "./MealItemForm.module.css" import Input from "../../UI/Input" function MealItemForm(props) { + const [amountIsValid, setAmountIsValid] = useState(true) + + const amountInputRef = useRef() + + const submitHandler = (event) => { + event.preventDefault() + const enteredAmount = amountInputRef.current.value + const enteredAmountNumber = +enteredAmount + if ( + enteredAmount.trim().length === 0 || + enteredAmountNumber < 1 || + enteredAmountNumber > 5 + ) { + setAmountIsValid(false) + return + } + + props.onAddToCart(enteredAmountNumber) + } + return ( -
    + + {!amountIsValid &&

    Please enter a valid amount (1-5)

    }
    ) } diff --git a/ordering/src/components/UI/Input.js b/ordering/src/components/UI/Input.js index 3447561..f302003 100644 --- a/ordering/src/components/UI/Input.js +++ b/ordering/src/components/UI/Input.js @@ -1,11 +1,12 @@ +import React from "react" import classes from "./Input.module.css" -function Input(props) { +const Input = React.forwardRef((props, ref) => { return (
    - +
    ) -} +}) export default Input diff --git a/ordering/src/store/CartProvider.js b/ordering/src/store/CartProvider.js index 328644c..3a47e27 100644 --- a/ordering/src/store/CartProvider.js +++ b/ordering/src/store/CartProvider.js @@ -1,12 +1,40 @@ +import { useReducer } from "react" import CartContext from "./cart-context" +const defaultCartState = { + items: [], + totalAmount: 0, +} + +const cartReducer = (state, action) => { + if (action.type === "ADD") { + const updatedItems = state.items.concat(action.item) + const updatedTotalAmount = + state.totalAmount + action.item.price * action.item.amount + return { + items: updatedItems, + totalAmount: updatedTotalAmount, + } + } + return defaultCartState +} + const CartProvider = (props) => { - const addItemToCartHandler = (item) => {} - const removeItemToCartHandler = (id) => {} + const [cartState, dispatchCartAction] = useReducer( + cartReducer, + defaultCartState + ) + + const addItemToCartHandler = (item) => { + dispatchCartAction({ type: "ADD", item }) + } + const removeItemToCartHandler = (id) => { + dispatchCartAction({ type: "REMOVE", id }) + } const cartContext = { - items: [], - totalAmount: 0, + items: cartState.items, + totalAmount: cartState.totalAmount, addItem: addItemToCartHandler, removeItem: removeItemToCartHandler, }