-
Notifications
You must be signed in to change notification settings - Fork 14
/
appNavigation.jsx
111 lines (96 loc) · 2.93 KB
/
appNavigation.jsx
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
import React, { useEffect, useState, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import SplashScreen from 'screens/splashScreen';
import HomeScreen from 'screens/homeScreen';
import ContactsScreen from 'screens/contactsScreen';
import ProfileScreen from 'screens/profileScreen';
import SignInScreen from 'screens/signInScreen';
import SignUpScreen from 'screens/signUpScreen';
import TabIcon from 'components/tabIcon';
import { USER_AUTHENTICATED } from 'resources/user/user.constants';
import * as userSelectors from 'resources/user/user.selectors';
import { getItem } from 'helpers/storage';
import config from 'resources/config';
import images from 'themes/images';
const prefix = `${config.applicationId}://`;
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
const tabBarOptions = {
keyboardHidesTabBar: true,
};
const tabs = [
{
id: 1,
title: 'Home',
component: HomeScreen,
tabIcon: images.home,
activeTabIcon: images.homeActive,
},
{
id: 2,
title: 'Contacts',
component: ContactsScreen,
tabIcon: images.contacts,
activeTabIcon: images.contactsActive,
},
{
id: 3,
title: 'Profile',
component: ProfileScreen,
tabIcon: images.profile,
activeTabIcon: images.profileActive,
},
];
const AppNavigation = () => {
const dispatch = useDispatch();
const [isLoading, setIsLoading] = useState(true);
const userAuthenticated = useSelector(userSelectors.getUserAuthenticated);
const getToken = useCallback(async () => {
const token = await getItem('token');
config.token = token;
setIsLoading(false);
if (token) {
dispatch({ type: USER_AUTHENTICATED });
}
}, [dispatch]);
useEffect(() => {
getToken();
}, [getToken]);
if (isLoading) {
return (
<SplashScreen />
);
}
return (
<NavigationContainer linking={{ prefixes: [prefix] }}>
{userAuthenticated
? (
<Tab.Navigator initialRouteName="Home" tabBarOptions={tabBarOptions}>
{tabs.map(tab => (
<Tab.Screen
key={tab.id}
name={tab.title}
component={tab.component}
options={{
tabBarIcon: icon => (
<TabIcon source={icon.focused ? tab.activeTabIcon : tab.tabIcon} />
),
}}
/>
))}
</Tab.Navigator>
)
: (
<Stack.Navigator headerMode="none">
<Stack.Screen name="SignIn" component={SignInScreen} />
<Stack.Screen name="SignUp" component={SignUpScreen} />
</Stack.Navigator>
)
}
</NavigationContainer>
);
};
export default AppNavigation;