-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
executable file
·72 lines (59 loc) · 2.25 KB
/
App.tsx
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
import React, {memo} from "react";
import GlobalStyles from './components/shared/GlobalStyle';
import {AccountCreationNavigator} from "./navigation/accountCreationStackNavigator";
import {ApolloClient, ApolloProvider, createHttpLink, InMemoryCache, useReactiveVar} from "@apollo/client";
import {setContext} from "@apollo/client/link/context";
import {NavigationContainer} from "@react-navigation/native";
import {jwt} from "./constants/grafql/jwt";
import {createNativeStackNavigator} from "@react-navigation/native-stack";
import {SafeAreaProvider} from "react-native-safe-area-context";
import {useCustomResourceLoading} from "./services/CustomResourceLoading";
import {LoginScreen} from "./components/screens/LoginScreen";
import * as SplashScreen from 'expo-splash-screen';
import {RegisterScreen} from "./components/screens/oboarding_process/RegisterScreen";
SplashScreen.preventAutoHideAsync();
//Backend Link
const authLink = setContext((_, {headers}) => {
const token = jwt();
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
//GraphQL Endpoint
const httpLink = createHttpLink({
uri: 'http://192.168.1.155:1337/graphql',
});
const Stack = createNativeStackNavigator();
//Remember the client Cache
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
//Prevent unnecessary redraws
export const AppStack = memo(() => {
return <Stack.Navigator>
<Stack.Screen name="LoginScreen" component={LoginScreen} options={{headerShown: false}}/>
<Stack.Screen name="RegisterScreen" component={RegisterScreen} options={{headerShown: false}}/>
</Stack.Navigator>;
});
//Navigation checker
export const Navigation = () => {
const isLoggedIn = useReactiveVar(jwt);
return <NavigationContainer>
{!isLoggedIn ? <AppStack/> : <AccountCreationNavigator/>}
</NavigationContainer>
}
//Main application
export default function App() {
const isLoading = useCustomResourceLoading();
return isLoading ? null : (
<ApolloProvider client={client}>
<SafeAreaProvider style={GlobalStyles.droidSafeArea}>
<Navigation/>
</SafeAreaProvider>
</ApolloProvider>
);
}