-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHomeScreen.js
More file actions
62 lines (54 loc) · 2.31 KB
/
HomeScreen.js
File metadata and controls
62 lines (54 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useEffect, useState } from 'react';
import { ScrollView, Alert, ActivityIndicator } from 'react-native';
import HeaderTabs from '../components/HeaderTabs';
import Screen from '../components/Screen'
import Categories from '../components/Categories'
import SearchBar from '../components/SearchBar'
import RestaurantItem from '../components/RestaurantItem'
import tailwind from 'tailwind-react-native-classnames';
import { localRestaurants } from '../data/localRestaurants';
import colors from '../configs/colors'
const YELP_API_KEY = "";
const HomeScreen = () => {
const [restaurantData, setRestaurantData] = useState(localRestaurants)
const [city, setCity] = useState("San Francisco")
const [activeTab, setActiveTab] = useState("Delivery");
const [loading, setLoading] = useState(false)
const getRestaurantsFromYelp = () => {
const yelpUrl = `https://api.yelp.com/v3/businesses/search?term=restaurants&location=${city}`;
const apiOptions = {
headers: {
Authorization: `Bearer ${YELP_API_KEY}`,
},
};
setLoading(true)
return fetch(yelpUrl, apiOptions)
.then((res) => res.json())
.then((json) => {
setLoading(false)
if (json.error) return Alert.alert('Sorry', json.error.description);
setRestaurantData(
json?.businesses?.filter((business) =>
business.transactions.includes(activeTab.toLowerCase())
)
)
})
.catch((err) => console.log(err));
};
useEffect(() => {
return // Remove return after adding Yelp API key
getRestaurantsFromYelp();
}, [city, activeTab]);
return (
<Screen style={tailwind`bg-white flex-1`}>
<HeaderTabs activeTab={activeTab} setActiveTab={setActiveTab} />
<SearchBar setCity={setCity} city={city} />
<ScrollView style={tailwind`flex-1`} showsVerticalScrollIndicator={false}>
<Categories />
{loading && <ActivityIndicator size="large" color={colors.primary} style={tailwind`mt-2 mb-6`} />}
<RestaurantItem restaurantData={restaurantData} />
</ScrollView>
</Screen>
);
}
export default HomeScreen;