-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
210 lines (193 loc) · 6.43 KB
/
Copy pathApp.js
File metadata and controls
210 lines (193 loc) · 6.43 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { Component } from 'react'
import {
ActivityIndicator,
Linking,
Platform,
StyleSheet,
Text,
View
} from 'react-native'
import { Actions } from 'react-native-router-flux'
import SafariView from 'react-native-safari-view'
import {
CLIENT_ID,
CLIENT_SECRET,
API_URL,
API_URL_VENUE,
API_DOWSER
} from 'react-native-dotenv'
// Imports all the views from the Router.js
import Router from './src/Router'
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
currentuser: null,
latitude: null,
longitude: null,
loading: false,
searchResults: null
}
}
async componentDidMount() {
// OAuth
Linking.addEventListener('url', this.handleOpenURL)
Linking.getInitialURL().then((url) => {
if (url) {
this.handleOpenURL({ url })
}
})
// Get the current location of the user's device
await navigator.geolocation.getCurrentPosition( position => {
const latitude = position.coords.latitude
const longitude = position.coords.longitude
this.setState({
latitude,
longitude,
})
Actions.LoginView({ loginWithGoogle: this.loginWithGoogle.bind(this) })
})
}
loginWithGoogle = () => {
this.setState({
loading: true
})
this.openURL(`${API_DOWSER}/auth/google`)
}
// This is where APP STATE is sent to UserView
handleOpenURL = async ({ url }) => {
// Extract stringified user string out of the URL
const [, user_string] = url.match(/user=([^#]+)/)
// SET STATE WITH CURRENT USER INFO
this.setState({
// Decode the user string and parse it into JSON
currentuser: JSON.parse(decodeURI(user_string)),
loading: false
}, () => {
// Send oAuth response to UserView as props
Actions.UserView({
currentuser: this.state.currentuser,
userFavorites: this.state.currentuser.favorites,
callFourSquareAPI: this.callFourSquareAPI.bind(this),
getVenueDetails: this.getVenueDetails.bind(this),
addToFavorites: this.addToFavorites.bind(this),
appState: this.state
})
})
if (Platform.OS === 'ios') {
SafariView.dismiss()
}
}
openURL = (url) => {
// Use SafariView on iOS
if (Platform.OS === 'ios') {
SafariView.show({
url: url,
fromBottom: true,
})
}
// Or Linking.openURL on Android
// else {
// Linking.openURL(url)
// }
}
// Makes a fetch call to the Foursquare search/recommendations endpoint
async callFourSquareAPI (searchTerm = `coffee`) {
const lat = this.state.latitude
const lng = this.state.longitude
const response = await fetch(`${API_URL}?v=20171114&query=${searchTerm}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&ll=${lat},${lng}&limit=50`)
const json = await response.json()
const jsonArr = json.response.group.results
// send the response to:
this.extractInfoFromFoursquareApi(jsonArr, searchTerm)
}
// Parse out and format the information we want from the API response, pass the searchTearm as a prop for the navBar
extractInfoFromFoursquareApi (jsonArr, searchTerm) {
const searchResults = jsonArr.map( item => {
const longitude = Number(item.venue.location.lng)
const latitude = Number(item.venue.location.lat)
const checkinCount = Number(item.venue.stats.checkinsCount)
const ratingColor = item.venue.ratingColor
const venueId = item.venue.id
return {
name: item.venue.name,
location:[latitude, longitude],
checkinCount,
ratingColor,
venueId
}
})
// Determine the appropiate quotient used to normalize the the radius of the map markers based on check-ins
const checkinsArr = jsonArr.map( item => Number(item.venue.stats.checkinsCount) )
const toScale = (Math.max(...checkinsArr)*0.014)
this.setState({
searchResults,
searchTerm
},() => Actions.NativeMapView({
searchResults: this.state.searchResults,
latitude: this.state.latitude,
longitude: this.state.longitude,
toScale,
getVenueDetails: this.getVenueDetails.bind(this),
})
)
}
// Make a fetch call to the Foursquare /venues endpoint to get details of a specific venue, parse out the information.
// Venue details are only available if they are provided by the venue to Foursquare.
async getVenueDetails(venueId) {
const response = await fetch(`${API_URL_VENUE}/${venueId}?v=20171114&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}`)
const json = await response.json()
const venueDetails = {
name: json.response.venue.name,
id: json.response.venue.id,
photo: json.response.venue.bestPhoto ? `${json.response.venue.bestPhoto.prefix}original${json.response.venue.bestPhoto.suffix}` : null,
description: json.response.venue.description ? json.response.venue.description : null,
status: json.response.venue.hours ? json.response.venue.hours.status : null,
address: json.response.venue.location.formattedAddress,
phone: json.response.venue.contact.formattedPhone,
website: json.response.venue ? json.response.venue.url : null,
delivery: json.response.venue.delivery ? json.response.venue.delivery.url : null,
checkIns: json.response.venue.stats.checkinsCount
}
Actions.VenueDetailView({
venueDetails,
addToFavorites: this.addToFavorites.bind(this)
})
}
async addToFavorites(details) {
// Add new favorite to our Mongo DB hosted on Heroku and Mlab.
const favPost = await fetch(`${API_DOWSER}/favorites/add/${this.state.currentuser.googleID}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: details.name,
venueId: details.id
})
})
// Get favorites array back from db.
const updatedFavorites = await favPost.json()
// Update favorites in state and redirect to user view.
this.setState({
userFavorites: updatedFavorites
}, () => {
Actions.UserView({
userFavorites: this.state.userFavorites,
currentuser: this.state.currentuser,
getVenueDetails: this.getVenueDetails.bind(this)
})
})
}
render() {
console.disableYellowBox = true
if (this.state.loading) {
return (
<ActivityIndicator />
)
}
return (
<Router searchTerm={this.state.searchTerm} />
)
}
}