diff --git a/src/App.js b/src/App.js index 203067e..429c1ff 100644 --- a/src/App.js +++ b/src/App.js @@ -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 ( -
-
- logo -

Welcome to React

-
-

- To get started, edit src/App.js and save to reload. -

+
+ + this.search(this.state.searchTerm)}>Search +
- ); + ) } } diff --git a/src/components/Content.js b/src/components/Content.js new file mode 100644 index 0000000..9e12847 --- /dev/null +++ b/src/components/Content.js @@ -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 ( + + ); + } + else { + return ( + + ) + } + } +} diff --git a/src/components/Error.js b/src/components/Error.js new file mode 100644 index 0000000..b7ad544 --- /dev/null +++ b/src/components/Error.js @@ -0,0 +1,9 @@ +import React, { Component } from 'react' + +export default class Error extends Component { + render() { + return ( +
{this.props.message}
+ ) + } +} diff --git a/src/components/FilterButton.js b/src/components/FilterButton.js new file mode 100644 index 0000000..ec94b19 --- /dev/null +++ b/src/components/FilterButton.js @@ -0,0 +1,9 @@ +import React, { Component } from 'react' + +export default class FilterButton extends Component { + render() { + return ( + + ) + } +} diff --git a/src/components/FilterInput.js b/src/components/FilterInput.js new file mode 100644 index 0000000..1816b9c --- /dev/null +++ b/src/components/FilterInput.js @@ -0,0 +1,9 @@ +import React, { Component } from 'react' + +export default class FilterInput extends Component { + render() { + return ( + + ) + } +} diff --git a/src/components/RecipeCard.js b/src/components/RecipeCard.js new file mode 100644 index 0000000..9b6edde --- /dev/null +++ b/src/components/RecipeCard.js @@ -0,0 +1,14 @@ +import React, { Component } from 'react' + +export default class RecipeCard extends Component { + render() { + const { label, calories } = this.props.data.recipe + + return ( +
  • +
    {label}
    +
    {Math.round(calories * 100) / 100}
    +
  • + ) + } +} diff --git a/src/components/RecipeList.js b/src/components/RecipeList.js new file mode 100644 index 0000000..d410b10 --- /dev/null +++ b/src/components/RecipeList.js @@ -0,0 +1,12 @@ +import React, { Component } from 'react' +import RecipeCard from './RecipeCard' + +export default class RecipeList extends Component { + render() { + return ( +
    +
      {this.props.data.map((item, index) => )}
    +
    + ) + } +} diff --git a/src/data/API_Constants.js b/src/data/API_Constants.js new file mode 100644 index 0000000..051325a --- /dev/null +++ b/src/data/API_Constants.js @@ -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 } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 4713834..164bfc4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5126,7 +5126,7 @@ react-dev-utils@^5.0.0: strip-ansi "3.0.1" text-table "0.2.0" -react-dom@16.2.0: +react-dom@^16.2.0: version "16.2.0" resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.2.0.tgz#69003178601c0ca19b709b33a83369fe6124c044" dependencies: @@ -5183,7 +5183,7 @@ react-scripts@1.1.1: optionalDependencies: fsevents "^1.1.3" -react@16.2.0: +react@^16.2.0: version "16.2.0" resolved "https://registry.yarnpkg.com/react/-/react-16.2.0.tgz#a31bd2dab89bff65d42134fa187f24d054c273ba" dependencies: