-
Notifications
You must be signed in to change notification settings - Fork 13
/
App.js
123 lines (110 loc) · 3.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
import React, { useCallback, useEffect, useState } from 'react';
import { ActivityIndicator, SafeAreaView, StyleSheet } from 'react-native';
import CustomerIoSDKConfig from './src/data/sdk/CustomerIoSDKConfig';
import AppNavigator from './src/navigation/AppNavigator';
import {
initializeCustomerIoSDK,
onUserLoggedIn,
onUserLoggedOut,
registerInAppEventListener,
} from './src/services/CustomerIOService';
import StorageService from './src/services/StorageService';
import { CustomerIoSdkContext } from './src/state/customerIoSdkState';
import { UserStateContext } from './src/state/userState';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
export default function App() {
const [loading, setLoading] = useState(true);
const [userState, setUserState] = useState(undefined);
const [customerIoSdkState, setCustomerIoSdk] = useState(undefined);
useEffect(() => {
const prepare = async () => {
const storageService = new StorageService();
const storage = await storageService.loadAll();
updateUserState(storage.user);
const sdkConfig = applyCustomerIoConfig(storage.sdkConfig);
updateCustomerIoSdkState(sdkConfig);
setLoading(false);
};
prepare();
const inAppEventListener = registerInAppEventListener();
return () => {
inAppEventListener.remove();
};
}, [
applyCustomerIoConfig,
handleCustomerIoConfigChanged,
updateCustomerIoSdkState,
updateUserState,
]);
const updateCustomerIoSdkState = useCallback(
(config) => {
setCustomerIoSdk({
config: config,
onSdkConfigStateChanged: handleCustomerIoConfigChanged,
});
},
[handleCustomerIoConfigChanged]
);
const handleCustomerIoConfigChanged = useCallback(
async (config) => {
const storageService = new StorageService();
const sdkConfig = applyCustomerIoConfig(config);
updateCustomerIoSdkState(sdkConfig);
await storageService.saveSDKConfigurations(config);
},
[applyCustomerIoConfig, updateCustomerIoSdkState]
);
const applyCustomerIoConfig = useCallback((config) => {
const sdkConfig = CustomerIoSDKConfig.applyDefaultForUndefined(config);
initializeCustomerIoSDK(sdkConfig);
return sdkConfig;
}, []);
const updateUserState = useCallback(
(user) => {
setUserState({
user: user,
onUserStateChanged: handleUserStateChanged,
});
},
[handleUserStateChanged]
);
const handleUserStateChanged = useCallback(
async (user) => {
setLoading(true);
const storageService = new StorageService();
if (user) {
// Save user to storage
await storageService.saveUser(user);
// Identify user to Customer.io
onUserLoggedIn(user);
} else {
// Clear user identify from Customer.io
onUserLoggedOut();
// Clear user from storage
await storageService.clearUser();
}
updateUserState(user);
setLoading(false);
},
[updateUserState]
);
if (loading) {
return <ActivityIndicator />;
}
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<CustomerIoSdkContext.Provider value={customerIoSdkState}>
<UserStateContext.Provider value={userState}>
<SafeAreaView style={styles.container}>
<AppNavigator />
</SafeAreaView>
</UserStateContext.Provider>
</CustomerIoSdkContext.Provider>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});