add to cart

This commit is contained in:
Tyrel Souza 2022-09-26 13:18:59 -04:00
parent 6915852524
commit 135e4a9a1d
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
4 changed files with 77 additions and 11 deletions

View File

@ -1,8 +1,19 @@
import { useContext } from "react"
import classes from "./MealItem.module.css" import classes from "./MealItem.module.css"
import MealItemForm from "./MealItemForm" import MealItemForm from "./MealItemForm"
import CartContext from "../../../store/cart-context"
function MealItem(props) { 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 ( return (
<li className={classes.meal}> <li className={classes.meal}>
<div> <div>
@ -11,7 +22,10 @@ function MealItem(props) {
<div className={classes.price}>{price}</div> <div className={classes.price}>{price}</div>
</div> </div>
<div> <div>
<MealItemForm id={props.id}/> <MealItemForm
id={props.id}
onAddToCart={addItemToCartHandler}
/>
</div> </div>
</li> </li>
) )

View File

@ -1,13 +1,35 @@
import { useRef, useState } from "react"
import classes from "./MealItemForm.module.css" import classes from "./MealItemForm.module.css"
import Input from "../../UI/Input" import Input from "../../UI/Input"
function MealItemForm(props) { 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 ( return (
<form className={classes.form}> <form className={classes.form} onSubmit={submitHandler}>
<Input <Input
ref={amountInputRef}
label="Amount" label="Amount"
input={{ input={{
id: "amount_" + props.id , id: "amount_" + props.id,
type: "number", type: "number",
min: "1", min: "1",
max: "5", max: "5",
@ -16,6 +38,7 @@ function MealItemForm(props) {
}} }}
/> />
<button>+ Add</button> <button>+ Add</button>
{!amountIsValid && <p>Please enter a valid amount (1-5)</p>}
</form> </form>
) )
} }

View File

@ -1,11 +1,12 @@
import React from "react"
import classes from "./Input.module.css" import classes from "./Input.module.css"
function Input(props) { const Input = React.forwardRef((props, ref) => {
return ( return (
<div className={classes.input}> <div className={classes.input}>
<label htmlFor={props.input.id}>{props.label}</label> <label htmlFor={props.input.id}>{props.label}</label>
<input {...props.input} /> <input ref={ref} {...props.input} />
</div> </div>
) )
} })
export default Input export default Input

View File

@ -1,12 +1,40 @@
import { useReducer } from "react"
import CartContext from "./cart-context" 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 CartProvider = (props) => {
const addItemToCartHandler = (item) => {} const [cartState, dispatchCartAction] = useReducer(
const removeItemToCartHandler = (id) => {} cartReducer,
defaultCartState
)
const addItemToCartHandler = (item) => {
dispatchCartAction({ type: "ADD", item })
}
const removeItemToCartHandler = (id) => {
dispatchCartAction({ type: "REMOVE", id })
}
const cartContext = { const cartContext = {
items: [], items: cartState.items,
totalAmount: 0, totalAmount: cartState.totalAmount,
addItem: addItemToCartHandler, addItem: addItemToCartHandler,
removeItem: removeItemToCartHandler, removeItem: removeItemToCartHandler,
} }