This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
95 lines (81 loc) · 2.84 KB
/
App.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 from 'react';
import { StyleSheet, AsyncStorage, Alert } from 'react-native';
import { createAppContainer } from 'react-navigation';
import { AppLoading } from 'expo';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import TabNavigator from './components/TabNavigator';
import rootReducer from './reducers';
import { fetchAnnouncements } from './actions/Announcements';
import { fetchConfiguration } from './actions/Configuration';
import { fetchEvents } from './actions/Events';
import { fetchLocations } from './actions/Locations';
import { fetchAuthFromToken } from './actions/Auth';
const AppContainer = createAppContainer(TabNavigator);
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);
export default class App extends React.Component {
state = {
isReady: false,
};
// Fetch some non-essential information on startup
componentDidMount() {
store.dispatch(fetchAnnouncements());
store.dispatch(fetchEvents());
store.dispatch(fetchLocations());
}
render() {
if (!this.state.isReady) {
return (
<AppLoading
startAsync={this._fetchInitialReduxState}
onFinish={() => { this.setState({ isReady: true }) }}
onError={this._appLoadingError}
/>
);
}
return (
<Provider store={store}>
<AppContainer />
</Provider>
);
}
// Load configuration data before the app fully loads in.
// This forces the app to wait until the data is received,
// which we need for configuration data. For less critical
// data like announcements and schedule, we shouldn't impact
// loading time to wait for them, so we load them in
// componentDidMount and don't wait.
//
// I'm not sure that this is the best way to pull this
// off, and we might want to figure out a more robust
// solution for the future.
async _fetchInitialReduxState() {
// If token is persisted, fill auth store with
// info about user
const token = await AsyncStorage.getItem('@auth:token');
let authPromise = null;
if (token !== null) {
authPromise = store.dispatch(fetchAuthFromToken(token));
}
return Promise.all([
store.dispatch(fetchConfiguration()),
authPromise
]);
}
_appLoadingError(error) {
Alert.alert('Failed to retrieve application information. Make sure that your internet connection is working.');
console.log(error);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});