-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
211 lines (188 loc) · 5.65 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { useCallback, useEffect, useState, useMemo } from "react";
import { View, StatusBar } from "react-native";
import * as Font from "expo-font";
import * as SplashScreen from "expo-splash-screen";
import { NavigationContainer } from "@react-navigation/native";
import { UnauthenticatedStack, UnverifiedStack } from "./navigators/Stacks";
import { BottomTabs } from "./navigators/BottomTabs";
import { save, getValueFor } from "./hooks/SecureStore";
import { saveLocally, getLocalValueFor } from "./hooks/LocalStorage";
import { AppContext } from "./context/AppContext";
import Loading from "./screens/Loading";
import * as Location from "expo-location";
import Constants from "expo-constants";
export default function App() {
const [attendance, setAttendance] = useState([]);
const [appIsReady, setAppIsReady] = useState(false);
const [location, setLocation] = useState(null);
const [email, setEmail] = useState(null);
const [password, setPassword] = useState(null);
const [token, setToken] = useState(null);
const [userInfo, setUserInfo] = useState(null);
const [lecturesData, setLecturesData] = useState({});
const [studentInfo, setStudentInfo] = useState({
student_id: "Loading",
programme: "Loading",
});
const [staffInfo, setStaffInfo] = useState({
staff_id: "Loading",
department: "Loading",
});
const contextValue = useMemo(
() => ({
token,
setToken,
updateAccessToken,
userInfo,
setUserInfo,
lecturesData,
setLecturesData,
studentInfo,
setStudentInfo,
staffInfo,
setStaffInfo,
location,
setLocation,
attendance,
setAttendance,
}),
[
token,
setToken,
updateAccessToken,
userInfo,
setUserInfo,
lecturesData,
setLecturesData,
studentInfo,
setStudentInfo,
staffInfo,
setStaffInfo,
location,
setLocation,
attendance,
setAttendance,
]
);
const options = {
method: "POST",
url: "http://qrollease-api-112d897b35ef.herokuapp.com/api/login/access-token",
headers: {
accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
},
data: {
grant_type: "",
username: email,
password: password,
scope: "",
client_id: "",
client_secret: "",
},
};
const options2 = {
method: "GET",
url: "https://qrollease-api-112d897b35ef.herokuapp.com/api/users/me",
headers: {
accept: "application/json",
Authorization: `Bearer ${JSON.parse(token)} `,
},
};
useEffect(() => {
async function getAppReady() {
try {
await SplashScreen.preventAutoHideAsync();
await Font.loadAsync({
bold: require("./assets/fonts/Montserrat-Bold.ttf"),
light: require("./assets/fonts/Montserrat-Light.ttf"),
medium: require("./assets/fonts/Montserrat-Medium.ttf"),
regular: require("./assets/fonts/Montserrat-Regular.ttf"),
semibold: require("./assets/fonts/Montserrat-SemiBold.ttf"),
thin: require("./assets/fonts/Montserrat-Thin.ttf"),
});
await new Promise((resolve) => setTimeout(resolve, 2000));
} catch (e) {
console.warn(e);
} finally {
setAppIsReady(true);
}
}
getAppReady();
}, []);
useEffect(() => {
StatusBar.setBarStyle("dark-content");
}, []);
useEffect(() => {
const fetchData = async () => {
const access_token = await getValueFor("access_token");
const email = await getValueFor("email");
const password = await getValueFor("password");
const user_info = await getLocalValueFor("user_info");
setToken(access_token);
setEmail(email);
setPassword(password);
setUserInfo(JSON.parse(user_info));
await updateAccessToken();
await updateUserInfo();
};
fetchData();
}, []);
useEffect(() => {
const getLocation = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
return;
}
const lastKnownLocation = await Location.getLastKnownPositionAsync();
setLocation(lastKnownLocation);
const currentLocation = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
distanceInterval: 100,
});
setLocation(currentLocation);
};
getLocation();
}, []);
const updateAccessToken = useCallback(async () => {
if (email && password) {
try {
const response = await axios(options);
save("access_token", JSON.stringify(response.data.access_token));
setToken(JSON.stringify(response.data.access_token));
} catch (error) {
console.log(error);
}
}
}, []);
const updateUserInfo = useCallback(async () => {
if (token) {
{
try {
const response = await axios(options2);
saveLocally("user_info", JSON.stringify(response.data));
setUserInfo(response.data);
} catch (error) {
console.log(error);
}
}
}
}, []);
const onLayoutRootView = useCallback(async () => {
if (appIsReady) {
await SplashScreen.hideAsync();
}
}, [appIsReady]);
if (!appIsReady) {
return null;
}
return (
<NavigationContainer onReady={onLayoutRootView}>
<AppContext.Provider value={contextValue}>
{token && !userInfo && <Loading />}
{token && userInfo && userInfo.is_verified && <BottomTabs />}
{token && userInfo && !userInfo.is_verified && <UnverifiedStack />}
{!token && <UnauthenticatedStack />}
</AppContext.Provider>
</NavigationContainer>
);
}