navigation error handling

This commit is contained in:
Ruidy Nemausat 2020-01-31 08:24:30 +01:00
parent fd3362a169
commit 12fdc42bd5
8 changed files with 80 additions and 62 deletions

View file

@ -96,9 +96,5 @@ Free meal planner for cooks short on ideas! (like me …)
- send message after contact form validation (confirm to sender and msg+info to admin) - send message after contact form validation (confirm to sender and msg+info to admin)
- code cleanup (props and refactoring) - code cleanup (props and refactoring)
- put a preloader - put a preloader
- redirect after failed fetch request: (history.push('/path'), or write handleFetchResponse function)
- https://stackoverflow.com/questions/45089386/what-is-the-best-way-to-redirect-a-page-using-react-router
- https://www.henriksommerfeld.se/error-handling-with-fetch/
- Use ErrorBoundaries component ?
- Back to top button - Back to top button
- Take a look at some components [here](http://react-materialize.github.io/react-materialize/?path=/story/css-grid--default) - Take a look at some components [here](http://react-materialize.github.io/react-materialize/?path=/story/css-grid--default)

View file

@ -106,24 +106,33 @@ export const App = () => {
return ( return (
<Router> <Router>
<Navbar handleClick={getRandomMeal} buttonUrl={buttonUrl} /> <Navbar handleClick={getRandomMeal} buttonUrl={buttonUrl} />
<SearchBar <SearchBar
searchString={searchString} searchString={searchString}
handleChange={handleChange} handleChange={handleChange}
onSubmit={getSearchResults} onSubmit={getSearchResults}
/> />
<Switch> <Switch>
<Route exact path="/"> <Route exact path="/">
<Home buttonUrl={buttonUrl} /> <Home buttonUrl={buttonUrl} />
</Route> </Route>
<Route exact path={buttonUrl}> <Route exact path={buttonUrl}>
<Meal meal={meal} getMeal={getRandomMeal} /> {meal !== undefined && meal.meals !== null ? (
<Meal meal={meal} getMeal={getRandomMeal} />
) : (
<NotFoundPage handleClick={getRandomMeal} />
)}
</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}
@ -132,25 +141,35 @@ export const App = () => {
meal={meal} meal={meal}
/> />
</Route> </Route>
<Route exact path="/search"> <Route exact path="/search">
<SearchPage <SearchPage
searchString={searchString} searchString={searchString}
searchResults={searchResults} searchResults={searchResults}
/> />
</Route> </Route>
<Route path="/contact"> <Route path="/contact">
<ContactPage /> <ContactPage />
</Route> </Route>
<Route path="/404"> <Route path="/404">
<NotFoundPage handleClick={getRandomMeal} /> <NotFoundPage handleClick={getRandomMeal} />
</Route> </Route>
<Route path="/:idMeal"> <Route path="/:idMeal">
<Meal meal={meal} getMeal={getMeal} /> {meal !== undefined && meal.meals !== null ? (
<Meal meal={meal} getMeal={getMeal} />
) : (
<NotFoundPage handleClick={getRandomMeal} />
)}
</Route> </Route>
<Route path="*"> <Route path="*">
<Redirect to="/404" /> <Redirect to="/404" />
</Route> </Route>
</Switch> </Switch>
<Footer /> <Footer />
</Router> </Router>
); );

View file

@ -11,10 +11,10 @@ export const Footer = () => {
<div className="row"> <div className="row">
<div className="container"> <div className="container">
<div className=" s12"> <div className=" s12">
<h5 className="white-text">Links</h5> <h5 className="white-text">Navigation</h5>
<ul> <ul>
{links.map((link, i) => ( {links.map((link, i) => (
<FooterLink i={i} link={link} /> <FooterLink key={i} link={link} />
))} ))}
</ul> </ul>
</div> </div>

View file

@ -2,9 +2,9 @@ import React from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import { upFirstChar } from "../utils/methods"; import { upFirstChar } from "../utils/methods";
export const FooterLink = ({ link, i }) => { export const FooterLink = ({ link }) => {
return ( return (
<li key={i}> <li>
<Link className="grey-text text-lighten-3" to={`/${link}`}> <Link className="grey-text text-lighten-3" to={`/${link}`}>
{upFirstChar(link)} {upFirstChar(link)}
</Link> </Link>

View file

@ -14,7 +14,7 @@ export const Navbar = props => {
<Logo /> <Logo />
<ul id="nav-mobile" className="right hide-on-med-and-down"> <ul id="nav-mobile" className="right hide-on-med-and-down">
{links.map((link, i) => ( {links.map((link, i) => (
<FooterLink i={i} link={link} /> <FooterLink key={i} link={link} />
))} ))}
<li> <li>
<RandomButton <RandomButton

View file

@ -1,7 +1,7 @@
import React from "react"; import React from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
export const RandomButton = ({ url, size, handleClick }) => { export const RandomButton = ({ url, size = "large", handleClick }) => {
const classString = `waves-effect waves-light btn-${size}`; const classString = `waves-effect waves-light btn-${size}`;
return ( return (
<Link to={url}> <Link to={url}>

View file

@ -2,7 +2,7 @@ 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, Redirect } from "react-router-dom"; import { useParams } from "react-router-dom";
export const Meal = props => { export const Meal = props => {
const { getMeal } = props; const { getMeal } = props;
@ -12,50 +12,46 @@ export const Meal = props => {
idMeal === null ? getMeal() : getMeal(idMeal); idMeal === null ? getMeal() : getMeal(idMeal);
}, []); }, []);
if (props.meal.meals !== null) { const meal = props.meal.meals[0];
const meal = props.meal.meals[0];
const { const {
strMeal, strMeal,
strMealThumb, strMealThumb,
strYoutube, strYoutube,
strCategory, strCategory,
strArea, strArea,
strInstructions strInstructions
} = meal; } = meal;
const item = { const item = {
mealName: strMeal, mealName: strMeal,
imgAddress: strMealThumb, imgAddress: strMealThumb,
videoAddress: strYoutube, videoAddress: strYoutube,
mealCategory: strCategory, mealCategory: strCategory,
mealArea: strArea mealArea: strArea
}; };
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}`;
if (meal[strIng] !== "" && meal[strIng] !== null) { if (meal[strIng] !== "" && meal[strIng] !== null) {
ingredientList.push([meal[strIng], meal[strMes]]); ingredientList.push([meal[strIng], meal[strMes]]);
}
} }
return (
<div className="container">
<div className="row">
<div className="col s12 m6">
<MealPresentation meal={item} />
</div>
<div className="col s12 m6">
<IngredientList ingredients={ingredientList} />
</div>
</div>
<Recipe recipe={strInstructions} />
</div>
);
} else {
return <Redirect to="/404" />;
} }
return (
<div className="container">
<div className="row">
<div className="col s12 m6">
<MealPresentation meal={item} />
</div>
<div className="col s12 m6">
<IngredientList ingredients={ingredientList} />
</div>
</div>
<Recipe recipe={strInstructions} />
</div>
);
}; };

View file

@ -3,15 +3,22 @@ import { RandomButton } from "../components/RandomButton";
export const NotFoundPage = props => { export const NotFoundPage = props => {
return ( return (
<div className="section"> <div className="container center-align">
<div className="container center-align"> <div className="row">
<h1>Wrong Way!</h1> <h1>Wrong Way!</h1>
<img <div className="col s12 offset-m3 m6">
src="https://images.otstatic.com/prod/26153735/2/large.jpg" <div className="card hoverable">
alt="404 not found" <div className="card-image">
/> <img
<div className="row"> className="responsive-img"
<RandomButton handleClick={props.handleClick} /> src="https://images.otstatic.com/prod/26153735/2/large.jpg"
alt="404 not found"
/>
</div>
<div className="card-content">
<RandomButton url="/random" />
</div>
</div>
</div> </div>
</div> </div>
</div> </div>