-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
134 lines (113 loc) · 4.31 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
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
import React from 'react';
import LoginScreen from './screens/loginScreen';
import HomeScreen from './screens/homeScreen';
import GlobalScreen from './screens/globalScreen';
import ChooseChangeScreen from './screens/chooseChangeScreen';
import MyChangesScreen from './screens/myChangesScreen';
import ContactsScreen from './screens/contactsScreen';
import deviceStorage from './components/deviceStorage';
import { Font } from 'expo';
import fetchToAPI from './components/fetchToAPI'
import { createStackNavigator, createAppContainer } from 'react-navigation';
const FONT_PATH_MAINTYPO = "./assets/fonts/Montserrat-ExtraBold.otf";
const FONT_PATH_NAMETYPO = "./assets/fonts/big_noodle_titling.ttf";
console.ignoredYellowBox = ['Remote debugger'];
import { YellowBox, View } from 'react-native';
import { PulseIndicator } from 'react-native-indicators';
import layoutStyle from './styles/layoutStyle';
YellowBox.ignoreWarnings([
'Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?'
]);
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { fontLoaded: false, tokenLoaded: false }
}
async componentDidMount() {
await Font.loadAsync({
'montserrat-extra-bold': require(FONT_PATH_MAINTYPO),
'big-noodle-titling': require(FONT_PATH_NAMETYPO),
});
await this.checkToken();
this.setState({ fontLoaded: true });
}
async checkToken() {
var savedToken;
return deviceStorage.loadJWT()
.then( token => {
if ( token ) {
savedToken = token;
return fetchToAPI('/auth/me', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-access-token': token
}
});
} else {
throw "Could not find the token";
}
})
.then(response => response.json())
.then(responseJSON => {
if ( responseJSON.auth === false )
throw responseJSON.message;
if ( responseJSON.name != undefined ) {
this.initialRouteName = "HomeScreen";
this.savedToken = savedToken;
this.setState({
tokenLoaded: true
});
} else {
throw "Error inesperado";
}
})
.catch((err) => {
this.initialRouteName = "LoginScreen";
this.setState({
tokenLoaded: true
});
// Nothing should happen if this method fails,
// simply continue the execution as if the token
// was not found
});
}
render() {
if ( this.state.fontLoaded && this.state.tokenLoaded ) {
var AppNavigator = createStackNavigator({
LoginScreen: { screen: LoginScreen },
HomeScreen: { screen: HomeScreen },
GlobalScreen: { screen: GlobalScreen },
ChooseChangeScreen: { screen: ChooseChangeScreen },
MyChangesScreen: { screen: MyChangesScreen },
ContactsScreen: { screen: ContactsScreen }
}, {
initialRouteName: this.initialRouteName,
headerMode: 'none',
navigationOptions: {
headerVisible: false,
},
initialRouteParams: {
token: this.savedToken
}
});
const AppContainer = createAppContainer(AppNavigator);
return (
<AppContainer></AppContainer>
)
} else {
return (
<View style={containerStyle}>
<PulseIndicator size={layoutStyle.horizontalUnits10*15} color="#067bdb" />
</View>
)
}
}
}
const containerStyle = {
flex: 1,
flexDirection: "column",
justifyContent: "center",
backgroundColor: "#bee6ef"
}