-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
165 lines (158 loc) · 4.96 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
import React, { useState, useEffect } from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import {
Platform,
StatusBar,
StyleSheet,
View,
TouchableOpacity,
AsyncStorage,
} from "react-native";
import TabBarIcon from "./components/TabBarIcon.js";
import useCachedResources from "./hooks/useCachedResources";
import BottomTabNavigator from "./navigation/BottomTabNavigator";
import LinkingConfiguration from "./navigation/LinkingConfiguration";
import { homeURL, storage_keys } from "./constants";
import fetch_a from "./util/fetch_auth";
import Colors from "./public/Colors";
import LoginScreen from "./screens/LoginScreen/LoginScreen.js";
import EditProfileScreen from "./screens/EditProfileScreen/EditProfileScreen.js";
import EditOfferScreen from "./screens/EditOfferScreen/EditOfferScreen.js";
import EditDetailsScreen from "./screens/EditDetailsScreen/EditDetailsScreen.js";
import RequestsScreen from "./screens/RequestsScreen/RequestsScreen.js";
import SettingsScreen from "./screens/SettingsScreen/SettingsScreen.js";
const navigationRef = React.createRef();
const Stack = createStackNavigator();
const headerAttributes = {
fontSize: 17,
fontFamily: "Inter-bold",
color: Colors.grey_font,
};
export default function App(props) {
const isLoadingComplete = useCachedResources();
async function handleAuth() {
try {
const idHolder = await AsyncStorage.getItem(storage_keys.SAVE_ID_KEY);
const tokenHolder = await AsyncStorage.getItem(
storage_keys.SAVE_TOKEN_KEY
);
if (idHolder && tokenHolder) {
await fetchUserAndNavigate(tokenHolder);
}
} catch (e) {
alert(e);
}
}
async function fetchUserAndNavigate(token) {
var url = homeURL + "/api/users/current";
try {
const res = await fetch_a(token, "token", url, {
method: "get",
});
if (res.ok) {
let user = await res.json();
if (user._id && user._id.length !== 0) {
console.log("\n***APP.JS*** User Name: " + user.first_name + "\n");
console.log(
"---- ***APP.JS*** User successfully fetched. Token is active. User is authorized ----\n"
);
navigationRef.current.navigate("Covaid", { user: user });
} else {
console.log(
"---- ***APP.JS*** Fetch ok, but user ID could not be found. Token is expired. User is unauthorized ----\n"
);
navigationRef.current.navigate("Login");
}
} else {
console.log(
"---- ***APP.JS*** User could not be fetched. Token is expired. User is unauthorized ----\n"
);
navigationRef.current.navigate("Login");
}
} catch (e) {
console.log("***APP.js*** fetchUserAndNavigate error: " + e);
// navigationRef.current.navigate("Login");
}
}
useEffect(() => {
handleAuth();
}, []);
if (!isLoadingComplete) {
return null;
}
return (
<View style={styles.container}>
{Platform.OS === "ios" && <StatusBar barStyle="dark-content" />}
<NavigationContainer linking={LinkingConfiguration} ref={navigationRef}>
<Stack.Navigator
initialRouteName="Login"
screenOptions={{
headerStyle: {
borderBottomColor: "rgba(206, 206, 206,0.95)",
borderBottomWidth: 0.25,
shadowColor: Colors.shadowColor,
shadowOffset: {
height: 1,
},
shadowOpacity: 0.05,
shadowRadius: 4,
},
}}
>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{
headerShown: false,
headerTitleStyle: headerAttributes,
}}
/>
<Stack.Screen
name="Covaid"
component={BottomTabNavigator}
options={{
gestureEnabled: false,
headerTitleStyle: headerAttributes,
}}
/>
<Stack.Screen
name="Edit Profile"
component={EditProfileScreen}
options={{
headerTitleStyle: headerAttributes,
}}
/>
<Stack.Screen
name="Edit Offer"
component={EditOfferScreen}
options={{
headerTitleStyle: headerAttributes,
}}
/>
<Stack.Screen
name="Edit Details"
component={EditDetailsScreen}
options={{
headerTitleStyle: headerAttributes,
}}
/>
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{
headerTitleStyle: headerAttributes,
}}
/>
</Stack.Navigator>
</NavigationContainer>
</View>
);
}
// }
const styles = StyleSheet.create({
container: {
backgroundColor: "#fff",
flex: 1,
},
});