forked from OpenBazaar/haven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnboardingWrapper.js
160 lines (140 loc) · 4.81 KB
/
OnboardingWrapper.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
import React, { PureComponent } from 'react';
import { Text, DeviceEventEmitter, NativeEventEmitter, NativeModules, Platform, Alert } from 'react-native';
import SplashScreen from 'react-native-splash-screen';
import { connect } from 'react-redux';
import Countly from 'countly-sdk-react-native';
import { getConfig, createProfile, createSettings } from './api/onboarding';
import { getProfile } from './api/profile';
import { getSettings } from './api/settings';
import { setOnboardingStatus, initializeLogin, triggerReviewPrompt, appInstalled, setGuest } from './reducers/appstate';
import { setProfile, updateAcceptedCoins } from './reducers/profile';
import { setSettings } from './reducers/settings';
import AppNavigator from './AppNavigator';
import { OnboardingContainer } from './routes';
import { changeTrackingStatus } from './utils/EventTracker';
import { COUNTLY_ROOT_URL, COUNTLY_APP_KEY } from './CountlyConfig';
import { COINS } from './utils/coins';
const { ModuleWithEmitter } = NativeModules;
const iOSeventEmitter = new NativeEventEmitter(ModuleWithEmitter);
class OnboardingWrapper extends PureComponent {
constructor(props) {
super(props);
Countly.init(COUNTLY_ROOT_URL, COUNTLY_APP_KEY).then(() => {
changeTrackingStatus(props.isTrackingEvent);
}).catch((err) => {
// console.error('Countly failed to begin!', err);
});
}
async componentWillMount() {
const { setOnboardingStatus } = this.props;
try {
await getConfig();
this.handleServerStarted();
} catch (err) {
if (Platform.OS === 'ios') {
iOSeventEmitter.addListener('onServerStarted', this.handleServerStarted);
} else {
DeviceEventEmitter.addListener('onServerStarted', this.handleServerStarted);
}
setOnboardingStatus('offline');
}
}
componentDidMount() {
const { triggerReviewPrompt, isAppInstalled } = this.props;
if (isAppInstalled) {
triggerReviewPrompt();
}
}
handleServerStarted = async () => {
if (Platform.OS === 'ios') {
iOSeventEmitter.removeListener('onServerStarted', this.handleServerStarted);
} else {
DeviceEventEmitter.removeListener('onServerStarted', this.handleServerStarted);
}
const {
guest,
setSettings,
setProfile,
setOnboardingStatus,
initializeLogin,
isAppInstalled,
setGuest,
updateAcceptedCoins,
currencies,
} = this.props;
try {
const profile = await getProfile();
const settings = await getSettings();
if (profile && settings) {
if (!isAppInstalled) {
appInstalled();
}
SplashScreen.hide();
setOnboardingStatus('loggedIn');
if (!currencies || !currencies.length) {
updateAcceptedCoins({ coins: Object.keys(COINS) });
}
initializeLogin();
if (!guest) {
setGuest({
name: profile.name,
country: settings.country,
localCurrency: settings.localCurrency,
});
}
return;
}
SplashScreen.hide();
if (guest) {
const { country, localCurrency, name } = guest;
const settings = await createSettings(country, localCurrency);
setSettings(settings);
const profile = await createProfile(name || 'Anonymous');
setProfile(profile);
setOnboardingStatus('loggedIn');
updateAcceptedCoins({ coins: Object.keys(COINS) });
console.warn('initialize called from guest');
initializeLogin();
} else {
setOnboardingStatus('onboarding');
}
} catch (err) {
// TODO: handle errors and show them in the UI. Currently an error in loading the settings
// data results in a red screen of death (test by making the api/settings url invalid).
// console.error(err);
// console.log('initial onboarding call failure');
}
};
render() {
const { status, guest } = this.props;
if (status === 'loading') {
return <Text />;
} else if ((status === 'offline' && guest) || status === 'loggedIn') {
return <AppNavigator />;
} else if ((status === 'offline' && !guest) || status === 'onboarding') {
return <OnboardingContainer />;
} else {
return <Text>Unknown</Text>;
}
}
}
const mapStateToProps = state => ({
status: state.appstate.onboardingStatus,
isTrackingEvent: state.appstate.isTrackingEvent,
guest: state.appstate.guest,
username: state.appstate.username,
password: state.appstate.password,
isAppInstalled: state.appstate.appInstalled,
currencies: state.profile.data.currencies,
});
const mapDispatchToProps = {
setProfile,
updateAcceptedCoins,
setSettings,
setOnboardingStatus,
initializeLogin,
triggerReviewPrompt,
appInstalled,
setGuest,
};
export default connect(mapStateToProps, mapDispatchToProps)(OnboardingWrapper);