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 pathvalidation.js
More file actions
137 lines (110 loc) · 3.48 KB
/
Copy pathvalidation.js
File metadata and controls
137 lines (110 loc) · 3.48 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
/* eslint import/prefer-default-export: 0 */
import { createSelector } from 'reselect'
import { REVIEW_AND_SUBMIT_SUBMIT, REVIEW_AND_SUBMIT_PRINT } from 'constants/sections'
import { formIsLocked } from 'validators'
import { sectionIsValid } from 'helpers/validation'
import { reduceSubsections } from 'helpers/navigation'
import { nestedFormSectionsSelector } from 'selectors/navigation'
/**
* Recursive function that loops through sections and their subsections, checks
* section validity from Redux, and adds the isValid boolean to the section
* object.
*/
const getFormSectionStatuses = (sections = [], store = '', state = {}) => {
const { form } = state
const newSections = sections
.filter(s => s.subsections || s.storeKey)
.map((s) => {
if (s.subsections) {
const parentStore = store || s.store
return {
...s,
subsections: getFormSectionStatuses(s.subsections, parentStore, state),
}
}
const { key } = s
const isValid = !!(form[key] && form[key].complete === true)
return {
...s,
isValid,
}
})
return newSections
}
const getFormStatus = (state) => {
const formSections = nestedFormSectionsSelector(state, true)
const formSectionStatuses = getFormSectionStatuses(formSections, '', state)
return formSectionStatuses
}
export const formStatusSelector = createSelector(
getFormStatus,
formSections => ({
formSections,
formIsValid: sectionIsValid(formSections),
})
)
const formHasErrors = (state) => {
const formStatus = formStatusSelector(state)
const { formIsValid } = formStatus
return !formIsValid
}
const getSectionLocked = (state, props) => {
const { section } = props
const formLocked = formIsLocked(state.application)
// Special cases
switch (section.key) {
case REVIEW_AND_SUBMIT_SUBMIT: {
return formLocked || formHasErrors(state)
}
case REVIEW_AND_SUBMIT_PRINT:
return !formLocked
default:
return formLocked
}
}
const getSectionErrors = (state, props) => {
const { application } = state
const { Errors } = application
const { topSection, section } = props
const sectionErrors = topSection
? Errors[topSection]
: Errors[section.name]
if (!sectionErrors) { return [] }
if (!section.subsections) {
return sectionErrors.filter(e => e.subsection === section.name)
}
const flatSections = reduceSubsections(section.subsections)
return sectionErrors.filter(s => (
flatSections.find(i => i.name === s.subsection)
))
}
const getSectionCompleted = (state, props) => {
const { form } = state
const { section } = props
if (section.subsections) {
// Check complete status of each subsection
const flatSections = reduceSubsections(section.subsections)
const sectionsWithData = flatSections.filter(s => !!s.storeKey)
return sectionsWithData.length > 0 && sectionsWithData.every((s) => {
const sectionData = form && form[s.key]
return sectionData && sectionData.complete === true
})
}
const sectionData = form && form[section.key]
if (!sectionData) return false
return sectionData.complete === true
}
export const sectionIsLockedSelector = createSelector(
getSectionLocked,
locked => ({ locked })
)
export const sectionHasErrorsSelector = createSelector(
getSectionErrors,
(errors = []) => ({
errors: errors.some(e => e.valid === false),
})
)
export const sectionIsValidSelector = createSelector(
getSectionCompleted,
completed => ({ completed })
)