Skip to content

Recipes fecthing and searching #3

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 5 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
44 changes: 35 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { BASE_ENDPOINT } from './data/API_Constants.js'
import FilterInput from './components/FilterInput'
import FilterButton from './components/FilterButton'
import Content from './components/Content'

class App extends Component {
constructor(props) {
super(props)

this.state = {
recipes: [],
error: '',
searchTerm: ''
}
}

defaultSearch = 'cake'

componentDidMount = () => {
this.setState({ searchTerm: this.defaultSearch }, () => this.search(this.state.searchTerm))
}

setSearchTerm = ev => this.setState({ searchTerm: ev.target.value, error: '', recipes: [] })

search = term => {
fetch(`${BASE_ENDPOINT}&q=${term}`)
.then(respone => respone.json())
.then(response => response.hits.length > 0 ? this.setState({ recipes: [...response.hits] }) : this.setState({ error: "No recipes found" }))
.catch(error => this.setState({ error: error.message }))
}


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>
<div>
<FilterInput value={this.state.searchTerm} onChange={this.setSearchTerm} />
<FilterButton onClick={() => this.search(this.state.searchTerm)}>Search</FilterButton>
<Content list={this.state.recipes} error={this.state.error} />
</div>
);
)
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/components/Content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { Component } from 'react'
import RecipeList from './RecipeList'
import Error from './Error'

export default class Content extends Component {
render() {
const { list, error } = this.props

if (list.length > 0) {
return (
<RecipeList data={list} />
);
}
else {
return (
<Error message={error} />
)
}
}
}
9 changes: 9 additions & 0 deletions src/components/Error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { Component } from 'react'

export default class Error extends Component {
render() {
return (
<div>{this.props.message}</div>
)
}
}
9 changes: 9 additions & 0 deletions src/components/FilterButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { Component } from 'react'

export default class FilterButton extends Component {
render() {
return (
<button onClick={this.props.onClick}>{this.props.children}</button>
)
}
}
9 changes: 9 additions & 0 deletions src/components/FilterInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { Component } from 'react'

export default class FilterInput extends Component {
render() {
return (
<input value={this.props.value} onChange={this.props.onChange} />
)
}
}
14 changes: 14 additions & 0 deletions src/components/RecipeCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Component } from 'react'

export default class RecipeCard extends Component {
render() {
const { label, calories } = this.props.data.recipe

return (
<li>
<div><label>Recipe: </label>{label}</div>
<div><label>Calories: </label>{Math.round(calories * 100) / 100}</div>
</li>
)
}
}
12 changes: 12 additions & 0 deletions src/components/RecipeList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { Component } from 'react'
import RecipeCard from './RecipeCard'

export default class RecipeList extends Component {
render() {
return (
<div>
<ul>{this.props.data.map((item, index) => <RecipeCard data={item} />)}</ul>
</div>
)
}
}
6 changes: 6 additions & 0 deletions src/data/API_Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const APP_ID = 'a0654b69';
const APP_KEY = '2ce30d2c76c4084e15a9264fcb77b7c0';

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

export { BASE_ENDPOINT }
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