From 362f3491c56601995be39b8c37106a3464c747bf Mon Sep 17 00:00:00 2001 From: ruben-9091 Date: Sat, 16 May 2026 11:24:13 +0200 Subject: [PATCH] done --- src/App.jsx | 33 +++++++++++------------------ src/components/Controls.jsx | 25 ++++++++++++++++------ src/components/Pizza.jsx | 26 +++++++++++------------ src/components/Price.jsx | 28 ++++++++++-------------- src/components/calculatePrice.jsx | 13 ++++++++++++ src/components/ingredientButton.jsx | 13 ++++++++++++ 6 files changed, 79 insertions(+), 59 deletions(-) create mode 100644 src/components/calculatePrice.jsx create mode 100644 src/components/ingredientButton.jsx diff --git a/src/App.jsx b/src/App.jsx index 3a4bcc3..8bc029c 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -6,33 +6,24 @@ import Pizza from './components/Pizza' import Footer from './components/Footer' function App() { - // TODO (Iteration 1): define the pizza state with useState. - // Each ingredient should start as `true` (all toppings visible by default). - // - // const [pizza, setPizza] = useState({ - // pepperoni: true, - // mushrooms: true, - // greenPeppers: true, - // whiteSauce: true, - // glutenFreeCrust: true, - // }) - // TODO (Iteration 1): implement a handleToggle(ingredient) function that flips - // the value of the given ingredient in the state and call setPizza with the - // new object. Pass it down to as the `onToggle` prop. +const [pizza, setPizza] = useState({ + pepperoni: true, + mushrooms: true, + greenPeppers: true, + whiteSauce: true, + glutenFreeCrust: true, +}) + +function handleToggle(ingredient) { + setPizza({...pizza, [ingredient]:!pizza[ingredient]}) +} - const pizza = { - pepperoni: true, - mushrooms: true, - greenPeppers: true, - whiteSauce: true, - glutenFreeCrust: true, - } return ( <>
- {}} /> +

diff --git a/src/components/Controls.jsx b/src/components/Controls.jsx index cc824d9..2124289 100644 --- a/src/components/Controls.jsx +++ b/src/components/Controls.jsx @@ -1,24 +1,35 @@ +import IngredientButton from "./ingredientButton" + + function Controls({ pizza, onToggle }) { - // TODO (Iteration 1+): wire each button's onClick to onToggle() - // TODO: add/remove the "active" class on each button based on the pizza state return (
  • - + onToggle("pepperoni")}> + Pepperoni +
  • - + onToggle("mushrooms")}> + Mushrooms +
  • - + onToggle("greenPeppers")}> + Green peppers +
  • - + onToggle("whiteSauce")}> + White sauce +
  • - + onToggle("glutenFreeCrust")}> + Gluten-free crust +
diff --git a/src/components/Pizza.jsx b/src/components/Pizza.jsx index 5ff6757..2c39a6b 100644 --- a/src/components/Pizza.jsx +++ b/src/components/Pizza.jsx @@ -7,33 +7,31 @@ import { function Pizza({ pizza }) { return (
- {/* Green peppers */} - {GREEN_PEPPER_POSITIONS.map((position) => ( + + {pizza.pepperoni && PEPPERONI_POSITIONS.map((position, index) => ( +
+ {index + 1} +
+ ))} + + {pizza.greenPeppers && GREEN_PEPPER_POSITIONS.map((position) => (
))} - {/* Mushrooms */} - {MUSHROOM_POSITIONS.map((position, index) => ( + {pizza.mushrooms && MUSHROOM_POSITIONS.map((position, index) => (
{index + 1}
))} - {/* Pepperoni */} - {PEPPERONI_POSITIONS.map((position, index) => ( -
- {index + 1} -
- ))} - - {/* Crust + Cheese + Sauce */} -
+ +
-
+
) diff --git a/src/components/Price.jsx b/src/components/Price.jsx index d89fc45..0619ac4 100644 --- a/src/components/Price.jsx +++ b/src/components/Price.jsx @@ -1,15 +1,9 @@ +import calculatePrice from "./calculatePrice" + function Price({ pizza }) { - // TODO (Iteration 4): - // 1. Hide each
  • when its ingredient is NOT active. - // 2. Compute the total price based on the pizza state and show it in . - // - // Prices: - // base (cheese pizza) -> $10 - // pepperoni -> $1 - // mushrooms -> $1 - // green peppers -> $1 - // white sauce -> $3 - // gluten-free crust -> $5 + + + return ( ) } diff --git a/src/components/calculatePrice.jsx b/src/components/calculatePrice.jsx new file mode 100644 index 0000000..6166036 --- /dev/null +++ b/src/components/calculatePrice.jsx @@ -0,0 +1,13 @@ +function calculatePrice(pizza) { + let total = 10; + + if (pizza.pepperoni) total += 1; + if (pizza.mushrooms) total += 1; + if (pizza.greenPeppers) total += 1; + if (pizza.whiteSauce) total += 3; + if (pizza.glutenFreeCrust) total += 5; + + return total; +} + +export default calculatePrice; \ No newline at end of file diff --git a/src/components/ingredientButton.jsx b/src/components/ingredientButton.jsx new file mode 100644 index 0000000..521aa73 --- /dev/null +++ b/src/components/ingredientButton.jsx @@ -0,0 +1,13 @@ +function IngredientButton ({ active, className, onClick, children }) { + return ( + + + ) +} + +export default IngredientButton \ No newline at end of file