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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,7 @@
import React from "react";
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}`;
return (
<Link to={url}>

View file

@ -2,7 +2,7 @@ import React, { useEffect } from "react";
import { MealPresentation } from "../components/MealPresentation";
import { IngredientList } from "../components/IngredientList";
import { Recipe } from "../components/Recipe";
import { useParams, Redirect } from "react-router-dom";
import { useParams } from "react-router-dom";
export const Meal = props => {
const { getMeal } = props;
@ -12,50 +12,46 @@ export const Meal = props => {
idMeal === null ? getMeal() : getMeal(idMeal);
}, []);
if (props.meal.meals !== null) {
const meal = props.meal.meals[0];
const meal = props.meal.meals[0];
const {
strMeal,
strMealThumb,
strYoutube,
strCategory,
strArea,
strInstructions
} = meal;
const {
strMeal,
strMealThumb,
strYoutube,
strCategory,
strArea,
strInstructions
} = meal;
const item = {
mealName: strMeal,
imgAddress: strMealThumb,
videoAddress: strYoutube,
mealCategory: strCategory,
mealArea: strArea
};
const item = {
mealName: strMeal,
imgAddress: strMealThumb,
videoAddress: strYoutube,
mealCategory: strCategory,
mealArea: strArea
};
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]]);
}
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]]);
}
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 => {
return (
<div className="section">
<div className="container center-align">
<div className="container center-align">
<div className="row">
<h1>Wrong Way!</h1>
<img
src="https://images.otstatic.com/prod/26153735/2/large.jpg"
alt="404 not found"
/>
<div className="row">
<RandomButton handleClick={props.handleClick} />
<div className="col s12 offset-m3 m6">
<div className="card hoverable">
<div className="card-image">
<img
className="responsive-img"
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>