-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
135 lines (123 loc) · 2.8 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';
var moment = require('moment');
var NASA_APOD_URL = 'https://api.nasa.gov/planetary/apod?concept_tags=True&api_key='+NASA_API_KEY;
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Image,
ListView,
} = React;
var NASA = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
};
},
componentDidMount: function() {
this.fetchData();
},
render: function() {
if (!this.state.loaded) {
return this.renderLoadingView();
} else {
//console.log(this.state.dataSource);
}
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderPic}
style={styles.listView}
/>
);
},
renderLoadingView: function() {
return (
<View style= {styles.container}>
<Text>
Loading Image...
</Text>
</View>
);
},
fetchData: function() {
var results = [];
for(var i=0; i<11; i++) {
var date = moment().subtract(i, 'days').format('YYYY-MM-DD');
this.queryAPOD(results, date);
};
},
queryAPOD: function(results, date) {
fetch(NASA_APOD_URL+'&date='+date)
.then((response) => response.json())
.then((responseData) => {
results.push({ date: date, responseData: responseData });
if(results.length === 11) {
results.sort(this.order);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(results),
loaded: true,
});
}
})
.done();
},
order: function(a, b) {
return (a.date < b.date) ? 1 : ((b.date < a.date) ? -1 : 0);
},
renderPic: function(result) {
var data = result.responseData;
return (
<View style={styles.container}>
<Image
source={{uri: data.url}}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{result.date}: {data.title}</Text>
</View>
</View>
);
},
});
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
borderBottomWidth: 0.3,
borderColor: '#ABCABC',
},
rightContainer: {
flex: 1,
},
title: {
fontSize: 14,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
thumbnail: {
width: 91,
height: 63,
marginTop:10,
marginBottom:10,
},
listView: {
paddingTop: 20,
backgroundColor: '#F5FCFF',
},
});
AppRegistry.registerComponent('NASA', () => NASA);