Fakultas Ilmu Komputer UI

index.tsx 1.71 KB
Newer Older
Wulan Mantiri's avatar
Wulan Mantiri committed
1
import React, { FC, useContext } from 'react';
2
3
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
4
import { ThemeProvider } from 'react-native-elements';
5
import Toast from 'react-native-toast-message';
6

Wulan Mantiri's avatar
Wulan Mantiri committed
7
import { DietelaCoverLoader } from 'components/core';
Wulan Mantiri's avatar
Wulan Mantiri committed
8
import ContextProvider, { UserContext } from 'provider';
9
import { theme } from 'styles/theme';
10

11
import { screenOptions, toastConfig } from './styles';
12
import LogoutButton from './LogoutButton';
Wulan Mantiri's avatar
Wulan Mantiri committed
13
import { getNavigation } from './schema';
14

15
16
const Stack = createStackNavigator();

Wulan Mantiri's avatar
Wulan Mantiri committed
17
const NavigationStack: FC = () => {
Wulan Mantiri's avatar
Wulan Mantiri committed
18
19
20
  const { isAuthenticated, isUnpaidClient, isFirstLoading } = useContext(
    UserContext,
  );
Wulan Mantiri's avatar
Wulan Mantiri committed
21
22
23
24
25
26

  const { initialRoute, navigation } = getNavigation(
    isAuthenticated,
    isUnpaidClient,
  );

Wulan Mantiri's avatar
Wulan Mantiri committed
27
28
29
  if (isFirstLoading) {
    return <DietelaCoverLoader />;
  }
Wulan Mantiri's avatar
Wulan Mantiri committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  return (
    <NavigationContainer>
      <Stack.Navigator
        initialRouteName={initialRoute}
        screenOptions={screenOptions}>
        <>
          {navigation.map((nav, i) => (
            <Stack.Screen
              key={`nav${i}`}
              name={nav.name}
              component={nav.component}
              options={{
                title: nav.header,
                headerShown: Boolean(nav.header),
                headerRight: LogoutButton,
              }}
            />
          ))}
        </>
      </Stack.Navigator>
    </NavigationContainer>
  );
};

54
55
const App: FC = () => {
  return (
Wulan Mantiri's avatar
Wulan Mantiri committed
56
    <ThemeProvider theme={theme}>
57
      <ContextProvider>
Wulan Mantiri's avatar
Wulan Mantiri committed
58
        <NavigationStack />
59
60
        <Toast config={toastConfig} ref={Toast.setRef} />
      </ContextProvider>
Wulan Mantiri's avatar
Wulan Mantiri committed
61
    </ThemeProvider>
62
63
64
65
  );
};

export default App;