-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
113 lines (99 loc) · 2.74 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
import React, { useEffect } from 'react';
import { HomeScreen , MapScreen , AuthScreen , ProfileScreen, AdminScreen } from './screens';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { KeyboardAvoidingView, Platform } from 'react-native';
import { selectIsLoading, selectRole, selectUserToken, userCheckRole } from './slices/userSlice';
import { useDispatch, useSelector } from 'react-redux';
import { setUserToken } from './slices/userSlice'
import Loading from './stylesComponent/Loading';
export default function App() {
const Stack = createNativeStackNavigator();
const dispath = useDispatch();
const token = useSelector(selectUserToken);
const role = useSelector(selectRole);
const loading = useSelector(selectIsLoading);
const isLoggedIn = async() => {
try {
let userToken = await AsyncStorage.getItem('token');
dispath(setUserToken(userToken));
} catch (error) {
console.log(`isLoggedIn ${error}`)
}
}
const checkRoleUser = async() => {
try {
let userId = await AsyncStorage.getItem('userId');
dispath(userCheckRole(userId))
} catch (error) {
console.log(`checkRole ${error}`)
}
}
useEffect(() => {
isLoggedIn()
},[])
useEffect(() => {
checkRoleUser()
},[])
if(loading){
return(
<Loading styling='flex-1 items-center justify-center' />
)
}
return (
<NavigationContainer>
<SafeAreaProvider>
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={{ flex : 1 }}
keyboardVerticalOffset={Platform.OS === "ios" ? -64 : 0}
>
<Stack.Navigator>
{ token === null ?
<Stack.Screen
name='AuthScreen'
component={AuthScreen}
options={{
headerShown: false,
}}
/> :
role === 'admin' ?
<Stack.Screen
name='AdminScreen'
component={AdminScreen}
options={{
headerShown: false,
}}
/> :
<>
<Stack.Screen
name='HomeScreen'
component={HomeScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name='MapScreen'
component={MapScreen}
options={{
headerShown: false,
}}
/>
<Stack.Screen
name='ProfileScreen'
component={ProfileScreen}
options={{
headerShown: false,
}}
/>
</>
}
</Stack.Navigator>
</KeyboardAvoidingView>
</SafeAreaProvider>
</NavigationContainer>
);
}