This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.js
106 lines (100 loc) · 3.5 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
import React from 'react';
import getTheme from './native-base-theme/components';
import { StyleProvider } from 'native-base';
import Setup from './views/setup';
import Home from './views/home';
import LoginComponent from './views/setup/login';
import commonColor from './native-base-theme/variables/commonColor';
import UserInfoStorage from './storage/userInfo';
import SetupAPI from './api/setup';
import StorageUtils from './storage/utils';
// import io from 'socket.io-client';
import config from './api/config';
console.ignoredYellowBox = ['Warning:'];
export default class App extends React.Component {
state = {
loading: true,
// loggedIn: true,
token: null
}
componentWillMount() {
// const SocketEndpoint = 'https://api.frida.cf/v1/socket/alerts';
// const socket = io(SocketEndpoint, {
// transportOptions: {
// polling: {
// extraHeaders: {
// 'Authorization': 'Bearer test'
// }
// }
// },
// });
// socket.on('connect', () => {
// alert('connected');
// });
// socket.on('ping', data => {
// alert('ping');
// });
// this.setState({ loading: false, loggedIn: true }); return;
// StorageUtils.dropUserInfoTable(); return;
UserInfoStorage.getAll()
.then(userData => {
console.log(userData);
if(userData && userData.role) { // todo: check all other fields
this.setState({ loading: false, loggedIn: true });
return;
}
this.setState({
loading: false,
// token: userData ? userData.token : null // this is to always make the user login
});
});
}
onLogout() {
StorageUtils.dropUserInfoTable();
this.setState({ loggedIn: false });
}
render() {
if (this.state.loading) {
return null;
}
return (
<StyleProvider style={getTheme(commonColor)}>
{
this.state.loggedIn ? <Home screenProps={{ onLogout: this.onLogout.bind(this) }}/> :
<Setup
screenProps={{
token: this.state.token,
onFinishSetup: (setupData) => {
if (config.FAKE_MODE) {
UserInfoStorage.set({
role: setupData.role,
institution: setupData.institution,
institutionId: setupData.institutionId,
locationId: setupData.locationId,
notificationToken: setupData.notificationToken
});
this.setState({ loggedIn: true });
} else {
SetupAPI.setupUser(setupData.role, setupData.institutionId, setupData.locationId, setupData.notificationToken)
.then(response => {
console.log(response);
if (response.message === 'User Created') {
UserInfoStorage.set({
role: setupData.role,
institution: setupData.institution,
institutionId: setupData.institutionId,
locationId: setupData.locationId,
notificationToken: setupData.notificationToken
});
this.setState({ loggedIn: true });
} else {
alert('Internal Error, something went wrong');
}
});
}
}}} />
}
</StyleProvider>
);
}
}