-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#DSDGROC-43 Recipe List Component - Home View
- Loading branch information
Showing
4 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Alert, Pressable, StyleSheet, Text, View } from 'react-native'; | ||
import RecipeCard from './RecipeCard'; | ||
import { FlatList } from 'react-native'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const RecipeList = ({ title, scrollEnabled }) => { | ||
const [recipes, setRecipes] = useState([]); | ||
const apiKey = '90ce20e792384e9a99fa0bd3d950c094'; | ||
|
||
const fetchRecipes = async () => { | ||
try { | ||
const response = await fetch( | ||
`https://api.spoonacular.com/recipes/random?apiKey=${apiKey}&number=10` | ||
); | ||
const data = await response.json(); | ||
setRecipes(data.recipes); | ||
} catch (error) { | ||
console.log('Error fetching data: ', error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchRecipes(); | ||
}, []); | ||
|
||
const renderRecipe = ({ item }) => { | ||
return ( | ||
<Pressable onPress={() => console.log('Recipe Detail Page')}> | ||
<RecipeCard | ||
title={item.title} | ||
image={item.image} | ||
minutes={item.readyInMinutes || 0} | ||
rating={item.aggregateLikes} | ||
/> | ||
</Pressable> | ||
); | ||
}; | ||
return ( | ||
<> | ||
<Text style={style.titleText}>{title}</Text> | ||
<FlatList | ||
numColumns={2} | ||
scrollEnabled={scrollEnabled || false} | ||
columnWrapperStyle={style.columnWrapper} | ||
data={recipes} | ||
renderItem={renderRecipe} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default RecipeList; | ||
|
||
const style = StyleSheet.create({ | ||
titleText: { | ||
fontSize: 25, | ||
fontWeight: 'bold', | ||
}, | ||
columnWrapper: { | ||
justifyContent: 'space-between', | ||
marginVertical: 5, | ||
marginHorizontal: 5, | ||
}, | ||
}); |