-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootWrapper.js
318 lines (302 loc) · 10.3 KB
/
RootWrapper.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
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
309
310
311
312
313
314
315
316
317
318
/******************************************************************************
* RootWrapper
*
* This page contains the main navigation stack as well as global
* functions and navigation flow. It is the first page that is rendered
* when the app is opened. This page causes the app to navigate to the
* appropriate page based on the user's current state. This page also
* causes the user data to be refreshed when auth state changes.
*
* Written by: William Soylemez
* Last edited: July 29, 2024
*
*****************************************************************************/
import { AppState } from 'react-native';
import React, { useEffect } from 'react';
import MEDrawerNavigator from '../components/drawer/Drawer';
import MEBottomSheet from '../components/modal/MEBottomSheet';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {
fetchCommunities,
fetchUserProfile,
setActiveCommunityAction,
setFirebaseAuthenticationAction,
toggleUniversalModalAction,
fetchAllUserInfo,
setQuestionnaireInfo
} from '../config/redux/actions';
import { useNavigation } from '@react-navigation/native';
import { isUserAuthenticated } from '../config/firebase';
import { GoogleSignin } from '@react-native-google-signin/google-signin';
import CommunitySelect from './community-select/CommunitySelect';
import { createStackNavigator } from '@react-navigation/stack';
import LoadingScreen from './misc/LoadingScreen';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { COMMUNITY_CHOICE, DONE_ONBOARDING, QUESTIONNAIRE_DATA } from '../utils/values';
import ActionDetails from './actions/ActionDetails';
import EventDetails from './events/EventDetails';
import OnboardingPage from './onboarding/Onboarding';
import TeamDetails from './teams/TeamDetails';
import Login from './auth/Login';
import Register from './auth/Register';
import ServiceProviderDetails from './service-providers/ServiceProviderDetails';
import TestimonialDetails from './testimonials/TestimonialDetails';
import ForgotPassword from './auth/ForgotPassword';
import SettingsPage from './user-profile/SettingsPage';
import ActionsScreen from './actions/ActionsScreen';
import EventsScreen from './events/EventsScreen';
import ImpactPage from './home/ImpactPage';
import AddTestimonial from './testimonials/AddTestimonial';
import EmailOnly from './auth/EmailOnly';
import AddEvent from './events/AddEvent';
import AddTeam from './teams/AddTeam';
import Questionnaire from './onboarding/Questionnaire';
import { endSession, startSession } from '../api/analytics';
GoogleSignin.configure({
webClientId:
'72842344535-02nclr1jgldostc3ajve2up59ice44gv.apps.googleusercontent.com',
});
const MainStack = createStackNavigator();
const screenOptions = {
// headerShown: false,
headerTintColor: "black",
headerBackTitleVisible: false,
};
const RootWrapper = ({
zipcodeOptions,
modalOptions,
toggleModal,
setFirebaseAuth,
fetchCommunitiesFromBackend,
// communities,
activeCommunity,
setActiveCommunity,
fetchMEUser,
fetchAllUserInfo,
setQuestionnaireInfo
}) => {
const navigation = useNavigation();
/* Effect for navigating to loading screen if saved community, or back to
* onboarding if needed
*/
useEffect(() => {
Promise.all([
AsyncStorage.getItem(COMMUNITY_CHOICE),
AsyncStorage.getItem(DONE_ONBOARDING),
])
.then(response => {
const [choice, onboarding] = response;
if (!onboarding) return navigation.navigate('Onboarding');
if (!choice) return navigation.navigate('CommunitySelectionPage');
navigation.navigate('Loading', { community_id: choice });
})
.catch(e => console.log('ERROR_FETCHING_SAVED_CHOICE', e.toString()));
}, []);
/* Get the list of communities from the backend and current community choice */
useEffect(() => {
const { zipcode, miles } = zipcodeOptions || {};
// First time app launches, it will load a few communities at 10 miles... (zipcode is set as wayland zip, and 10 miles check reducers.js)
fetchCommunitiesFromBackend({ zipcode, maxDistance: miles }, data => {
if (!data || activeCommunity?.id) return;
// There is a scenario where the list of communities will not be ready by the time the useEffect above has retrieved a saved community choice
// In that case, that process can go ahead and load the other community data.
// When the community list is retrieved, the object of the chosen community will be found here
// and set to redux quitely
// Catch: all of this is if activeCommunity value in redux store is empty ofcourse
AsyncStorage.getItem(COMMUNITY_CHOICE)
.then((communityChoice) => {
// If there is no saved community choice, we go to community selection page
const found = data?.find(com => com.id?.toString() === communityChoice);
if (!found) return navigation.navigate('CommunitySelectionPage'); // This means the saved choice of community is not in the community list retrieved. In such case we go babck to community selection page
setActiveCommunity(found);
})
.catch(e =>
console.log(
'ERROR_FINDING_COMMUNITY_OBJECT_FOR_SAVED_CHOICE',
e.toString(),
),
);
});
}, []);
/* Check if user is authenticated */
useEffect(() => {
isUserAuthenticated((yes, user) => {
console.log('USER IS FIREBASE_AUTHENTICATED: ', user?.email);
if (yes) {
setFirebaseAuth(user);
user?.getIdToken().then(token => {
fetchMEUser(token);
});
fetchAllUserInfo();
} else console.log('User is not signed in yet!');
});
}, []);
/* Check for questionaire completion */
useEffect(() => {
AsyncStorage.getItem(QUESTIONNAIRE_DATA)
.then(data => {
setQuestionnaireInfo(JSON.parse(data));
})
.catch(e => console.log('ERROR_FETCHING_QUESTIONAIRE_DATA', e.toString()));
}, []);
/* Log opening and closing app */
useEffect(() => {
const handleAppStateChange = (nextAppState) => {
if (nextAppState === 'active') {
startSession();
} else if (nextAppState.match(/background/)) {
endSession();
}
};
const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => {
subscription.remove();
};
}, []);
return (
// <NavigationContainer>
<>
{/* Main app navigation stack */}
<MainStack.Navigator screenOptions={screenOptions} initialRouteName="Onboarding">
<MainStack.Screen
options={{ headerShown: false }}
name="Onboarding"
component={OnboardingPage}
/>
<MainStack.Screen
// options={{headerShown: false}}
name="Questionnaire"
component={Questionnaire}
/>
<MainStack.Screen
options={{ headerShown: false }}
name="CommunitySelectionPage"
component={CommunitySelect}
/>
<MainStack.Screen
options={{ headerShown: false }}
name="CommunityPages"
component={MEDrawerNavigator}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="Impact"
component={ImpactPage}
/>
<MainStack.Screen
options={{ headerShown: false }}
name="Loading"
component={LoadingScreen}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="ActionDetails"
component={ActionDetails}
/>
<MainStack.Screen
name='Actions'
component={ActionsScreen}
/>
<MainStack.Screen
name="ServiceProviderDetails"
component={ServiceProviderDetails}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="TestimonialDetails"
component={TestimonialDetails}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="EventDetails"
component={EventDetails}
/>
<MainStack.Screen
name="Events"
component={EventsScreen}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="SubteamDetails"
component={TeamDetails}
/>
<MainStack.Screen
options={{ headerTitle: '' }}
name="TeamDetails"
component={TeamDetails}
/>
<MainStack.Screen
name="Login"
component={Login}
/>
<MainStack.Screen
name="Register"
component={Register}
/>
<MainStack.Screen
name="ForgotPassword"
component={ForgotPassword}
/>
<MainStack.Screen
name="EmailOnly"
component={EmailOnly}
/>
<MainStack.Screen
name="Settings"
component={SettingsPage}
/>
<MainStack.Screen
options={{
headerTitle: ''
}}
name="AddTestimonial"
component={AddTestimonial}
/>
<MainStack.Screen
options={{
headerTitle: ''
}}
name="AddEvent"
component={AddEvent}
/>
<MainStack.Screen
options={{
headerTitle: ''
}}
name="AddTeam"
component={AddTeam}
/>
</MainStack.Navigator>
<MEBottomSheet
{...(modalOptions || {})}
onClose={() => {
toggleModal({ isVisible: false, component: <></> });
modalOptions?.onClose && modalOptions.onClose();
}}
/>
</>
// </NavigationContainer>
);
};
const mapStateToProps = state => ({
modalOptions: state.modalOptions,
fireAuth: state.fireAuth,
zipcodeOptions: state.zipcodeOptions,
activeCommunity: state.activeCommunity,
});
const mapDispatchToProps = dispatch => {
return bindActionCreators(
{
toggleModal: toggleUniversalModalAction,
setFirebaseAuth: setFirebaseAuthenticationAction,
fetchCommunitiesFromBackend: fetchCommunities,
setActiveCommunity: setActiveCommunityAction,
fetchMEUser: fetchUserProfile,
fetchAllUserInfo: fetchAllUserInfo,
setQuestionnaireInfo: setQuestionnaireInfo
},
dispatch,
);
};
export default connect(mapStateToProps, mapDispatchToProps)(RootWrapper);