mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
error Handling
This commit is contained in:
parent
b624122f13
commit
162857407d
9 changed files with 101 additions and 90 deletions
|
|
@ -83,6 +83,6 @@ Free meal planner for cooks short on ideas! (like me …)
|
||||||
## TO DO
|
## TO DO
|
||||||
|
|
||||||
- put a preloader
|
- put a preloader
|
||||||
- visual styling
|
|
||||||
- route bad request to notFOund (exple: /categories/string, when search results is null)
|
- route bad request to notFOund (exple: /categories/string, when search results is null)
|
||||||
- add sidenav on mobile
|
- add sidenav on mobile
|
||||||
|
- contact form
|
||||||
|
|
|
||||||
28
src/App.js
28
src/App.js
|
|
@ -1,5 +1,10 @@
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
|
import {
|
||||||
|
BrowserRouter as Router,
|
||||||
|
Switch,
|
||||||
|
Route,
|
||||||
|
Redirect
|
||||||
|
} from "react-router-dom";
|
||||||
import HomePage from "./pages/Home";
|
import HomePage from "./pages/Home";
|
||||||
import MealPage from "./pages/Meal";
|
import MealPage from "./pages/Meal";
|
||||||
import SearchPage from "./pages/Search";
|
import SearchPage from "./pages/Search";
|
||||||
|
|
@ -12,11 +17,10 @@ import Footer from "./components/Footer";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
const App = () => {
|
const App = () => {
|
||||||
// State Hooks
|
|
||||||
const [searchString, setSearchString] = useState("");
|
const [searchString, setSearchString] = useState("");
|
||||||
const [categories, setCategories] = useState({ categories: [] });
|
const [categories, setCategories] = useState({ categories: [] });
|
||||||
const [searchResults, setSearchResults] = useState({ meals: [] });
|
const [searchResults, setSearchResults] = useState({ meals: [] });
|
||||||
// const [isLoading, setIsLoading] = useState(true); For Preloader
|
// const [isLoading, setIsLoading] = useState(true); //For Preloader
|
||||||
// Default meal object. TODO: Find a better alternative …
|
// Default meal object. TODO: Find a better alternative …
|
||||||
const mealDef = {
|
const mealDef = {
|
||||||
meals: [
|
meals: [
|
||||||
|
|
@ -79,7 +83,6 @@ const App = () => {
|
||||||
};
|
};
|
||||||
const [meal, setMeal] = useState(mealDef);
|
const [meal, setMeal] = useState(mealDef);
|
||||||
|
|
||||||
// Fetch API functions
|
|
||||||
const createURI = (keyword, option) => {
|
const createURI = (keyword, option) => {
|
||||||
const ROOT = "https://www.themealdb.com/api/json/v1/1/";
|
const ROOT = "https://www.themealdb.com/api/json/v1/1/";
|
||||||
if (option === null) {
|
if (option === null) {
|
||||||
|
|
@ -100,11 +103,8 @@ const App = () => {
|
||||||
.then(data => set(data));
|
.then(data => set(data));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch wrappers for each use
|
|
||||||
const getRandomMeal = () => {
|
const getRandomMeal = () => {
|
||||||
// setIsLoading(true);
|
|
||||||
getData("random", setMeal);
|
getData("random", setMeal);
|
||||||
// setIsLoading(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMeal = id => {
|
const getMeal = id => {
|
||||||
|
|
@ -123,6 +123,7 @@ const App = () => {
|
||||||
const { value } = ev.target;
|
const { value } = ev.target;
|
||||||
setSearchString(value);
|
setSearchString(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const buttonUrl = "/random";
|
const buttonUrl = "/random";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -146,14 +147,14 @@ const App = () => {
|
||||||
// isLoading={isLoading}
|
// isLoading={isLoading}
|
||||||
/>
|
/>
|
||||||
</Route>
|
</Route>
|
||||||
/>
|
|
||||||
<Route exact path="/categories">
|
<Route exact path="/categories">
|
||||||
<CategoryListPage
|
<CategoryListPage
|
||||||
categories={categories}
|
categories={categories}
|
||||||
getCategories={getCategories}
|
getCategories={getCategories}
|
||||||
/>
|
/>
|
||||||
</Route>
|
</Route>
|
||||||
/>
|
|
||||||
<Route path="/categories/:strCategory/">
|
<Route path="/categories/:strCategory/">
|
||||||
<CategoryPage
|
<CategoryPage
|
||||||
getData={getData}
|
getData={getData}
|
||||||
|
|
@ -168,13 +169,16 @@ const App = () => {
|
||||||
searchResults={searchResults}
|
searchResults={searchResults}
|
||||||
/>
|
/>
|
||||||
</Route>
|
</Route>
|
||||||
|
|
||||||
|
<Route path="/404">
|
||||||
|
<NotFound handleClick={getRandomMeal} />
|
||||||
|
</Route>
|
||||||
<Route path="/:idMeal">
|
<Route path="/:idMeal">
|
||||||
<MealPage meal={meal} getMeal={getMeal} />
|
<MealPage meal={meal} getMeal={getMeal} />
|
||||||
</Route>
|
</Route>
|
||||||
<Route>
|
<Route path="*">
|
||||||
<NotFound handleClick={getRandomMeal} />
|
<Redirect to="/404" />
|
||||||
</Route>
|
</Route>
|
||||||
/>
|
|
||||||
</Switch>
|
</Switch>
|
||||||
<Footer />
|
<Footer />
|
||||||
</Router>
|
</Router>
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@ import { Link } from "react-router-dom";
|
||||||
|
|
||||||
const Logo = () => {
|
const Logo = () => {
|
||||||
return (
|
return (
|
||||||
<logo>
|
<div className="logo">
|
||||||
<Link to="/" className="brand-logo">
|
<Link to="/" className="brand-logo">
|
||||||
<span role="img" aria-label="cookie">
|
<span role="img" aria-label="cookie">
|
||||||
👩🍳 Chef's
|
👩🍳 Chef's
|
||||||
</span>
|
</span>
|
||||||
</Link>
|
</Link>
|
||||||
</logo>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,6 @@ div {
|
||||||
/* width: 100%; */
|
/* width: 100%; */
|
||||||
}
|
}
|
||||||
|
|
||||||
logo {
|
.logo {
|
||||||
font-family: "Marck Script", cursive;
|
font-family: "Marck Script", cursive;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,4 @@ import * as serviceWorker from "./serviceWorker";
|
||||||
|
|
||||||
ReactDOM.render(<App />, document.getElementById("root"));
|
ReactDOM.render(<App />, document.getElementById("root"));
|
||||||
|
|
||||||
serviceWorker.unregister();
|
serviceWorker.register();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { useParams, Link, useRouteMatch } from "react-router-dom";
|
import { useParams, Link, Redirect } from "react-router-dom";
|
||||||
import CardEntry from "../components/CardEntry";
|
|
||||||
|
|
||||||
const CategoryPage = props => {
|
const CategoryPage = props => {
|
||||||
const [meals, setMeals] = useState({ meals: [] });
|
const [meals, setMeals] = useState({ meals: [] });
|
||||||
|
|
@ -15,33 +14,34 @@ const CategoryPage = props => {
|
||||||
getMeals();
|
getMeals();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const { url } = useRouteMatch();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<h1>Chef's {strCategory} Recipes</h1>
|
<h1>Chef's {strCategory} Recipes</h1>
|
||||||
|
{meals.meals === null ? (
|
||||||
<ul>
|
<Redirect to="/404" />
|
||||||
{meals.meals.map((meal, i) => (
|
) : (
|
||||||
<li key={i}>
|
<ul>
|
||||||
{/* <CardEntry item={meal} /> */}
|
{meals.meals.map((meal, i) => (
|
||||||
<Link to={`/${meal.idMeal}`}>
|
<li key={i}>
|
||||||
<div className="row">
|
{/* <CardEntry item={meal} /> */}
|
||||||
<div className="col s12 m6">
|
<Link to={`/meal/${meal.idMeal}`}>
|
||||||
<div className="card ">
|
<div className="row">
|
||||||
<div className="card-image">
|
<div className="col s12 m6">
|
||||||
<img src={meal.strMealThumb} alt={meal.strMeal} />
|
<div className="card ">
|
||||||
</div>
|
<div className="card-image">
|
||||||
<div class="card-content">
|
<img src={meal.strMealThumb} alt={meal.strMeal} />
|
||||||
<h4>{meal.strMeal}</h4>
|
</div>
|
||||||
|
<div className="card-content">
|
||||||
|
<h4>{meal.strMeal}</h4>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</Link>
|
||||||
</Link>
|
</li>
|
||||||
</li>
|
))}
|
||||||
))}
|
</ul>
|
||||||
</ul>
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,7 @@ const HomePage = props => {
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<div className="col s12 m6">
|
<div className="col s12 m6">
|
||||||
<h1>The Chef's Meal Suggestions</h1>
|
<h1>The Chef's Meal Suggestions</h1>
|
||||||
<RandomButton
|
<RandomButton url={props.buttonUrl} size="large" />
|
||||||
handleClick={props.handleClick}
|
|
||||||
url={props.buttonUrl}
|
|
||||||
size="large"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<div className="col s12 m6">
|
<div className="col s12 m6">
|
||||||
<img
|
<img
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,10 @@ import React, { useEffect } from "react";
|
||||||
import MealPresentation from "../components/MealPresentation";
|
import MealPresentation from "../components/MealPresentation";
|
||||||
import IngredientList from "../components/IngredientList";
|
import IngredientList from "../components/IngredientList";
|
||||||
import Recipe from "../components/Recipe";
|
import Recipe from "../components/Recipe";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams, Redirect } from "react-router-dom";
|
||||||
// import PreLoader from "../components/PreLoader";
|
// import PreLoader from "../components/PreLoader";
|
||||||
|
|
||||||
const MealPage = props => {
|
const MealPage = props => {
|
||||||
const meal = props.meal.meals[0];
|
|
||||||
const { getMeal } = props;
|
const { getMeal } = props;
|
||||||
const { idMeal } = useParams();
|
const { idMeal } = useParams();
|
||||||
|
|
||||||
|
|
@ -14,48 +13,53 @@ const MealPage = props => {
|
||||||
idMeal === null ? getMeal() : getMeal(idMeal);
|
idMeal === null ? getMeal() : getMeal(idMeal);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const {
|
if (props.meal.meals !== null) {
|
||||||
strMeal,
|
const meal = props.meal.meals[0];
|
||||||
strMealThumb,
|
|
||||||
strYoutube,
|
|
||||||
strCategory,
|
|
||||||
strArea,
|
|
||||||
strInstructions
|
|
||||||
} = meal;
|
|
||||||
|
|
||||||
const item = {
|
const {
|
||||||
mealName: strMeal,
|
strMeal,
|
||||||
imgAddress: strMealThumb,
|
strMealThumb,
|
||||||
videoAddress: strYoutube,
|
strYoutube,
|
||||||
mealCategory: strCategory,
|
strCategory,
|
||||||
mealArea: strArea
|
strArea,
|
||||||
};
|
strInstructions
|
||||||
|
} = meal;
|
||||||
|
|
||||||
let ingredientList = [];
|
const item = {
|
||||||
var i;
|
mealName: strMeal,
|
||||||
for (i = 1; i <= 20; i++) {
|
imgAddress: strMealThumb,
|
||||||
var strIng = `strIngredient${i}`;
|
videoAddress: strYoutube,
|
||||||
var strMes = `strMeasure${i}`;
|
mealCategory: strCategory,
|
||||||
if (meal[strIng] !== "" && meal[strIng] !== null) {
|
mealArea: strArea
|
||||||
ingredientList.push([meal[strIng], meal[strMes]]);
|
};
|
||||||
|
|
||||||
|
let ingredientList = [];
|
||||||
|
var i;
|
||||||
|
for (i = 1; i <= 20; i++) {
|
||||||
|
var strIng = `strIngredient${i}`;
|
||||||
|
var strMes = `strMeasure${i}`;
|
||||||
|
if (meal[strIng] !== "" && meal[strIng] !== null) {
|
||||||
|
ingredientList.push([meal[strIng], meal[strMes]]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// const page =
|
// return isLoading ? <PreLoader /> : page;
|
||||||
|
return (
|
||||||
// return isLoading ? <PreLoader /> : page;
|
<div className="container">
|
||||||
return (
|
<div className="row">
|
||||||
<div className="container">
|
<div className="col s12 m6">
|
||||||
<div className="row">
|
<MealPresentation meal={item} />
|
||||||
<div className="col s12 m6">
|
</div>
|
||||||
<MealPresentation meal={item} />
|
<div className="col s12 m6">
|
||||||
</div>
|
<IngredientList ingredients={ingredientList} />
|
||||||
<div className="col s12 m6">
|
<Recipe recipe={strInstructions} />
|
||||||
<IngredientList ingredients={ingredientList} />
|
</div>
|
||||||
<Recipe recipe={strInstructions} />
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
);
|
||||||
);
|
} else {
|
||||||
|
return <Redirect to="/404" />;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default MealPage;
|
export default MealPage;
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,24 @@ import React from "react";
|
||||||
import SearchResult from "../components/SearchResult";
|
import SearchResult from "../components/SearchResult";
|
||||||
|
|
||||||
const SearchPage = props => {
|
const SearchPage = props => {
|
||||||
const { meals } = props.searchResults;
|
let { meals } = props.searchResults;
|
||||||
const { searchString } = props;
|
const { searchString } = props;
|
||||||
|
if (meals === null) {
|
||||||
|
meals = [];
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<h1>Search Results for: {searchString} </h1>
|
<h1>Search Results for: {searchString} </h1>
|
||||||
<ul>
|
{meals[0] === undefined ? (
|
||||||
{meals.map((meal, i) => (
|
<p> Nothing ! Create a component to illustrate emptiness</p>
|
||||||
<SearchResult key={i} meal={meal} />
|
) : (
|
||||||
))}
|
<ul>
|
||||||
</ul>
|
{meals.map((meal, i) => (
|
||||||
|
<SearchResult key={i} meal={meal} />
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue