diff --git a/.gitignore b/.gitignore index d8e35bc..0ad5f05 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ # misc .idea +.env .DS_Store .env.local .env.development.local diff --git a/src/App.js b/src/App.js index 203067e..7eddf11 100644 --- a/src/App.js +++ b/src/App.js @@ -1,18 +1,73 @@ import React, { Component } from 'react'; -import logo from './logo.svg'; -import './App.css'; +import './styles/App.css'; +import { RecipeList } from './RecipeList.js'; +import SearchBar from './SearchBar.js'; +import { Error } from './Error.js'; + +const app_id = process.env.REACT_APP_APP_ID; +const app_key = process.env.REACT_APP_APP_KEY; class App extends Component { + constructor(props) { + super(props); + this.state = { + error: null, + isLoading: false, + recipes: [] + }; + } + + fetchRecipes = (foodItem = 'cake') => { + let recipeUrl = `https://api.edamam.com/search?&app_id=${app_id}&app_key=${app_key}&q=${foodItem}`; + return new Promise((res, rej) => { + return fetch(recipeUrl) + .then(res => res.json()) + .then( + value => { + if (value.hits.length === 0) { + rej('No recipe found!'); + } else { + const recipes = value.hits.map(foodItem => ({ + name: foodItem.recipe.label, + calories: Math.round(foodItem.recipe.calories) + })); + res(recipes); + } + }, + error => { + rej('Oops, something went wrong!'); + throw error; + } + ); + }); + }; + + componentDidMount() { + this.fetchRecipes() + .then(recipes => { + this.setState({ recipes }); + }) + .catch(e => this.setState({ error: e })); + } + + updateError = error => { + this.setState({ error }); + }; + + updateRecipes = recipes => { + this.setState({ recipes }); + }; + render() { return (
- To get started, edit src/App.js and save to reload.
-
{error}
; +}; diff --git a/src/RecipeList.js b/src/RecipeList.js new file mode 100644 index 0000000..d0d6411 --- /dev/null +++ b/src/RecipeList.js @@ -0,0 +1,12 @@ +import React from 'react'; +import { EachRecipe } from './EachRecipe.js'; + +export const RecipeList = ({ recipes }) => { + return ( +