/
AppSwitch.js
134 lines (120 loc) · 4.86 KB
/
AppSwitch.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
// @flow
import React, { useEffect } from 'react'
import { AsyncStorage } from 'react-native'
import { SceneView } from '@react-navigation/core'
import some from 'lodash/some'
import { DESTINATION_PATH } from '../../lib/constants/localStorage'
import logger from '../../lib/logger/pino-logger'
import API from '../../lib/API/api'
import SimpleStore from '../../lib/undux/SimpleStore'
import GDStore from '../../lib/undux/GDStore'
import { updateAll as updateWalletStatus } from '../../lib/undux/utils/account'
import { checkAuthStatus as getLoginState } from '../../lib/login/checkAuthStatus'
type LoadingProps = {
navigation: any,
descriptors: any,
}
const log = logger.child({ from: 'AppSwitch' })
/**
* The main app route rendering component. Here we decide where to go depending on the user's credentials status
*/
const AppSwitch = (props: LoadingProps) => {
const store = SimpleStore.useStore()
const gdstore = GDStore.useStore()
const { router, state } = props.navigation
/*
Check if user is incoming with a URL with action details, such as payment link or email confirmation
*/
const getParams = async () => {
// const navInfo = router.getPathAndParamsForState(state)
const destinationPath = await AsyncStorage.getItem(DESTINATION_PATH).then(JSON.parse)
AsyncStorage.removeItem(DESTINATION_PATH)
log.debug('getParams', { destinationPath, router, state })
if (destinationPath) {
const app = router.getActionForPathAndParams(destinationPath.path)
const destRoute = actions => (some(actions, 'action') ? destRoute(actions.action) : actions.action)
const destData = { ...destRoute(app), params: destinationPath.params }
return destData
}
return undefined
}
/*
If a user has a saved destination path from before logging in or from inside-app (receipt view?)
He won't be redirected in checkAuthStatus since it is called on didmount effect and won't happen after
user completes signup and becomes loggedin which just updates this component
*/
const navigateToUrlAction = async () => {
log.info('didUpdate')
let destDetails = await getParams()
//once user logs in we can redirect him to saved destinationpath
if (destDetails) {
log.debug('destinationPath found:', destDetails)
return props.navigation.navigate(destDetails)
}
}
/**
* Check's users' current auth status
* @returns {Promise<void>}
*/
const initialize = async () => {
//after dynamic routes update, if user arrived here, then he is already loggedin
//initialize the citizen status and wallet status
const { isLoggedInCitizen, isLoggedIn } = await Promise.all([getLoginState(), updateWalletStatus(gdstore)]).then(
([authResult, _]) => authResult
)
gdstore.set('isLoggedIn')(isLoggedIn)
gdstore.set('isLoggedInCitizen')(isLoggedInCitizen)
isLoggedInCitizen ? API.verifyTopWallet() : Promise.resolve()
// if (isLoggedIn) {
// if (destDetails) {
// props.navigation.navigate(destDetails)
// return AsyncStorage.removeItem(DESTINATION_PATH)
// } else props.navigation.navigate('AppNavigation')
// } else {
// const { jwt } = credsOrError
// if (jwt) {
// log.debug('New account, not verified, or did not finish signup', jwt)
// //for new accounts check if link is email validation if so
// //redirect to continue signup flow
// if (destDetails) {
// log.debug('destinationPath details found', destDetails)
// if (destDetails.params.validation) {
// log.debug('destinationPath redirecting to email validation')
// props.navigation.navigate(destDetails)
// return
// }
// log.debug('destinationPath saving details')
// //for non loggedin users, store non email validation params to the destinationPath for later
// //to be used once signed in
// const destinationPath = JSON.stringify(destDetails)
// AsyncStorage.setItem(DESTINATION_PATH, destinationPath)
// }
// props.navigation.navigate('Auth')
// } else {
// // TODO: handle other statuses (4xx, 5xx), consider exponential backoff
// log.error('Failed to sign in', credsOrError)
// props.navigation.navigate('Auth')
// }
// }
}
const init = async () => {
// store.set('loadingIndicator')({ loading: true })
await initialize()
store.set('loadingIndicator')({ loading: false })
}
useEffect(() => {
init()
navigateToUrlAction()
}, [])
// useEffect(() => {
// })
const { descriptors, navigation } = props
const activeKey = navigation.state.routes[navigation.state.index].key
const descriptor = descriptors[activeKey]
return (
<React.Fragment>
<SceneView navigation={descriptor.navigation} component={descriptor.getComponent()} />
</React.Fragment>
)
}
export default AppSwitch