-
Notifications
You must be signed in to change notification settings - Fork 7
/
App.js
175 lines (160 loc) · 4.63 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import React, { Component } from 'react';
import { Container, Root, Toast } from 'native-base';
import * as Font from 'expo-font';
import { Asset } from 'expo-asset';
import { Notifications } from 'expo';
import * as Permissions from 'expo-permissions';
import Roboto from 'native-base/Fonts/Roboto.ttf';
import RobotoMedium from 'native-base/Fonts/Roboto_medium.ttf';
import {
createDrawerNavigator,
createStackNavigator,
createAppContainer
} from 'react-navigation';
import AppContext from './components/utils/appContext';
import Sidebar from './components/shell/sidebar';
import Feed from './views/feed';
import Calendar from './views/calendar';
import Mess from './views/mess';
import Map from './views/map';
import Admin from './views/admin';
import Profile from './views/profile';
import { AppLoading } from 'expo';
import axios from 'axios';
const registerForNotifs = async () => {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
};
const _loadFonts = () => {
return Font.loadAsync({
Roboto: Roboto,
Roboto_medium: RobotoMedium
});
};
const _loadAssets = () => {
return Asset.loadAsync([
require('./assets/cover.jpg'),
require('./assets/profile-pic.png')
]);
};
const pages = {
Feed: { screen: Feed },
Calendar: { screen: Calendar },
Admin: { screen: Admin },
Mess: { screen: Mess },
Profile: { screen: Profile },
Map: { screen: Map }
};
const Drawer = createDrawerNavigator(pages, {
initialRouteName: 'Calendar',
contentOptions: {
activeTintColor: '#e91e63'
},
contentComponent: props => <Sidebar {...props} />
});
const AppNavigator = createStackNavigator(
{ ...pages, Drawer: { screen: Drawer } },
{
initialRouteName: 'Drawer',
headerMode: 'none'
}
);
const AppContainer = createAppContainer(AppNavigator);
class App extends Component {
state = {
loggedIn: false,
details: {},
appIsReady: false
};
login = async (username, password) => {
const token = await Notifications.getExpoPushTokenAsync();
return new Promise((resolve, reject) => {
axios({
method: 'post',
url: 'https://lifeiitk.tk/api/users/auth/login/',
data: `username=${username}&password=${password}&token=${token}`,
withCredentials: true
})
.then(res => {
// GET PROFILE DETAILS
axios({
method: 'get',
url: 'https://lifeiitk.tk/api/users/profile/',
withCredentials: true
})
.then(res => this.setState({ details: res.data }))
.catch(err => {
Toast.show({
text: 'An unexpected error occured.',
duration: 3000
});
console.log(err);
});
// SET STATE TO LOGGED IN
this.setState({ loggedIn: true });
resolve(res);
})
.catch(err => reject(err));
});
};
logout = async () => {
const token = await Notifications.getExpoPushTokenAsync();
return new Promise(resolve => {
axios({
method: 'post',
url: 'https://lifeiitk.tk/api/users/auth/logout/',
data: `token=${token}`,
withCredentials: true
})
.then(() => {
this.setState({ loggedIn: false, details: {} });
resolve();
})
.catch(() => reject());
});
};
handleLog = async (username, password, mode) => {
if (mode === 'logout') return this.logout();
else return this.login(username, password);
};
checkIfLoggedIn = () => {
return new Promise(resolve => {
axios
.get('https://lifeiitk.tk/api/users/profile/', {
withCredentials: true
})
.then(res => this.setState({ loggedIn: true, details: res.data }))
.catch(() => this.setState({ loggedIn: false, details: {} }))
.finally(() => resolve());
});
};
componentWillMount = () => {
registerForNotifs();
Promise.all([this.checkIfLoggedIn(), _loadAssets(), _loadFonts()])
.then(() => this.setState({ appIsReady: true }))
.catch(err => console.log(err));
};
render() {
if (!this.state.appIsReady) return <AppLoading />;
else {
return (
<Root>
<Container>
<AppContext.Provider
value={{ state: { ...this.state }, log: this.handleLog }}
>
<AppContainer />
</AppContext.Provider>
</Container>
</Root>
);
}
}
}
export default App;