From 6bb836c14a52462f0a9092d971fce9c1ac78e282 Mon Sep 17 00:00:00 2001 From: Eray C Date: Mon, 1 Nov 2021 23:44:30 +0100 Subject: [PATCH] feat: Add limits, displays, use React hooks --- src/App.jsx | 103 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 78 insertions(+), 25 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 2ac9e31..4fc6ea5 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,4 +1,4 @@ -import * as React from "react"; +import * as React from "react"; import "./index.css"; // Components @@ -9,78 +9,131 @@ import { Button } from "./components/Button"; import { defaultOrder, defaultBreads, defaultMeats, defaultVegetables, defaultSauces } from "./data"; -class App extends React.Component { - constructor() { - super(); - this.state = { currentOrder: defaultOrder, isLoading: true, timerHandle: null, +function App() { + const [currentOrder, setCurrentOrder] = React.useState(defaultOrder); + const [isLoading, setIsLoading] = React.useState(true); + const [timerHandle, setTimerHandle] = React.useState(null); + const [orderDisplay, setOrderDisplay] = React.useState({bread: true, meat: false, vegetables: false, sauces: false}); + const [orderLimited, setOrderLimited] = React.useState({"vegetables": { limited: false, amount: 3}, "sauces": { limited: false, amount: 2}}); + + React.useEffect(() => { + setTimerHandle(setTimeout(() => setIsLoading(false), 2500)); + return () => { + clearTimeout(timerHandle); + setTimerHandle(null); }; + }, [setTimerHandle]); + + if (isLoading) { return (
its loading
); } + + const handleOnSelected = (ingredient, value, display) => { + const newOrder = {...currentOrder}; + newOrder[ingredient] = value; + setCurrentOrder(newOrder); + if (display) { handleDisplay(display.hide, display.show); } + } + + const handleOnAdded = (ingredient, value) => { + if (handleOrderLimited(ingredient)) { return false }; + const newOrder = {...currentOrder}; + newOrder[ingredient].push(value); + setCurrentOrder(newOrder); } - componentDidMount() { this.timerHandle = setTimeout(() => this.setState({ isLoading: false }), 2500); } - componentWillUnmount() { - if (this.timerHandle) { - clearTimeout(this.timerHandle); - this.timerHandle = null; + const handleOrderLimited = (ingredient) => { + const newLimit = {...orderLimited}; + if (currentOrder[ingredient].length === orderLimited[ingredient].amount) { + console.log("limited: ", currentOrder[ingredient].length, orderLimited[ingredient].amount); + newLimit[ingredient].limited = true; + setOrderLimited(newLimit); + return true; } + // console.log("not limited:", currentOrder[ingredient].length, orderLimited[ingredient].amount); + return false; + } + + const handleDisplay = (toHide, toShow) => { + // Used to toggle the display of vegetables & sauces + const canDisplay = {...orderDisplay}; + canDisplay[toHide] = false; + canDisplay[toShow] = true; + setOrderDisplay(canDisplay); } - render() { - if (this.state.isLoading) { return (
its loading
); } - return (
+ {console.log(currentOrder)} + {console.log(orderLimited)} {/* Choose Bread */} -
+
+
Choisissez le pain
{defaultBreads.map((bread) => ( -
+
handleOnSelected('bread', bread.name, {hide:'bread', show:'meat'})}>
))}
-
{/* Choose Meat */} -
+
+
Passons à la viande... ou au tofu ?
{defaultMeats.map((meat) => ( -
+
handleOnSelected('meat', meat.name, {hide:'meat', show:'vegetables'})}>
))}
-
{/* Choose Vegetables */} -
+
Des crudités pour votre merveille ?
{defaultVegetables.map((veg) => ( -
- +
handleOnAdded('vegetables', veg.name)}> + handleOrderLimited('vegetables')} />
))}
-
+ +
handleDisplay('vegetables', 'sauces')}> +
{/* Choose Sauces */} -
+
Des sauces pour donner de la couleur à cette magie ?
@@ -97,6 +150,6 @@ class App extends React.Component {
); -}}; +}; export default App;