-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
283 lines (258 loc) · 8.5 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
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
import codePush from "react-native-code-push";
import React, { useCallback, useEffect, useState } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { StyleSheet, View, Platform } from "react-native";
import * as SplashScreen from "expo-splash-screen";
import { SafeAreaProvider } from "react-native-safe-area-context";
import FlashMessage from "react-native-flash-message";
import { StripeProvider } from "@stripe/stripe-react-native";
import * as Notifications from "expo-notifications";
import Config from "react-native-config";
const {
STRIPE_PUBLIC_KEY,
CODEPUSH_ANDROID_DEPLOYMENT_KEY,
CODEPUSH_IOS_DEPLOYMENT_KEY,
} = Config;
import {
useFonts,
Nunito_300Light,
Nunito_400Regular,
Nunito_500Medium,
Nunito_600SemiBold,
Nunito_700Bold,
Nunito_800ExtraBold,
} from "@expo-google-fonts/nunito";
import { Navigation } from "#navigation";
import { localStorage, Context, userSvc } from "#services";
import { RequireRegistration } from "#modals";
import { DropdownBackdrop } from "#backdrops";
import { FIVE_MINUTES } from "#utils";
import { GestureHandlerRootView } from "react-native-gesture-handler";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
// Create a react-query client
const queryClient = new QueryClient({
defaultOptions: { queries: { refetchInterval: FIVE_MINUTES } },
});
function App() {
let [loaded, error] = useFonts({
Nunito_300Light,
Nunito_400Regular,
Nunito_500Medium,
Nunito_600SemiBold,
Nunito_700Bold,
Nunito_800ExtraBold,
});
const [token, setToken] = useState();
const [initialRouteName, setInitialRouteName] = useState("TabNavigation"); // Initial route name for the AppNavigation
const [initialAuthRouteName, setInitialAuthRouteName] = useState("Welcome"); // Initial route name for the AuthNavigation
const [isTmpUser, setIsTmpUser] = useState(null); // Is the user logged in as a guest
const [isRegistrationModalOpan, setIsRegistrationModalOpen] = useState(false);
const [currencySymbol, setCurrencySymbol] = useState("");
const [userPin, setUserPin] = useState(); // The value of the user's PIN code
const [hasCheckedTmpUser, setHasCheckedTmpUser] = useState(false);
const [activeCoupon, setActiveCoupon] = useState();
const [isAnonymousRegister, setIsAnonymousRegister] = useState(false);
const [theme, setTheme] = useState(null);
const [isInConsultation, setIsInConsultation] = useState(false);
const [isLoginDisabled, setIsLoginDisabled] = useState(false);
const [hasAuthenticatedWithPin, setHasAuthenticatedWithPin] = useState(false);
const [dropdownOptions, setDropdownOptions] = useState({
isOpen: false,
options: [],
heading: "",
selectedOption: "",
handleChooseOption: () => {},
dropdownId: null,
});
const handleRegistrationModalClose = () => setIsRegistrationModalOpen(false);
const handleRegistrationModalOpen = () => setIsRegistrationModalOpen(true);
const handleRegisterRedirection = () => {
setInitialAuthRouteName("RegisterPreview");
handleRegistrationModalClose();
localStorage.removeItem("token");
localStorage.removeItem("refresh-token");
localStorage.removeItem("expires-in");
setToken(null);
};
useEffect(() => {
async function checkCurencySymbol() {
const localStorageCurrencySymbol =
await localStorage.getItem("currencySymbol");
if (!currencySymbol && localStorageCurrencySymbol) {
setCurrencySymbol(localStorageCurrencySymbol);
}
if (!localStorageCurrencySymbol && currencySymbol) {
await localStorage.setItem("currencySymbol", currencySymbol);
}
if (
localStorageCurrencySymbol &&
currencySymbol &&
localStorageCurrencySymbol !== currencySymbol
) {
await localStorage.setItem("currencySymbol", currencySymbol);
}
}
checkCurencySymbol();
}, [currencySymbol]);
const handleCodePushCheck = async (data) => {
const [token, pinCode] = data;
const clearTokenIfNoPinOrBiometrics = async () => {
// If the client doesn't have biometrics enabled and doesn't have a pin code remove the
// token from the local storage, so that re-authentication is required on next app launch
const hasBiometrics = await localStorage.getItem("biometrics-enabled");
// await localStorage.removeItem("has-declined-biometrics");
if (!hasBiometrics && !pinCode && token && !__DEV__) {
await localStorage.removeItem("token");
setToken(null);
}
};
const deploymentKey =
Platform.OS === "android"
? CODEPUSH_ANDROID_DEPLOYMENT_KEY
: CODEPUSH_IOS_DEPLOYMENT_KEY;
codePush.notifyApplicationReady();
codePush
.sync(
{
checkFrequency: codePush.CheckFrequency.ON_APP_RESUME,
installMode: codePush.InstallMode.IMMEDIATE,
minimumBackgroundDuration: 5,
// updateDialog: true,
rollbackRetryOptions: 5,
maxRetryAttempts: 999,
deploymentKey,
},
(status) => {
switch (status) {
// Clear token if no biometrics or pin code
// after the app has been updated
case codePush.SyncStatus.UP_TO_DATE:
clearTokenIfNoPinOrBiometrics();
break;
default:
break;
}
}
)
.catch((err) => {
console.log(err, "err");
});
};
useEffect(() => {
SplashScreen.preventAutoHideAsync();
async function checkToken() {
const token = await localStorage.getItem("token");
setToken(token);
const pinCode = await localStorage.getItem("pin-code");
setUserPin(pinCode);
return [token, pinCode];
}
checkToken().then((data) => {
handleCodePushCheck(data);
});
}, []);
useEffect(() => {
async function checkIsTmpUser() {
if (token) {
const userId = await userSvc.getUserID();
const tmpUser = userId === "tmp-user";
setIsTmpUser(tmpUser);
setHasCheckedTmpUser(true);
}
}
checkIsTmpUser();
}, [token]);
// Hide the splash screen when the fonts finish loading
const onLayoutRootView = useCallback(async () => {
if (loaded) {
await SplashScreen.hideAsync();
}
}, [loaded]);
if (error) {
return (
<View style={styles.container}>{JSON.stringify(error, null, 2)}</View>
);
}
if (!loaded) {
return null;
}
const contextValues = {
token,
setToken,
userPin,
initialRouteName,
setInitialRouteName,
isTmpUser,
setIsTmpUser,
handleRegistrationModalOpen,
initialAuthRouteName,
setInitialAuthRouteName,
currencySymbol,
setCurrencySymbol,
dropdownOptions,
setDropdownOptions,
hasCheckedTmpUser,
activeCoupon,
setActiveCoupon,
isAnonymousRegister,
setIsAnonymousRegister,
theme,
setTheme,
isLoginDisabled,
setIsLoginDisabled,
isInConsultation,
setIsInConsultation,
setUserPin,
hasAuthenticatedWithPin,
setHasAuthenticatedWithPin,
};
return (
<GestureHandlerRootView style={styles.flex1}>
<StripeProvider publishableKey={STRIPE_PUBLIC_KEY}>
<Context.Provider value={contextValues}>
<QueryClientProvider client={queryClient}>
<SafeAreaProvider>
<View style={styles.flex1} onLayout={onLayoutRootView}>
<Navigation
contextTheme={theme}
setTheme={setTheme}
isInConsultation={isInConsultation}
>
<DropdownBackdrop
onClose={() =>
setDropdownOptions((options) => ({
...options,
isOpen: false,
}))
}
{...dropdownOptions}
/>
<RequireRegistration
handleContinue={handleRegisterRedirection}
isOpen={isRegistrationModalOpan}
onClose={handleRegistrationModalClose}
/>
</Navigation>
</View>
</SafeAreaProvider>
<FlashMessage position="top" />
</QueryClientProvider>
</Context.Provider>
</StripeProvider>
</GestureHandlerRootView>
);
}
export default App;
const styles = StyleSheet.create({
container: {
backgroundColor: "#fff",
flex: 1,
},
flex1: { flex: 1 },
});