-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.js
166 lines (155 loc) · 4.09 KB
/
index.ios.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var React = require('react-native');
var {
NavigatorIOS,
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} = React;
// App Modules
var LocationCell = require('./LocationCell');
var LocationScreen = require('./LocationScreen');
// functions
var filterByDistance = require('./filterByDistance');
// common modules
// var fetch = require('fetch');
// dev mock data
var mockResponseData = require('./foursquare.response');
// Foursquare API info
var API_KEY = 'SBSSS1IL2LXNNU3WD23ICXBJLGK5EI1JJOU20L3OSTI1L0P1';
// var API_URL = 'https://api.foursquare.com/v2/venues/search?ll=37.579253,-122.357889&v=20150327&query=in-in-out';
var API_URL = 'https://api.foursquare.com/v2/venues/search?&v=20150327&query=burgers';
var PARAMS = '&oauth_token=' + API_KEY
var REQUEST_URL = API_URL + PARAMS;
function addLocationParamsToURL(latitude, longitude) {
return REQUEST_URL + '&ll=' + latitude + ',' + longitude;
}
var BurgersApp = React.createClass({
render: function() {
return (
<NavigatorIOS
style={styles.container}
initialRoute={{
title: 'Burgers Near Me',
component: LocationList,
// TODO - doesnt work
// TODO - doesnt work
}}
titleTextColor='white'
barTintColor='#e64f61'
/>
);
}
});
var LocationList = React.createClass({
watchID: (null: ?number),
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
initialPosition: 'unknown',
lastPosition: 'unknown',
};
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(initialPosition) => {
this.setState({initialPosition})
this.fetchData();
// this.loadOfflineData();
},
(error) => {
console.error(error);
this.loadOfflineData();
}
);
this.watchID = navigator.geolocation.watchPosition((lastPosition) => {
this.setState({lastPosition});
});
},
loadOfflineData: function() {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(filterByDistance(mockResponseData.response.venues)),
loaded: true,
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
fetchData: function() {
var latitude = this.state.initialPosition.coords.latitude;
var longitude = this.state.initialPosition.coords.longitude;
var url = addLocationParamsToURL(latitude, longitude);
// render dynamic data
fetch(url)
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(filterByDistance(responseData.response.venues)),
loaded: true,
});
})
.done();
},
render: function() {
if (!this.state.loaded) {
return this.renderLoadingView();
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
style={styles.listView}>
</ListView>
);
},
renderLoadingView: function() {
return (
<View style={styles.container}>
<Text>
Loading burgers..
</Text>
</View>
);
},
selectLocation: function(location: Object) {
// console.log('selected location ', location);
this.props.navigator.push({
title: '',//location.name,
component: LocationScreen,
passProps: {location},
backButtonTitle: 'Back' // this aint working
});
},
renderRow: function(location: Object) {
return (
<LocationCell
onSelect={() => this.selectLocation(location)}
location={location}/>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
wrapper: {
flex: 1,
},
listView: {
paddingTop: 3,
backgroundColor: '#E5E5E5',
},
});
AppRegistry.registerComponent('AwesomeProject', () => BurgersApp);