-
Notifications
You must be signed in to change notification settings - Fork 13
/
Settings.js
271 lines (251 loc) · 8.65 KB
/
Settings.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
import { HeaderBackButton } from '@react-navigation/elements';
import { useFocusEffect } from '@react-navigation/native';
import React, {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from 'react';
import {
BackHandler,
Clipboard,
Platform,
ScrollView,
StyleSheet,
View,
} from 'react-native';
import { FilledButton, TextButton } from '../components/Button';
import { SwitchField } from '../components/SwitchField';
import { Caption, Text } from '../components/Text';
import { TextField } from '../components/TextField';
import { styles as settingsStyles } from '../styles/stylesheet';
import CustomerIoSDKConfig from '../data/sdk/CustomerIoSDKConfig';
import { useCustomerIoSdkContext } from '../state/customerIoSdkState';
import { useUserStateContext } from '../state/userState';
import { resetRoute } from '../utils/navigation';
import Prompts from '../utils/prompts';
import { CustomerIO } from 'customerio-reactnative';
const Settings = ({ navigation, route }) => {
const { params } = route;
const initialSiteId = params?.site_id;
const initialCdpApiKey = params?.api_key;
const { config: initialConfig, onSdkConfigStateChanged } =
useCustomerIoSdkContext();
const { user } = useUserStateContext();
const defaultConfig = CustomerIoSDKConfig.createDefault();
const [deviceToken, setDeviceToken] = useState('');
const [siteId, setSiteId] = useState('');
const [cdpApiKey, setCdpApiKey] = useState('');
const [isTrackScreensEnabled, setTrackScreensEnabled] = useState(false);
const [isTrackDeviceAttributesEnabled, setTrackDeviceAttributesEnabled] =
useState(false);
const [isDebugModeEnabled, setDebugModeEnabled] = useState(false);
const [
isAppLifecycleEventTrackingEnabled,
setAppLifecycleEventTrackingEnabled,
] = useState(false);
const handleOnBackPress = useCallback(() => {
if (!navigation.canGoBack()) {
resetRoute(navigation, user);
return true;
}
return false;
}, [navigation, user]);
useLayoutEffect(() => {
if (!navigation.canGoBack()) {
navigation.setOptions({
headerLeft: (props) => (
<HeaderBackButton
{...props}
style={settingsStyles.backButton}
labelVisible={Platform.OS === 'ios'}
accessibilityLabel="Navigate up"
onPress={() => handleOnBackPress()}
/>
),
});
}
}, [handleOnBackPress, navigation]);
useFocusEffect(
useCallback(() => {
const subscription = BackHandler.addEventListener(
'hardwareBackPress',
handleOnBackPress
);
return () => subscription.remove();
}, [handleOnBackPress])
);
useEffect(() => {
CustomerIO.pushMessaging
.getRegisteredDeviceToken()
.then((token) => {
setDeviceToken(token);
})
.catch((error) => {
console.log(error);
});
setSiteId(initialSiteId ?? initialConfig.siteId);
setCdpApiKey(initialCdpApiKey ?? initialConfig.cdpApiKey);
setTrackScreensEnabled(initialConfig.trackScreens);
setTrackDeviceAttributesEnabled(initialConfig.trackDeviceAttributes);
setDebugModeEnabled(initialConfig.debugMode);
setAppLifecycleEventTrackingEnabled(initialConfig?.trackAppLifecycleEvents);
}, [initialCdpApiKey, initialConfig, initialSiteId]);
const handleRestoreDefaultsPress = async () => {
saveConfigurations(defaultConfig);
};
const isFormValid = () => {
let message;
let blankFieldMessageBuilder = (fieldName) => {
return `${fieldName} cannot be blank`;
};
if (!cdpApiKey) {
message = blankFieldMessageBuilder('CDP API Key');
}
if (message) {
Prompts.showAlert({
title: 'Error',
message: message,
});
return false;
}
return true;
};
const handleSavePress = () => {
if (!isFormValid()) {
return;
}
const config = new CustomerIoSDKConfig();
config.siteId = siteId;
config.cdpApiKey = cdpApiKey;
config.trackScreens = isTrackScreensEnabled;
config.trackDeviceAttributes = isTrackDeviceAttributesEnabled;
config.debugMode = isDebugModeEnabled;
config.trackAppLifecycleEvents = isAppLifecycleEventTrackingEnabled;
saveConfigurations(config);
navigation.goBack();
};
const saveConfigurations = async (config) => {
await onSdkConfigStateChanged(config);
};
const copyToDeviceClipboard = async () => {
Clipboard.setString(deviceToken);
Prompts.showSnackbar({ text: 'Device token copied to clipboard' });
};
const siteIdRef = useRef();
const apiKeyRef = useRef();
const handleGoToInternalSettingsPress = async () => {
navigation.navigate('InternalSettings');
};
return (
<View style={settingsStyles.container}>
<ScrollView contentContainerStyle={settingsStyles.content}>
<View style={settingsStyles.section}>
<TextField
style={settingsStyles.textInputContainer}
label="Device Token"
value={deviceToken}
contentDesc="Device Token Input"
editable={false}
leadingIconImageSource={require('../../assets/images/paper.png')}
onLeadingIconPress={() => copyToDeviceClipboard()}
/>
</View>
<View style={settingsStyles.section}>
<Text style={settingsStyles.sectionLabel}>Config Settings</Text>
<TextField
style={settingsStyles.textInputContainer}
label="Site Id"
value={siteId}
contentDesc="Site ID Input"
onChangeText={(text) => setSiteId(text)}
textInputRef={siteIdRef}
getNextTextInput={() => ({ ref: apiKeyRef, value: cdpApiKey })}
textInputProps={{
autoCapitalize: 'none',
keyboardType: 'default',
}}
/>
<TextField
style={settingsStyles.textInputContainer}
label="CDP API Key"
value={cdpApiKey}
contentDesc="CDP Api Key Input"
onChangeText={(text) => setCdpApiKey(text)}
textInputRef={apiKeyRef}
textInputProps={{
autoCapitalize: 'none',
keyboardType: 'default',
}}
/>
</View>
<View style={settingsStyles.section}>
<SwitchField
style={settingsStyles.switchRow}
label="Track Screens"
onValueChange={() => setTrackScreensEnabled(!isTrackScreensEnabled)}
value={isTrackScreensEnabled}
contentDesc="Track Screens Toggle"
/>
<SwitchField
style={settingsStyles.switchRow}
label="App Lifecycle Events Tracking"
onValueChange={() =>
setAppLifecycleEventTrackingEnabled(
!isAppLifecycleEventTrackingEnabled
)
}
value={isAppLifecycleEventTrackingEnabled}
contentDesc="App Lifecycle Events Tracking Toggle"
/>
</View>
<SwitchField
style={settingsStyles.switchRow}
label="Auto track device attributes"
onValueChange={() =>
setTrackDeviceAttributesEnabled(!isTrackDeviceAttributesEnabled)
}
value={isTrackDeviceAttributesEnabled}
contentDesc="Auto track device attributes Toggle"
/>
<View style={settingsStyles.section}>
<Text style={settingsStyles.sectionLabel}>Development</Text>
<SwitchField
style={settingsStyles.switchRow}
label="Debug Mode"
onValueChange={() => setDebugModeEnabled(!isDebugModeEnabled)}
value={isDebugModeEnabled}
contentDesc="Debug Mode Toggle"
/>
</View>
<View style={settingsStyles.buttonContainer}>
<FilledButton
style={settingsStyles.saveButton}
onPress={handleSavePress}
text="Save"
contentDesc="Save Settings Button"
textStyle={settingsStyles.saveButtonText}
/>
<FilledButton
style={settingsStyles.saveButton}
onPress={handleGoToInternalSettingsPress}
text="Internal Settings"
contentDesc="Internal Settings Button"
textStyle={settingsStyles.saveButtonText}
/>
<TextButton
style={settingsStyles.restoreDefaultsButton}
onPress={handleRestoreDefaultsPress}
text="Restore Defaults"
contentDesc="Restore Default Settings Button"
/>
<Caption style={settingsStyles.note}>
Note: You must restart the app to apply these settings
</Caption>
</View>
</ScrollView>
</View>
);
};
export default Settings;