Check token earlier, load app faster

This commit is contained in:
Jake Howard 2016-05-05 18:25:00 +01:00
parent bad4e649e1
commit 87c6748037
2 changed files with 26 additions and 10 deletions

View file

@ -4,6 +4,8 @@ import RouteMaster from './routes/RouteMaster';
import RouteMapper from './navigation/RouteMapper';
import GlobalStyles from '../settings/styles';
import BusyIndicator from 'react-native-busy-indicator';
import token from '../api/token';
import { checkToken } from '../api/CircleCI';
require('moment-duration-format');
@ -27,6 +29,13 @@ const styles = StyleSheet.create({
});
export default class extends React.Component {
constructor() {
super();
this.state = {
initialRoute: false
};
}
renderScene(route, nav) {
const Component = route.component;
const props = route.props || {};
@ -41,15 +50,30 @@ export default class extends React.Component {
}
componentWillMount() {
this.initialRoute = RouteMaster.get('LOGIN');
token.get().then(function (CIToken) {
if (!CIToken) {
this.setState({
initialRoute: RouteMaster.get('HOME')
});
return;
}
checkToken(CIToken).then(function (isValid) {
this.setState({
initialRoute: isValid ? RouteMaster.get('HOME') : RouteMaster.get('LOGIN')
});
}.bind(this));
}.bind(this));
}
render() {
if (!this.state.initialRoute) {
return null; // Replace with a splash screen?
}
return (
<View style={{ flex: 1 }}>
<Navigator
renderScene={this.renderScene}
initialRoute={this.initialRoute}
initialRoute={this.state.initialRoute}
navigationBar={
<Navigator.NavigationBar
style={styles.navbar}

View file

@ -163,14 +163,6 @@ export default class Login extends React.Component {
componentWillMount() {
BackAndroid.addEventListener('hardwareBackPress', () => { return true; }); // Disable back button
token.get().then(function (CIToken) {
if (!CIToken) {
return;
}
checkToken(CIToken).then(function (isValid) {
this.proceed();
}.bind(this));
}.bind(this));
}
componentDidMount() {