-
-
Save olajohn-ajiboye/0b0d0a913566f8f690036018534e6bba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import RecipeList from './components/RecipeList'; | |
import './App.css' | |
function App() { | |
// remeber to replace key | |
const apiKey = `36920f6651c9cd9d91a6c3205cabaa19` | |
let url = `https://www.food2fork.com/api/search?key=${apiKey}` | |
const [showHomeButton, setShowHomeButton] = useState(false) | |
const [recipes, setRecipes] = useState([]) | |
const [loading, setLoading] = useState(true) | |
const [search, setSearch] = useState('') | |
const [error, setError] = useState('') | |
const fetchRecipe = async () => { | |
try { | |
const recipeData = await fetch(url) | |
const { recipes } = await recipeData.json() | |
setRecipes(recipes) | |
setLoading(false) | |
} catch (e) { | |
if (e) { | |
setError(e.message) | |
console.log(error) | |
} | |
} | |
} | |
const handleSubmit = async (e) => { | |
e.preventDefault() | |
try { | |
setLoading(true) | |
const searchUrl = `${url}&q=${search}` | |
const searchedRecipeData = await fetch(searchUrl) | |
const { recipes } = await searchedRecipeData.json() | |
setRecipes(recipes) | |
setLoading(false) | |
setShowHomeButton(true) | |
} catch (e) { | |
console.log(e) | |
} | |
} | |
const handleSearchChange = (e) => { | |
setSearch(e.target.value) | |
} | |
const handleReturnHome = () => { | |
fetchRecipe() | |
setShowHomeButton(false) | |
} | |
useEffect(() => { | |
fetchRecipe() | |
}, []) | |
return ( | |
<div> | |
{loading ? <h1 className="text-center">…fetching {search} Recipe</h1> : | |
<RecipeList | |
search={search} | |
handleSubmit={handleSubmit} | |
handleSearchChange={handleSearchChange} | |
recipes={recipes} | |
showHomeButton={showHomeButton} | |
handleReturnHome={handleReturnHome} />} | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment