-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
95 lines (85 loc) · 2.37 KB
/
Home.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
import React, { Component, Fragment } from 'react';
import { SafeAreaView, ScrollView, RefreshControl, Text, View, StyleSheet, Image } from 'react-native';
import PushController from './PushController';
import History from './History';
import Today from './Today';
import Loading from './Loading';
class Home extends Component {
static navigationOptions = {
title: 'Honor The Flag',
headerStyle: {
backgroundColor: '#4169e1',
height: 40,
},
headerForceInset: { top: 'never', bottom: 'never' },
headerTintColor: '#FFFFFF',
};
constructor(props) {
super(props);
this.state = { loading:true, data: null, error: false };
}
_refresh = () => {
this.setState({ loading: true, data: null, error: false });
const url = 'https://api.fatherstorm.com/flag.php?long=true';
setTimeout(() => {
fetch(url)
.then(data => {return data.json();})
.then(json => {
this.setState({ loading: false, data: json });
})
.catch(error => {
this.setState({ loading: false, error: true });
});
}, 2000);
}
async componentDidMount() {
this._refresh();
}
_getTodayStatus = mostRecentStatus => {
return (
<>
<Today item={mostRecentStatus} />
</>
);
}
_getHistoryItems = data => {
return (
<History items={data.slice(0,20)} navigation={this.props.navigation} />
);
}
render() {
const { loading, data, error } = this.state;
return (
<SafeAreaView>
<ScrollView
refreshControl={
<RefreshControl refreshing={false} onRefresh={this._refresh} />
}
contentInsetAdjustmentBehavior="automatic">
<View style={styles.page}>
<Fragment>
{error ? <Text>An unexpected error has occurred</Text> : null}
</Fragment>
<Fragment>
{loading ? <Loading /> : null}
</Fragment>
<Fragment>
{data != null ? this._getTodayStatus(data[0]) : null}
{data != null ? this._getHistoryItems(data) : null}
</Fragment>
<PushController/>
</View>
</ScrollView>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
page: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: '#d3d3d3',
},
});
export default Home;