forked from rafalbromirski/IcelandEarthquakes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarthquakesScreen.js
83 lines (69 loc) · 1.67 KB
/
EarthquakesScreen.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
'use strict';
var React = require('react-native');
var {
ListView,
} = React;
var LoadingScreen = require('./LoadingScreen');
var EarthquakeScreen = require('./EarthquakeScreen');
var EarthquakeRow = require('./EarthquakeRow');
var REQUEST_URL = "http://apis.is/earthquake/is";
var EarthquakesScreen = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
};
},
componentDidMount: function() {
this.fetchData();
},
fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData.results),
loaded: true,
});
})
.done();
},
render: function() {
if(!this.state.loaded) {
return this.renderLoadingView();
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
/>
);
},
renderLoadingView: function() {
return (
<LoadingScreen/>
);
},
renderRow: function(earthquake) {
return (
<EarthquakeRow
onSelect={() => this.selectRow(earthquake)}
earthquake={earthquake}
/>
);
},
selectRow: function(earthquake) {
this.props.navigator.push({
title: this.getTitle(earthquake.humanReadableLocation),
component: EarthquakeScreen,
passProps: {earthquake},
});
},
getTitle: function(title) {
var arr = title.split(/\s+/);
return arr[arr.length - 1];
}
});
module.exports = EarthquakesScreen;