-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket-updater.js
67 lines (63 loc) · 1.67 KB
/
market-updater.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
'use strict';
var React = require('react-native');
var MarketData = require('./market-data');
var {
AlertIOS
} = React;
class MarketUpdater {
constructor(component) {
this.component = component;
this.marketData = new MarketData();
}
update() {
this.marketData.sortBy(this.component.state.sortBy)
.then(markets => this.setMarketData(markets))
.catch(error => AlertIOS.alert(error.message))
.done();
}
setMarketData(markets) {
var dataSource = this.component.state.dataSource;
this.component.setState({
markets: markets,
dataSource: dataSource.cloneWithRows(markets),
loading: false
});
}
sortMarketsBy(name) {
if (this.component.state.sortBy == name) { return; }
this.component.setState({
loading: true,
sortBy: name
});
this.marketData.sortBy(name)
.then(markets => this.setMarketData(markets))
.catch(error => AlertIOS.alert(error.message))
.done();
}
sortByLocation(name){
this.component.setState({
loading: true,
sortBy: name
});
this.getLocation()
.then(location => this.marketData.sortByLocation(location))
.catch(error => AlertIOS.alert(`Unable to get location. ${error.message}`))
.then(markets => this.setMarketData(markets))
.done();
}
getLocation() {
var executor = function(resolve, reject) {
navigator.geolocation.getCurrentPosition(
(geoLocation) => resolve(geoLocation.coords),
reject,
{
enableHighAccuracy: false,
timeout: 10000,
maximumAge: 60000
}
);
}
return new Promise(executor);
}
}
module.exports = MarketUpdater;