diff --git a/src/App.css b/src/App.css index c5c6e8a..4265894 100644 --- a/src/App.css +++ b/src/App.css @@ -26,3 +26,8 @@ from { transform: rotate(0deg); } to { transform: rotate(360deg); } } + +.Input { + width: 20em; + height: 1.3em; +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index 203067e..1a57f10 100644 --- a/src/App.js +++ b/src/App.js @@ -1,21 +1,76 @@ import React, { Component } from 'react'; import logo from './logo.svg'; import './App.css'; +import { Credentials } from './config/Config.js' +import Recipe from './components/Recipe.js' +import Error from './components/Error.js' +import SearchBar from './components/SearchBar' + + +const branch = (test, ComponentOnPass, ComponentOnFail) => props => test ? props.errorMessage.map(error => ) : props.recipeList.map(recipe => ) class App extends Component { + constructor(){ + super(); + this.state = { + search: "cakes", + lastSearch: "", + recipeList: [], + errorMessage: [], + hasError: false + } + } + + updateSearch = (event) => { + this.setState({search: event.target.value}) + } + + handleErrors = (response) => { + if (this.state.lastSearch.length === 0) { + throw {message: "Empty search"}; + }; + if (this.state.recipeList.length === 0) { + throw {message: "No recipes found"}; + }; + return response; + } + + fetchRecipes = (query) => { + const endpoint = `${Credentials.URL}?app_id=${Credentials.APP_ID}&app_key=${Credentials.APP_KEY}&q=${query}` + this.setState({ + lastSearch: this.state.search, + hasError: false, + errorMessage: [] + }) + + fetch(endpoint) + .then(response => response.json()) + .then(response => this.setState({recipeList: response.hits})) + .then(this.handleErrors) + .catch((error) => this.setState({hasError: true, errorMessage: [error.message], recipeList: []})) + } + + componentDidMount() { + this.fetchRecipes(this.state.search); + } + render() { return (
logo -

Welcome to React

+

Welcome to Edamam Recipes

-

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

+
+ +

Recipe List for "{this.state.lastSearch}":

+ {branch(this.state.hasError, Recipe, Error)(this.state)} +
); } } export default App; + + diff --git a/src/components/Error.js b/src/components/Error.js new file mode 100644 index 0000000..648593c --- /dev/null +++ b/src/components/Error.js @@ -0,0 +1,10 @@ +import React from 'react'; + +const Error = (props) => +
+

Ops...: {props.message}

+
+ + +export default Error; + diff --git a/src/components/Recipe.js b/src/components/Recipe.js new file mode 100644 index 0000000..8720048 --- /dev/null +++ b/src/components/Recipe.js @@ -0,0 +1,8 @@ +import React from 'react'; + +const Recipe = (props) => ( +

Name: {props.myRecipe.recipe.label} - Calories: {Math.floor(props.myRecipe.recipe.calories)}

+) + +export default Recipe; + diff --git a/src/components/SearchBar.js b/src/components/SearchBar.js new file mode 100644 index 0000000..ad0cb53 --- /dev/null +++ b/src/components/SearchBar.js @@ -0,0 +1,11 @@ +import React from 'react'; + +const SearchBar = ({updateSearch, search, query}) => ( +
+ updateSearch(event)} /> + +
+) + +export default SearchBar; + diff --git a/src/config/Config.js b/src/config/Config.js new file mode 100644 index 0000000..faa1f8a --- /dev/null +++ b/src/config/Config.js @@ -0,0 +1,9 @@ +export const Credentials = { + APP_ID: 'd16f0366', + APP_KEY: '67b7336730c87b15ed22e731e1723c47', + URL: 'https://api.edamam.com/search' +} + +// an example API request with a cake query would be +// `${BASE_ENDPOINT}&q=cake` +//BASE_ENDPOINT: `https://api.edamam.com/search?app_id=${APP_ID}&app_key=${APP_KEY}` \ No newline at end of file