Fakultas Ilmu Komputer UI

RootNavigator.tsx 3.11 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 CreateEcosystemStackNavigator from "./CreateEcosystemStackNavigator";
5
import { RootStackParamList } from "../types/navigation";
6
import AuthStackNavigator from "./AuthStackNavigator";
Abraham Rudolf Brahmana's avatar
Abraham Rudolf Brahmana committed
7
import SuccessChangePasswordScreen from "../screens/SuccessChangePasswordScreen";
8
import { IUser } from "../types/firestore/User";
9
10
11
12
13
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";
14
15
16
17
18
19
20
21

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

export default RootNavigator;