-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
73 lines (62 loc) · 2.15 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
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import * as firebase from 'firebase';
import 'firebase/firestore';
import firebaseConfig from './firebaseConfig';
// Check if firebase is already initialized, if not, initialize firebaseConfig
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
import PlantDashboardScreen from './screens/PlantDashboardScreen';
import LandingScreen from './screens/LandingScreen';
import RegisterScreen from './screens/RegisterScreen';
import AddPlantScreen from './screens/AddPlantScreen';
import PlantDetailScreen from './screens/PlantDetailScreen';
import AddLogScreen from './screens/AddLogScreen';
const Stack = createStackNavigator();
const onAuthStateChange = (callback) => {
return firebase.auth().onAuthStateChanged((user) => {
if (user) {
callback({ loggedIn: true });
} else {
callback({ loggedIn: false });
}
});
};
const App = () => {
const [user, setUser] = useState({ loggedIn: false });
useEffect(() => {
const unsubscribe = onAuthStateChange(setUser);
return () => {
unsubscribe();
};
}, []);
if (!user.loggedIn) {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Landing">
<Stack.Screen
name="Landing"
component={LandingScreen}
options={{ headerShown: false }}
/>
<Stack.Screen name="Register" component={RegisterScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Dashboard">
<Stack.Screen name="Dashboard" component={PlantDashboardScreen} />
<Stack.Screen name="AddPlant" component={AddPlantScreen} />
<Stack.Screen name="PlantDetail" component={PlantDetailScreen} />
<Stack.Screen name="AddLog" component={AddLogScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;