-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
308 lines (285 loc) · 7.89 KB
/
App.tsx
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Vendor
import React, {useEffect} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {
Badge,
HStack,
NativeBaseProvider,
Pressable,
Text,
Icon,
} from 'native-base';
import Ionicon from 'react-native-vector-icons/Ionicons';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {Platform, StyleSheet} from 'react-native';
import {LogBox} from 'react-native';
// Expo
// import AppLoading from 'expo-app-loading'
// State
//- Redux
import {Provider} from 'react-redux';
import store from './redux/store';
import {PersistGate} from 'redux-persist/integration/react';
import {persistStore} from 'redux-persist';
import {useAppDispatch, useAppSelector} from './redux/features/hooks';
import {
checkIsAdmin,
getLoadingState,
getLoggedState,
logUserOut,
} from './redux/features/auth/authSlice';
import {auth} from './firebaseSetup';
// Components
//- Screens
import LandingScreen from './src/components/Screens/GlobalWrappers/LandingScreen';
import RegisterScreen from './src/components/Screens/Authentication/RegisterScreen';
import LoginScreen from './src/components/Screens/Authentication/LoginScreen';
import HomeScreen from './src/components/Screens/GlobalWrappers/HomeScreen';
import AddPhotoScreen from './src/components/Screens/Report/TakePhotoScreen';
import BrowsePhotoScreen from './src/components/Screens/Report/BrowsePhotoScreen';
import CreateReportScreen from './src/components/Screens/Report/CreateReportScreen';
//- Molecules
import LoadingIndicator from './src/components/Molecules/LoadingIndicator';
import AdminFeed from './src/components/Organisms/AdminFeed';
import Profile from './src/components/Organisms/Profile';
import {
getUnnaprovedReportsCount,
setReports,
} from './redux/features/reports/reportsSlice';
import {fetchAllReports} from './redux/features/reports/reportOperations';
// Nav config
const Stack = createNativeStackNavigator();
let persistor = persistStore(store);
// Ignoring the "Long timer" warning for Android that is caused by long timer in firebase storage
if (Platform.OS !== 'web') {
LogBox.ignoreLogs(['Setting a timer']);
}
// Creating an additional element to wrap it in the AuthProvider as Expo is missing index.ts which would usually be the wrapepr
const Index = () => {
const dispatch = useAppDispatch(),
loaded = useAppSelector(getLoadingState),
loggedInState = useAppSelector(getLoggedState);
console.log('Logged on land ', loggedInState);
const isAdmin = useAppSelector(checkIsAdmin);
console.log('Is admin: ', isAdmin);
// Trigger signout func in order to clear the persisted state
useEffect(() => {
if (!loggedInState) {
SignOut();
} else {
try {
(async () => {
const fetchedReports = await fetchAllReports();
dispatch(
setReports(JSON.parse(JSON.stringify(fetchedReports)))
);
})();
} catch (e) {
console.log('Error on getting all reports: ', e);
}
}
}, [loggedInState]);
// Get All reports on start
// useEffect(() => {
// if (loggedInState) {
// // Only try getting reports once
// try {
// (async () => {
// const fetchedReports = await fetchAllReports();
// dispatch(
// setReports(JSON.parse(JSON.stringify(fetchedReports)))
// );
// })();
// } catch (e) {
// console.log('Error on getting all reports: ', e);
// }
// }
// console.log('APP.ts useEffect');
// }, []);
// Count the pending ones
const unapprovedReprotsCount = useAppSelector(getUnnaprovedReportsCount);
// Sign the user out on demand
// Clear the local state
// Cleare Redux state
// Purge the persisted state
const SignOut = async () => {
await auth.signOut();
// dispatch(logUserOut())
// TODO: Should this be here or in an action? / Should it purge everything?
persistor.purge();
console.log('Logged out');
};
return (
<>
{loaded ? (
<NavigationContainer>
<Stack.Navigator initialRouteName='Home'>
{loggedInState ? (
<>
<Stack.Screen
name='Home'
component={HomeScreen}
options={({navigation, route}) => ({
headerBackVisible: false,
headerBackButtonMenuEnabled: false,
headerStyle: {
backgroundColor: '#203a43',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
headerRight: () => {
return (
<>
{isAdmin && (
<Pressable
px='4'
onPress={() =>
navigation.navigate(
'AdminFeed'
)
}
>
<HStack>
<Badge
colorScheme='info'
rounded='999px'
zIndex={1}
mr={2}
variant='outline'
alignSelf='flex-end'
_text={{
fontSize: 6,
}}
>
<Text>
{
unapprovedReprotsCount
}
</Text>
</Badge>
<Icon
color={
'#fff'
}
size='lg'
as={
<Ionicon name='beer-outline' />
}
/>
</HStack>
</Pressable>
)}
<Pressable
onPress={() =>
navigation.navigate(
'Profile'
)
}
mr='10px'
>
<Icon
color={'#fff'}
size='lg'
as={
<Ionicon name='person-circle-outline' />
}
/>
</Pressable>
</>
);
},
})}
/>
<Stack.Screen
name='Add Photo'
component={AddPhotoScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name='Browse Photo'
component={BrowsePhotoScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name='Create Report'
component={CreateReportScreen}
options={() => ({
headerStyle: {
backgroundColor: '#203a43',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
})}
/>
<Stack.Screen
name='Profile'
component={Profile}
options={() => ({
headerStyle: {
backgroundColor: '#203a43',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
})}
/>
{isAdmin && (
<Stack.Screen
name='AdminFeed'
component={AdminFeed}
/>
)}
</>
) : (
<>
<Stack.Screen
name='Landing'
component={LandingScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name='Login'
component={LoginScreen}
options={{headerShown: false}}
/>
<Stack.Screen
name='Register'
component={RegisterScreen}
options={{headerShown: false}}
/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
) : (
<LoadingIndicator />
)}
</>
);
};
// The app itself wrapped in providers
export default function App() {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<SafeAreaProvider>
<NativeBaseProvider config={config}>
<Index />
</NativeBaseProvider>
</SafeAreaProvider>
</PersistGate>
</Provider>
);
}
const styles = StyleSheet.create({});
const config = {
dependencies: {
'linear-gradient': require('expo-linear-gradient').LinearGradient,
},
};