-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
218 lines (204 loc) · 6.28 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
import AsyncStorage from '@react-native-async-storage/async-storage'
import { isEmpty, isEqual } from 'lodash'
import React, { PureComponent } from 'react'
import {
View, SafeAreaView, StatusBar, Platform,
} from 'react-native'
import { connect } from 'react-redux'
import auth from '@react-native-firebase/auth'
import Splashscreen from './src/Components/SplashScreen'
import { AppScreens, AuthScreens } from './src/screens'
import {
clearLoginResp, getUserDetails, userSignin,
} from './src/Screens/Auth/actions'
import { COLORS } from './src/Theme'
import { withTheme } from './src/Theme/ThemeProvider'
import Onboarding from './src/UI/Onboarding/Onboarding'
import { Snackbar } from './src/UI/Snackbar'
import { ASYNCSTORAGE_MAP } from './src/utils/consts'
const AppView = Platform.select({
ios: SafeAreaView,
android: View,
})
const ROUTES = {
AUTH: 'auth',
APP: 'app',
ONBOARDING: 'onboarding',
LOADING: 'loading',
}
const MAX_SPLASHSCREEN_TIME = 1000
class App extends PureComponent {
constructor(props) {
super(props)
this.state = {
activeRoute: ROUTES.LOADING,
}
this.initializeTime = new Date()
}
componentDidUpdate(prevProps) {
const {
loginResp,
fetchUserDetails,
user_details = {},
userDetailsError,
userDetailsErrorMsg,
isLoggedIn,
clearLoginResponse,
isFetchingUser,
} = this.props
const { activeRoute } = this.state
const { uid } = user_details
if (!isEqual(prevProps.loginResp, loginResp) && loginResp && !isFetchingUser) {
auth().onAuthStateChanged(user => fetchUserDetails(user))
} else if (loginResp && !uid && !isFetchingUser && !userDetailsError) {
auth().onAuthStateChanged(user => fetchUserDetails(user))
}
// user details came check for terms
if (prevProps.user_details.uid !== uid && isLoggedIn && uid) {
if (activeRoute !== ROUTES.APP) {
AsyncStorage.setItem(ASYNCSTORAGE_MAP.AUTH_TOKEN, loginResp)
this.compareTimeAndUpdate(
{ activeRoute: ROUTES.APP }, this.initializeTime, MAX_SPLASHSCREEN_TIME,
)
// this.setState({ activeRoute: ROUTES.APP })
} else {
AsyncStorage.setItem(ASYNCSTORAGE_MAP.AUTH_TOKEN, loginResp)
}
} else if (prevProps.userDetailsError !== userDetailsError && userDetailsError) {
// log out on auth error
if (userDetailsErrorMsg === 'auth' || !uid) {
clearLoginResponse()
}
}
}
async componentDidMount() {
// AsyncStorage.removeItem(ASYNCSTORAGE_MAP.SHOW_ONBOARDING)
// AsyncStorage.removeItem(ASYNCSTORAGE_MAP.AUTH_TOKEN)
this.showOnboarding = await AsyncStorage.getItem(
ASYNCSTORAGE_MAP.SHOW_ONBOARDING,
)
if (this.showOnboarding !== 'false') {
this.compareTimeAndUpdate(
{ activeRoute: ROUTES.ONBOARDING },
this.initializeTime,
MAX_SPLASHSCREEN_TIME,
)
// this.setState({ activeRoute: ROUTES.ONBOARDING })
} else {
this.checkLogin()
}
}
compareTimeAndUpdate = (
newState = {},
compareTime = new Date(),
maxTime = 0,
) => {
const timeDiff = Math.abs(
new Date().getMilliseconds() - compareTime.getMilliseconds(),
)
if (timeDiff > maxTime) {
this.setState(newState)
} else {
this.stateUpdateTimer = setTimeout(() => {
this.setState(newState)
}, Math.abs(maxTime - timeDiff))
}
};
checkLogin = async () => {
const { signIn } = this.props
try {
const value = await AsyncStorage.getItem(ASYNCSTORAGE_MAP.AUTH_TOKEN) // user_details
if (value !== null && !isEmpty(value)) {
signIn(value)
} else {
this.compareTimeAndUpdate(
{ activeRoute: ROUTES.AUTH },
this.initializeTime,
MAX_SPLASHSCREEN_TIME,
)
// this.setState({ activeRoute: ROUTES.AUTH })
}
} catch (e) {
this.compareTimeAndUpdate(
{ activeRoute: ROUTES.AUTH },
this.initializeTime,
MAX_SPLASHSCREEN_TIME,
)
// this.setState({ activeRoute: ROUTES.AUTH })
}
};
markOnboardingComplete = () => {
this.showOnboarding = 'false'
AsyncStorage.setItem(ASYNCSTORAGE_MAP.SHOW_ONBOARDING, 'false')
this.checkLogin()
};
componentWillUnmount() {
if (this.updateAlertTimer) {
clearTimeout(this.updateAlertTimer)
}
if (this.stateUpdateTimer) {
clearTimeout(this.stateUpdateTimer)
}
}
render() {
const { activeRoute } = this.state
const { theme, isDark } = this.props
const appViewStyle = { flex: 1, backgroundColor: theme.bgPrimary }
const barStyle = isDark ? 'light-content' : 'dark-content'
if (activeRoute === ROUTES.AUTH) {
return (
<View style={appViewStyle}>
<StatusBar backgroundColor={COLORS.WHITE} barStyle="light-content" />
{AuthScreens()}
<Snackbar />
</View>
)
}
if (activeRoute === ROUTES.APP) {
return (
<AppView style={appViewStyle}>
<StatusBar backgroundColor={theme.bgPrimary} barStyle={barStyle} />
{AppScreens()}
<Snackbar />
</AppView>
)
}
if (activeRoute === ROUTES.ONBOARDING) {
return (
<View style={appViewStyle}>
<StatusBar backgroundColor={COLORS.WHITE} barStyle="light-content" />
<Onboarding onFinish={this.markOnboardingComplete} />
<Snackbar />
</View>
)
}
return (
<>
<View style={appViewStyle}>
<StatusBar backgroundColor={COLORS.WHITE} barStyle="light-content" />
<Splashscreen />
<Snackbar />
</View>
</>
)
}
}
const mapStateToProps = (state) => {
return {
// appLoading: state.auth.appLoading,
loginResp: state.auth.loginResp,
user_details: state.auth.user_details,
userDetailsError: state.auth.userDetailsError,
userDetailsErrorMsg: state.auth.userDetailsErrorMsg,
isLoggedIn: state.auth.isLoggedIn,
isFetchingUser: state.auth.isFetchingUser,
}
}
const mapDispatchToProps = (dispatch) => {
return {
fetchUserDetails: params => dispatch(getUserDetails(params)),
signIn: loginResp => dispatch(userSignin(loginResp)),
clearLoginResponse: () => dispatch(clearLoginResp()),
}
}
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(undefined)(App))