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 24
Expand file tree
/
Copy pathform.js
More file actions
148 lines (124 loc) · 4.44 KB
/
Copy pathform.js
File metadata and controls
148 lines (124 loc) · 4.44 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
148
/* eslint import/no-cycle: 0 */
import {
select, call, put, all, takeEvery, takeLatest,
} from 'redux-saga/effects'
import {
HANDLE_SUBSECTION_UPDATE,
VALIDATE_FORM,
} from 'constants/actionTypes'
import {
updateSubsection,
updateSubsectionData,
validateForm,
} from 'actions/FormActions'
import { updateApplication } from 'actions/ApplicationActions'
import { validateSection } from 'helpers/validation'
import sectionKeys from 'helpers/sectionKeys'
import { unschema } from 'schema'
import { env } from 'config'
import { selectApplicantBirthdate, selectMaritalStatus } from 'selectors/data'
import { selectValidUSPassport } from 'selectors/misc'
import { selectForm, selectSubsection, formTypeSelector } from './selectors'
/** LEGACY ACTIONS - store.application */
/** Setting form data on login (this might be replaced) */
export function* updateSectionDataLegacy(name, data) {
try {
yield all(Object.keys(data).map((subsection) => {
const sectionKey = sectionKeys[`${name}.${subsection}`]
const sectionData = unschema(data[subsection])
const updateActions = [put(updateApplication(name, subsection, sectionData))]
if (sectionKey) {
// This sets form data but does not validate
updateActions.push(put(updateSubsectionData(sectionKey, undefined, sectionData)))
}
return all(updateActions)
}))
} catch (e) {
console.warn('failed to update section', name, e)
yield call(env.History().push, '/error')
}
}
export function* setFormData(data) {
try {
const { Metadata = {} } = data
const formType = Metadata.form_type
const formVersion = Metadata.form_version
yield put(updateApplication('Settings', 'formType', formType))
yield put(updateApplication('Settings', 'formVersion', formVersion))
yield all(Object.keys(data)
.map(section => call(updateSectionDataLegacy, section, data[section])))
yield put(validateForm())
} catch (e) {
console.warn('failed to set form data', e)
yield call(env.History().push, '/error')
}
}
/** NEW ACTIONS - store.form */
// This fn currently unused. Will be useful if form starts field-level updates.
export const updateSectionData = (prevData, field, data) => ({
...prevData,
[field]: {
...(prevData && prevData[field]),
...data,
},
})
// Loop through all form sections and re-validate with the existing data
export function* handleValidateForm() {
const formType = yield select(formTypeSelector)
const applicantBirthdate = yield select(selectApplicantBirthdate)
const maritalStatus = yield select(selectMaritalStatus)
const hasValidUSPassport = yield select(selectValidUSPassport)
// pass any x-section form data required to validate here
const validationOptions = {
applicantBirthdate,
maritalStatus,
...hasValidUSPassport,
}
const formData = yield select(selectForm)
yield all(Object.keys(formData)
.map((key) => {
const sectionData = formData[key].data
const errors = validateSection({
key,
data: sectionData,
options: validationOptions,
}, formType)
const newFormSection = {
data: sectionData,
errors: errors === true ? [] : errors,
complete: errors.length === 0 || errors === true,
}
return put(updateSubsection(key, newFormSection))
}))
}
export function* handleSubsectionUpdate({ key, data }) {
const formType = yield select(formTypeSelector)
const applicantBirthdate = yield select(selectApplicantBirthdate)
const maritalStatus = yield select(selectMaritalStatus)
const hasValidUSPassport = yield select(selectValidUSPassport)
const formSection = yield select(selectSubsection, key)
// This because currently, data is updated a whole subsection at a time
// Consider changing to updateSectionData for field-level updates in the future
const newData = { ...formSection.data, ...data }
const errors = yield call(validateSection, {
key,
data: newData,
options: { // pass any x-section form data required to validate here
applicantBirthdate,
maritalStatus,
...hasValidUSPassport,
},
}, formType)
const newFormSection = {
data: newData,
errors: errors === true ? [] : errors,
complete: errors.length === 0 || errors === true,
}
yield put(updateSubsection(key, newFormSection))
}
export function* formWatcher() {
yield all([
takeEvery(HANDLE_SUBSECTION_UPDATE, handleSubsectionUpdate),
takeLatest(VALIDATE_FORM, handleValidateForm),
])
}