This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinitialize.js
More file actions
147 lines (122 loc) · 3.73 KB
/
Copy pathinitialize.js
File metadata and controls
147 lines (122 loc) · 3.73 KB
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
/* eslint import/no-cycle: 0 */
import {
take, put, call, race, spawn,
} from 'redux-saga/effects'
import * as actionTypes from 'constants/actionTypes'
import { STATUS_SUBMITTED } from 'constants/enums/applicationStatuses'
import { fetchForm, fetchStatus } from 'actions/api'
import { updateApplication } from 'actions/ApplicationActions'
import { env } from 'config'
import { setFormData, formWatcher } from 'sagas/form'
import { handleLogout } from 'sagas/session'
export function* loggedOutWatcher() {
const { error } = yield race({
success: take(actionTypes.LOGIN_SUCCESS),
error: take(actionTypes.LOGIN_ERROR),
})
if (error) {
// Restart the watcher for the next login attempt
yield call(loggedOutWatcher)
} else {
yield call(initializeApp) // eslint-disable-line
}
}
export function* loggedInWatcher() {
const { logout } = yield race({
loggedIn: call(formWatcher),
logout: take(actionTypes.LOGOUT),
})
if (logout) {
const { timedOut } = logout
yield call(handleLogout, timedOut)
}
}
/** This is a somewhat generic handler for API fetch failures */
export function* handleFetchError(action) {
const { error } = action
if (error && error.response) {
switch (error.response.status) {
case 401:
case 403:
yield spawn(loggedOutWatcher)
yield call(env.History().push, '/login')
break
default:
yield call(env.History().push, '/error')
}
} else {
console.warn('Unknown error', error)
yield call(env.History().push, '/error')
}
}
export function* handleFetchFormSuccess(data, path) {
const { response } = data
if (response && response.data) {
yield call(setFormData, response.data)
yield call(env.History().replace, path)
} else {
console.warn('Missing response', response)
yield call(env.History().push, '/error')
}
}
/** Initialize app (on page load) */
export function* handleInitError(action) {
yield call(handleFetchError, action)
}
export function* handleInitSuccess(action, path = '/form/identification/intro') {
const { response } = action
if (response && response.data) {
const { Status, Hash } = response.data
// TODO - consolidate these into one action
yield put(updateApplication('Settings', 'status', Status))
yield put(updateApplication('Settings', 'hash', Hash))
// Check to see if the account is locked
const status = response.data
if (status.Status === STATUS_SUBMITTED) {
yield call(env.History().push, '/locked')
return
}
// start watching for logged in events
yield spawn(loggedInWatcher)
// attempt to load /form
yield call(env.History().push, '/loading')
yield put(fetchForm())
// watch for success or failure
const { data, error } = yield race({
data: take(actionTypes.FETCH_FORM_SUCCESS),
error: take(actionTypes.FETCH_FORM_ERROR),
})
if (error) {
yield call(handleFetchError, error)
} else {
yield call(handleFetchFormSuccess, data, path)
}
} else {
console.warn('Missing response', response)
yield call(env.History().push, '/error')
}
}
export function* initializeApp(path) {
// attempt to load /status
yield put(fetchStatus())
// watch for success or failure
const { data, error } = yield race({
data: take(actionTypes.FETCH_STATUS_SUCCESS),
error: take(actionTypes.FETCH_STATUS_ERROR),
})
if (error) {
yield call(handleInitError, error)
} else {
yield call(handleInitSuccess, data, path)
}
}
/**
* Main saga entry point
* kicks off auto-login if logged in
* kicks off login screen if not
*/
export function* initializeAppWatcher() {
const initAction = yield take(actionTypes.INIT_APP)
const { path } = initAction
yield call(initializeApp, path)
}