-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
176 lines (162 loc) · 5.45 KB
/
App.tsx
File metadata and controls
176 lines (162 loc) · 5.45 KB
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
import React, { useEffect, useState } from 'react';
import { useFonts } from 'expo-font';
import AppLoading from 'expo-app-loading';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import LoginScreen from './src/screens/LoginScreen';
import MainScreen from './src/screens/MainScreen';
import Navbar from './src/components/Navbar';
import { PortalProvider } from '@gorhom/portal';
import { RootStackParamsList } from './src/types/navigation';
import BarcodeScanner from './src/screens/BarcodeScanner';
import { Colors } from './src/global-styles';
import ItemDetail from './src/screens/ItemDetail';
import { UserProvider } from './src/contexts/UserContext';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { User } from './src/types/API';
import EditItemScreen from './src/screens/EditItemScreen';
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { InventoryProvider } from './src/contexts/InventoryContext';
import { StatusBar } from 'expo-status-bar';
import useLoader from './src/hooks/loading';
import ReservationScreen from './src/screens/ReservationScreen';
import CreateReservationScreen from './src/screens/CreateReservationScreen';
import { ReservationProvider } from './src/contexts/ReservationContext';
import AddItemScreen from './src/screens/AddItemScreen';
import jwtDecode from 'jwt-decode';
import { UserJWT } from './src/util/types';
import API from './src/util/API';
const Stack = createNativeStackNavigator<RootStackParamsList>();
type WithProvidersProps = {
children: React.ReactNode;
initialUserValue?: User;
};
const WithProviders = ({ children, initialUserValue }: WithProvidersProps) => (
<SafeAreaProvider>
<ActionSheetProvider>
<InventoryProvider>
<UserProvider initialValue={initialUserValue}>
<ReservationProvider>
<PortalProvider>{children}</PortalProvider>
</ReservationProvider>
</UserProvider>
</InventoryProvider>
</ActionSheetProvider>
</SafeAreaProvider>
);
const App = (): JSX.Element => {
const [initialRoute, setInitialRoute] = useState<keyof RootStackParamsList>('Login');
const [initialUserValue, setInitialUserValue] = useState<User | undefined>(undefined);
const loader = useLoader(false);
const [fontsLoaded, fontLoadError] = useFonts({
'Gotham-Book': require('./assets/fonts/Gotham-Book.ttf'),
'Gotham-Medium': require('./assets/fonts/Gotham-Medium.ttf'),
'Gotham-Bold': require('./assets/fonts/Gotham-Bold.ttf')
});
const loadUserFromStorage = async () => {
loader.startLoading();
// If the user was previously logged in, grab their credentials
// from AsyncStorage, set the the user context object,
// then take them to the main screen
try {
const user = await AsyncStorage.getItem('user');
const parsedUser = JSON.parse(user || '') as User;
const jwt = jwtDecode<UserJWT>(parsedUser.token);
const time = new Date();
time.setMinutes(time.getMinutes() - 30);
// Only log the user in if their stored JWT isn't within 30
// minutes of its expiration time
if (jwt.exp !== undefined && +time < jwt.exp * 1000) {
setInitialUserValue(parsedUser);
setInitialRoute('Main');
}
} catch (err) {
console.log('User not found in storage', err);
} finally {
loader.stopLoading();
}
};
const stack = (
<Stack.Navigator
initialRouteName={initialRoute}
screenOptions={{
gestureEnabled: false,
contentStyle: {
backgroundColor: Colors.appBackgroundColor
},
header: () => <Navbar />
}}
>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{ headerShown: false }}
/>
<Stack.Screen name="Main" component={MainScreen} />
<Stack.Screen
name="BarcodeScanner"
component={BarcodeScanner}
options={{
headerShown: false,
gestureEnabled: true
}}
/>
<Stack.Screen
name="ItemDetail"
component={ItemDetail}
options={{
headerShown: false,
gestureEnabled: true
}}
/>
<Stack.Screen
name="EditItemScreen"
component={EditItemScreen}
options={{ headerShown: false }}
/>
<Stack.Screen
name="ReservationScreen"
component={ReservationScreen}
options={{
headerShown: false,
gestureEnabled: true
}}
/>
<Stack.Screen
name="CreateReservationScreen"
component={CreateReservationScreen}
options={{
headerShown: false,
gestureEnabled: false
}}
/>
<Stack.Screen
name="AddItem"
component={AddItemScreen}
options={{ headerShown: false }}
/>
</Stack.Navigator>
);
useEffect(() => {
API.setupAxiosInterceptor();
loadUserFromStorage();
}, []);
useEffect(() => {
if (fontLoadError) {
console.error('Error loading fonts', fontLoadError);
}
}, [fontLoadError]);
if (!fontsLoaded || loader.isLoading) {
return <AppLoading />;
}
return (
<WithProviders initialUserValue={initialUserValue}>
<NavigationContainer>
<StatusBar style="dark" />
{stack}
</NavigationContainer>
</WithProviders>
);
};
export default App;