This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
Sphere/app/index.js

115 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-02-01 20:58:00 +00:00
import React from 'react-native';
2016-05-05 21:04:50 +01:00
import RouteMaster from './components/routes/RouteMaster';
import RouteMapper from './components/navigation/RouteMapper';
import GlobalStyles from './settings/styles';
2016-02-21 12:05:14 +00:00
import BusyIndicator from 'react-native-busy-indicator';
2016-05-05 21:04:50 +01:00
import token from './api/token';
import { checkToken } from './api/CircleCI';
2016-05-07 20:32:37 +01:00
import NavigationBar from './components/navigation/NavigationBar';
2016-02-01 20:58:00 +00:00
2016-05-05 21:51:49 +01:00
import 'moment-duration-format';
2016-02-01 20:58:00 +00:00
const {
Navigator,
2016-02-06 15:07:29 +00:00
StyleSheet,
2016-05-05 21:51:49 +01:00
View,
Alert
2016-02-01 20:58:00 +00:00
} = React;
2016-02-06 15:07:29 +00:00
const styles = StyleSheet.create({
navbar: {
2016-02-06 15:49:57 +00:00
backgroundColor: GlobalStyles.get('CIRCLE_NAVBAR_BG'),
2016-02-06 15:07:29 +00:00
flexDirection:'row',
justifyContent: 'center',
},
container: {
flex: 1,
2016-05-07 20:32:37 +01:00
marginTop: GlobalStyles.get('NAVBAR_HEIGHT')
2016-02-06 15:07:29 +00:00
}
});
2016-05-05 21:51:49 +01:00
const LOGGED_IN_ROUTE = RouteMaster.get('HOME');
const LOGIN_ROUTE = RouteMaster.get('LOGIN');
2016-05-05 21:04:50 +01:00
export default class App extends React.Component {
2016-05-05 18:25:00 +01:00
constructor() {
super();
this.state = {
initialRoute: false
};
}
2016-02-01 20:58:00 +00:00
renderScene(route, nav) {
2016-02-06 15:07:29 +00:00
const Component = route.component;
2016-02-01 20:58:00 +00:00
const props = route.props || {};
2016-05-07 20:32:37 +01:00
const navbarStyles = {
marginTop: (route.hideNavbar ? GlobalStyles.get('BANNER_HEIGHT') : GlobalStyles.get('NAVBAR_HEIGHT'))
};
2016-02-06 15:07:29 +00:00
return (
2016-05-07 20:32:37 +01:00
<View style={[styles.container, navbarStyles]}>
2016-02-06 15:07:29 +00:00
<Component
nav={nav}
2016-02-23 23:12:32 +00:00
currentRoute={route}
2016-02-06 15:07:29 +00:00
{...props} />
</View>
);
2016-02-01 20:58:00 +00:00
}
2016-05-05 21:51:49 +01:00
warnInvalidToken() {
Alert.alert(
'Token Invalidated',
'The token you used previously is now invalid. Please generate a new one in your account settings.',
[
{ text: 'ok' }
]
);
}
2016-02-01 21:50:50 +00:00
componentWillMount() {
2016-05-05 18:25:00 +01:00
token.get().then(function (CIToken) {
if (!CIToken) {
this.setState({
2016-05-05 21:51:49 +01:00
initialRoute: LOGIN_ROUTE
2016-05-05 18:25:00 +01:00
});
return;
}
checkToken(CIToken).then(function (isValid) {
this.setState({
2016-05-05 21:51:49 +01:00
initialRoute: isValid ? LOGGED_IN_ROUTE : LOGIN_ROUTE
2016-05-05 18:25:00 +01:00
});
2016-05-05 21:51:49 +01:00
if (!isValid) {
this.warnInvalidToken();
}
2016-05-05 18:25:00 +01:00
}.bind(this));
}.bind(this));
2016-02-01 20:58:00 +00:00
}
render() {
2016-05-05 18:25:00 +01:00
if (!this.state.initialRoute) {
return null; // Replace with a splash screen?
}
2016-02-01 20:58:00 +00:00
return (
2016-03-11 20:53:33 +00:00
<View style={{ flex: 1 }}>
<Navigator
renderScene={this.renderScene}
2016-05-05 18:25:00 +01:00
initialRoute={this.state.initialRoute}
2016-03-11 20:53:33 +00:00
navigationBar={
2016-05-07 20:32:37 +01:00
<NavigationBar
2016-03-11 20:53:33 +00:00
style={styles.navbar}
routeMapper={RouteMapper} />
2016-02-01 20:58:00 +00:00
}
2016-03-11 20:53:33 +00:00
configureScene={(route) => {
if (route.sceneConfig) {
return route.sceneConfig;
}
return Navigator.SceneConfigs.PushFromRight;
}}
/>
<BusyIndicator />
</View>
2016-02-01 20:58:00 +00:00
);
}
};