-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
198 lines (168 loc) · 5.9 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { NavigationContainer} from '@react-navigation/native';
import { createNativeStackNavigator} from '@react-navigation/native-stack';
import React, {useEffect, useState} from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import FirstScreen from './src/screens/Auth/FirstScreen'
import * as Font from 'expo-font'; // Import the Font module
import StyleUtils from './src/utils/StyleUtils';
import { FontAwesome5 } from '@expo/vector-icons'; // Import FontAwesome5 icons
import UserMenu from './src/screens/MainMenu/UserMenu';
import UserSettings from './src/screens/Settings/UserSettings';
import UserClubScreen from './src/screens/Club/UserClubScreen';
import Menu from './src/screens/MainMenu/Menu';
import AddClubScreen from './src/screens/Club/AddClubScreen';
import AddFloorScreen from './src/screens/Club/AddFloorScreen';
import MyClubScreen from './src/screens/Club/MyClubScreen';
import EditClubScreen from './src/screens/Club/EditClub';
import {User, onAuthStateChanged} from 'firebase/auth';
import {doc, getDoc} from 'firebase/firestore';
import { FIREBASE_AUTH, FIRESTORE_INSTANCE } from './src/firebase/FirebaseConfig';
const Stack = createNativeStackNavigator();
const InsideStack = createNativeStackNavigator();
const UserStack = createNativeStackNavigator();
const UserTab = createBottomTabNavigator();
//TODO Treba odvojen screen za ovu navigaciju napraviti
//Obican user layout
//Privremeno dodan AddClub, on ce ici u stack ispod
function InsideLayout() {
return (
<InsideStack.Navigator>
<InsideStack.Screen name="Menu" component={Menu} options={{ headerShown: false }} />
<InsideStack.Screen name="AddClub" component={AddClubScreen} options={{ headerShown: false }} />
<InsideStack.Screen name="AddFloor" component={AddFloorScreen} options={{ headerShown: false }}/>
<InsideStack.Screen name="MyClub" component={MyClubScreen} options={{ headerShown: false }} />
<InsideStack.Screen name="EditClub" component={EditClubScreen} options={{ headerShown: false }} />
</InsideStack.Navigator>
);
}
function UserStackNavigator() {
return (
<UserStack.Navigator>
<UserStack.Screen
name="UserMenu"
component={UserMenu}
options={{ headerShown: false }}
/>
<UserStack.Screen
name="UserClubScreen"
component={UserClubScreen}
options={{ headerShown: false }}
/>
</UserStack.Navigator>
);
}
function UserLayout() {
return (
<UserTab.Navigator
screenOptions={{
tabBarLabelActiveTintColor: StyleUtils.PRIMARY_COLOR,
tabBarInactiveTintColor: StyleUtils.TEXT_COLOR,
tabBarStyle: [
{
backgroundColor: StyleUtils.SECONDARY_COLOR,
},
],
}}
>
<UserTab.Screen
name="UserMenu"
component={UserStackNavigator}
options={{
tabBarLabel: 'Menu',
tabBarIcon: ({ color }) => (
<FontAwesome5 name="bars" size={20} color={color} />
),
headerShown: false,
}}
/>
<UserTab.Screen
name="UserSettings"
component={UserSettings}
options={{
tabBarLabel: 'Profile',
tabBarIcon: ({ color }) => (
<FontAwesome5 name="user" size={20} color={color} />
),
headerShown: false,
}}
/>
</UserTab.Navigator>
);
}
//TODO Ispod treba napravit drugi layout za korisnika
export default function App() {
const [dataLoaded, setDataLoaded] = useState(false);
const [user, setUser] = useState(null);
const [isAdmin, setIsAdmin] = useState(false); // State to store admin status
//Provjeri font stanje - obrisano
useEffect(() => {
// Load fonts when the component mounts
const loadFonts = async () => {
await Font.loadAsync({
'Roboto-Regular': require('./src/utils/fonts/Roboto-Regular.ttf'),
'Roboto-Bold': require('./src/utils/fonts/Roboto-Bold.ttf')
// Load other fonts if needed
});
setDataLoaded(true); // Set dataLoaded to true when fonts are loaded
};
loadFonts(); // Call the function to load fonts
}, []);
useEffect(() => {
const unsubscribe = onAuthStateChanged(FIREBASE_AUTH, (user) => {
setUser(user);
console.log('User status', user);
if (user) {
console.log('USER EXISTS!');
const userDocRef = doc(FIRESTORE_INSTANCE, 'user', user.uid);
getDoc(userDocRef)
.then((docSnapshot) => {
if (docSnapshot.exists()) {
console.log('Snapshot does exist!');
const userData = docSnapshot.data();
const userIsAdmin = userData.admin;
setIsAdmin(userIsAdmin);
console.log('Admin check:', userIsAdmin);
} else {
setIsAdmin(false);
}
})
.catch((error) => {
console.error('Error fetching user data:', error);
});
} else {
setIsAdmin(false);
}
});
// Return a cleanup function to unsubscribe when the component unmounts
return () => unsubscribe();
}, [FIREBASE_AUTH]); // Include FIREBASE_AUTH in the dependencies array
if (!dataLoaded) {
// Wait for fonts to be loaded before rendering the app
return null;
}
return (
<NavigationContainer>
<Stack.Navigator initialRouteName='FirstScreen'>
{user ? ( isAdmin ? (
<Stack.Screen
name="Inside"
component={InsideLayout}
options={{ headerShown: false }}
/>
):(
<Stack.Screen
name="User"
component={UserLayout}
options={{ headerShown: false }}
/>
)):(
<Stack.Screen
name="FirstScreen"
component={FirstScreen}
options={{ headerShown: false }}
/>
)}
</Stack.Navigator>
</NavigationContainer>
);
}