Skip to content

Adds ability to search for recipe & search for errors #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import React, { Component } from "react";
import "./App.css";
import { Recipe } from "./Components/Recipe";
import { Search } from "./Components/Search";

const APP_ID = "fc6f60d3";
const APP_KEY = "bd93df5272c4eb7bc29fd506cea911b9";
const BASE_ENDPOINT = `https://api.edamam.com/search?app_id=${APP_ID}&app_key=${APP_KEY}`;

const withErrorHandling = WrappedComponent => ({ error, children }) => {
return (
<WrappedComponent>
{error ? <div className="error-message">Error! {error}</div> : children}
</WrappedComponent>
);
};

const ErrorWrapper = withErrorHandling(({ children }) => <div>{children}</div>);

class App extends Component {
constructor() {
super();
this.state = {
error: false,
recipeList: [],
query: ""
};
}

callApiWithQuery = query => {
fetch(`${BASE_ENDPOINT}&q=${query}`)
.then(response => response.json())
.then(
response =>
response.hits.length > 0
? this.setState({ recipeList: [...response.hits], error: null })
: this.setState({ error: "Nothing to see here!", recipeList: "" })
)
.catch(error => this.setState({ error: error.message }));
};

componentDidMount() {
this.callApiWithQuery("cake");
}

updateSearch = e => {
this.setState({ query: e.target.value });
this.callApiWithQuery(this.state.query);
};

render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<Search onChange={this.updateSearch} value={this.state.query} />
<ErrorWrapper error={this.state.error}>
{this.state.recipeList &&
this.state.recipeList.map(recipe => (
<Recipe
name={recipe.recipe.label}
calories={recipe.recipe.calories}
key={recipe.recipe.url}
/>
))}
</ErrorWrapper>
</div>
);
}
Expand Down
11 changes: 11 additions & 0 deletions src/Components/Recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

export const Recipe = ({ name, calories }) => {
return (
<div key={name}>
Name: {name}
<br />
Calories: {calories}
</div>
);
};
5 changes: 5 additions & 0 deletions src/Components/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from "react";

export const Search = ({ onChange, value }) => {
return <input value={value} onChange={e => onChange(e)} />;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful here. When binding an input like this, anyone can pass any function here. Like an API call 😉. What happens is that when a user types, they can keep typing, and each type will fire an independent API call. This means for hello we have 5 API requests and they all return at different times because APIs take their own time to return.

One way to fix this problem is to use a function that keeps track, or holds on to the call for a certain amount of time, until either, the timer runs out, or the function is called again. This is traditionally called a debounce function. Its pretty normal to bind one to an input.

};
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5126,7 +5126,7 @@ react-dev-utils@^5.0.0:
strip-ansi "3.0.1"
text-table "0.2.0"

[email protected]:
react-dom@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044"
dependencies:
Expand Down Expand Up @@ -5183,7 +5183,7 @@ [email protected]:
optionalDependencies:
fsevents "^1.1.3"

[email protected]:
react@^16.2.0:
version "16.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba"
dependencies:
Expand Down