random recipe ok

This commit is contained in:
Ruidy Nemausat 2020-01-24 22:31:23 +01:00
parent 533e5dabc1
commit 935b47bbf8
7 changed files with 67 additions and 67 deletions

View file

@ -1,38 +1,9 @@
.App { body {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex; display: flex;
min-height: 100vh;
flex-direction: column; flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
} }
.App-link { main {
color: #61dafb; flex: 1 0 auto;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
} }

View file

@ -6,11 +6,11 @@ import SearchPage from "./pages/Search";
import NotFound from "./pages/NotFound"; import NotFound from "./pages/NotFound";
import Navbar from "./components/Navbar"; import Navbar from "./components/Navbar";
import Footer from "./components/Footer"; import Footer from "./components/Footer";
// import "./App.css";
const App = () => { const App = () => {
const [searchString, setSearchString] = useState(""); const [searchString, setSearchString] = useState("");
const mealDef = {
const mealItem = {
meals: [ meals: [
{ {
idMeal: "52837", idMeal: "52837",
@ -69,24 +69,48 @@ const App = () => {
} }
] ]
}; };
const [mealItem, setMeal] = useState(mealDef);
const URI = "https://www.themealdb.com/api/json/v1/1/random.php";
const getMeal = () => {
fetch(URI)
.then(response => response.json())
.then(mealItem => setMeal(mealItem));
};
// const { mealItem } = meal;
const handleChange = ev => { const handleChange = ev => {
const { value } = ev.target; const { value } = ev.target;
setSearchString(value); setSearchString(value);
}; };
const handleClick = () => {
getMeal();
};
return ( return (
<Router> <Router>
<Navbar searchString={searchString} handleChange={handleChange} /> <Navbar
searchString={searchString}
handleChange={handleChange}
handleClick={handleClick}
/>
<Switch> <Switch>
<Route exact path="/" component={HomePage} /> <Route
exact
path="/"
render={props => (
<HomePage {...props} getMeal={getMeal} handleClick={handleClick} />
)}
/>
<Route <Route
exact exact
path="/meal" path="/meal"
render={props => <MealPage {...props} meal={mealItem} />} render={props => <MealPage {...props} meal={mealItem} />}
/> />
<Route exact path="/search" component={SearchPage} /> <Route exact path="/search" component={SearchPage} />
{/* We'll have to input searchString somewhere */} {/* We'll have to input searchResults somewhere */}
<Route component={NotFound} /> <Route component={NotFound} />
</Switch> </Switch>
<Footer /> <Footer />

View file

@ -1,7 +1,14 @@
import React from "react"; import React from "react";
const CopyrightText = () => { const CopyrightText = () => {
return <span>© 2020 - Meal's Planner - Made with </span>; return (
<span>
© 2020 - Meal's Planner - Made with{" "}
<span role="img" aria-label="heart">
</span>
</span>
);
}; };
export default CopyrightText; export default CopyrightText;

View file

@ -19,7 +19,7 @@ const Navbar = props => {
/> />
</div> </div>
<div className="col s4"> <div className="col s4">
<RandomButton /> <RandomButton handleClick={props.handleClick} />
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,10 +1,15 @@
import React from "react"; import React from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
const RandomButton = () => { const RandomButton = props => {
return ( return (
<Link to="/meal"> <Link to="/meal">
<button class="waves-effect waves-light btn-large">Random Recipe</button> <button
class="waves-effect waves-light btn-large"
onClick={props.handleClick}
>
Random Recipe
</button>
</Link> </Link>
); );
}; };

View file

@ -1,20 +1,15 @@
import React, { Component } from "react"; import React from "react";
import RandomButton from "../components/RandomButton"; import RandomButton from "../components/RandomButton";
export default class HomePage extends Component { const HomePage = props => {
constructor(props) { return (
super(props); <div className="section">
this.initState = {}; <div className="container center-align">
this.state = this.initState; <h1>The Chef's meal suggestions</h1>
} <RandomButton handleClick={props.handleClick} />
render() {
return (
<div className="section">
<div className="container center-align">
<h1>Get a daily suggestion from the Chef</h1>
<RandomButton />
</div>
</div> </div>
); </div>
} );
} };
export default HomePage;

View file

@ -4,9 +4,9 @@ import IngredientList from "../components/IngredientList";
import Recipe from "../components/Recipe"; import Recipe from "../components/Recipe";
const MealPage = props => { const MealPage = props => {
const meal = props.meal.meals[0]; let meal = props.meal.meals[0];
const { let {
strMeal, strMeal,
strMealThumb, strMealThumb,
strYoutube, strYoutube,
@ -15,7 +15,7 @@ const MealPage = props => {
strInstructions strInstructions
} = meal; } = meal;
const item = { let item = {
mealName: strMeal, mealName: strMeal,
imgAddress: strMealThumb, imgAddress: strMealThumb,
videoAddress: strYoutube, videoAddress: strYoutube,
@ -23,13 +23,14 @@ const MealPage = props => {
mealArea: strArea mealArea: strArea
}; };
// Please improve this code …
let ingredientList = []; let ingredientList = [];
var i; var i;
for (i = 1; i <= 20; i++) { for (i = 1; i <= 20; i++) {
var strIng = `strIngredient${i}`; var strIng = `strIngredient${i}`;
var strMes = `strMeasure${i}`; var strMes = `strMeasure${i}`;
ingredientList.push([meal[strIng], meal[strMes]]); if (meal[strIng] !== "") {
ingredientList.push([meal[strIng], meal[strMes]]);
}
} }
return ( return (
@ -39,9 +40,6 @@ const MealPage = props => {
<div className="col s6"> <div className="col s6">
<IngredientList ingredients={ingredientList} /> <IngredientList ingredients={ingredientList} />
</div> </div>
{/* <p>
{meal["strIngredient9"]}: {meal["strMeasure9"]}
</p> */}
<div className="col s6"> <div className="col s6">
<Recipe recipe={strInstructions} /> <Recipe recipe={strInstructions} />
</div> </div>