Fakultas Ilmu Komputer UI

RootNavigator.tsx 2.95 KB
Newer Older
1
import React, { useEffect, useState } from "react";
2
import { createNativeStackNavigator } from "@react-navigation/native-stack";
3
import BottomTabNavigator from "./BottomTabNavigator";
4
import { RootStackParamList } from "../types/navigation";
5
import AuthStackNavigator from "./AuthStackNavigator";
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
6
import SuccessChangePasswordScreen from "../screens/SuccessChangePasswordScreen";
7
import { IUser } from "../types/firestore/User";
8
9
10
11
12
import { useAppDispatch, useAppSelector } from "../hooks/reduxHooks";
import { getUser } from "../redux/user/actions";
import LandingScreen from "../screens/auth/LandingScreen";
import firebase from "firebase";
import { Alert } from "react-native";
13
14
15
16
17
18
19
20

/**
 * A root stack navigator is often used for displaying modals on top of all other content.
 * https://reactnavigation.org/docs/modal
 */
const RootStack = createNativeStackNavigator<RootStackParamList>();

const RootNavigator = () => {
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
  const user: IUser = useAppSelector((state) => state.user);
  const [isLoading, setIsLoading] = useState(true);
  const dispatch = useAppDispatch();

  useEffect(() => {
    const unsubscribeAuth = firebase
      .auth()
      .onAuthStateChanged((currentUser) => {
        try {
          // await (authenticatedUser ? setUser(authenticatedUser) : setUser(null));
          // setIsLoading(false);
          if (currentUser) {
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
            dispatch(getUser(currentUser.uid)).then(setIsLoading(false));
          } else {
            setIsLoading(false);
          }
        } catch (error) {
          console.log(error);
          Alert.alert("Error", error);
        }
      });

    // unsubscribe auth listener on unmount
    return unsubscribeAuth;
    // const currentUser = getCurrentAuthUser();
    // if (currentUser) {
    //   // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    //   dispatch(getUser(currentUser.uid)).then(setIsLoading(false));
    // } else {
    //   setIsLoading(false);
    // }
  }, []);

  return !isLoading ? (
56
    <RootStack.Navigator>
57
58
59
60
61
62
63
64
65
66
67
68
69
70
      {user.id === "" && (
        <RootStack.Screen
          name="Auth"
          component={AuthStackNavigator}
          options={{ headerShown: false }}
        />
      )}
      {user.id !== "" && (
        <RootStack.Screen
          name="MainTab"
          component={BottomTabNavigator}
          options={{ headerShown: false }}
        />
      )}
71
      <RootStack.Screen
72
73
        name="SuccessChangePassword"
        component={SuccessChangePasswordScreen}
74
75
        options={{ headerShown: false }}
      />
76
      {/* <RootStack.Screen
77
78
79
        name="CreateBusinessEcosystem"
        component={CreateBusinessEcosystemScreen}
        options={{ title: "" }}
80
      /> */}
81
82
83
    </RootStack.Navigator>
  ) : (
    <RootStack.Navigator>
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
84
      <RootStack.Screen
85
86
        name="RootLoading"
        component={LandingScreen}
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
87
88
        options={{ headerShown: false }}
      />
89
90
91
92
93
    </RootStack.Navigator>
  );
};

export default RootNavigator;