-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
155 lines (143 loc) · 4.3 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
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
import React, { useEffect, useState } from "react";
import { Dimensions } from "react-native";
import { GluestackUIProvider, Text, Box } from "@gluestack-ui/themed";
import { config } from "@gluestack-ui/config"; // Optional if you want to use default theme
import {
NavigationContainer,
useNavigation,
useNavigationContainerRef,
} from "@react-navigation/native";
import { SafeAreaProvider } from "react-native-safe-area-context";
import Navigation from "./navigation/Navigation";
import Home from "./screens/Home";
import * as Linking from "expo-linking";
import Login from "./screens/Login";
import {
createStackNavigator,
StackNavigationProp,
} from "@react-navigation/stack";
import store, { RootState } from "./redux/store";
import { Provider, useDispatch } from "react-redux";
import {
setToken,
clearTokens,
AuthActionTypes,
setRoles,
setUserID,
} from "./redux/actions";
import { Dispatch } from "@reduxjs/toolkit";
import { decodeToken } from "./api/decodeToken";
import { useAppSelector } from "./redux/hooks";
import { State } from "react-native-gesture-handler";
import Toast, { ErrorToast } from "react-native-toast-message";
const prefix = Linking.createURL("/");
// console.log(prefix);
type RootStackParamList = {
Login: undefined;
Main: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
const { width, height } = Dimensions.get("window");
const App = (props) => {
const [deepLinkHandled, setDeepLinkHandled] = useState(false);
const [isReady, setIsReady] = useState(false);
const dispatch = useDispatch<Dispatch<AuthActionTypes>>();
const navigationRef = useNavigationContainerRef<RootStackParamList>();
const linking = {
prefixes: [prefix],
config: {
screens: {
Login: "Login",
Main: "Main",
Home: "home",
Camera: "camera",
Events: "events",
Profile: "profile",
Shop: "shop",
},
},
};
const toastConfig = {
error: (props) => (
<ErrorToast
{...props}
text1Style={{
fontSize: 20,
}}
text2Style={{
fontSize: 15,
}}
/>
),
};
useEffect(() => {
const handleDeepLink = (event: { url: string }) => {
console.log("handling deep link:", event.url);
const searchParams = new URL(event.url).searchParams;
const token = searchParams.get("token");
if (token) {
const decoded = decodeToken(token);
console.log("token", token, "decoded token", decoded);
console.log(decoded.roles);
if (!decoded.roles.includes("USER")) {
Toast.show({
type: "error",
text1: "No user found!",
text2: "Please register on the R|P website!",
topOffset: 50,
});
setDeepLinkHandled(false);
} else {
console.log('dispatched')
dispatch(setToken(token));
dispatch(setRoles(decoded.roles));
dispatch(setUserID(decoded.userId));
setDeepLinkHandled(true);
}
}
};
async function getInitialURL() {
const initialURL = await Linking.getInitialURL();
console.log("getting initial URL:", initialURL);
if (initialURL) handleDeepLink({ url: initialURL });
}
getInitialURL();
const listener = Linking.addEventListener("url", handleDeepLink);
return () => listener.remove();
}, [dispatch]);
useEffect(() => {
if (isReady && deepLinkHandled) {
console.log("navigating");
navigationRef.navigate("Main");
}
}, [isReady, deepLinkHandled]);
return (
<>
<GluestackUIProvider config={config}>
<SafeAreaProvider>
<NavigationContainer
linking={linking}
ref={navigationRef}
onReady={() => setIsReady(true)}
>
<Stack.Navigator
screenOptions={{ headerShown: false }}
initialRouteName="Login"
>
<Stack.Screen name="Login" component={Login} />
<Stack.Screen name="Main" component={Navigation} />
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
</GluestackUIProvider>
<Toast config={toastConfig} />
</>
);
};
export default function AppWrapper() {
return (
<Provider store={store}>
<App />
</Provider>
);
}