This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy patherror.js
64 lines (53 loc) · 1.68 KB
/
error.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
const defaultState = {}
// returns the list of errors with those from this subsection removed
const clearErrors = (sectionErrors, action) => {
return sectionErrors.filter(
x => x.section === action.property && x.subsection !== action.subsection
)
}
const errorMatches = (err1, err2) => {
return err1.section === err2.section && err1.code === err2.code
}
const getErrorIndex = (sectionErrors, err) => {
return sectionErrors.findIndex(x => errorMatches(x, err))
}
// modifies the sectionErrors
const updateError = (sectionErrors, err) => {
const idx = getErrorIndex(sectionErrors, err)
if (idx === -1) {
sectionErrors.push(err)
} else {
sectionErrors[idx] = err
}
}
// modifies the sectionErrors
const updateErrors = (sectionErrors, action) => {
for (const err of action.values) {
updateError(sectionErrors, err)
}
}
const errorReducer = function(sectionName) {
return function(state = defaultState, action) {
// Check that section matches intended section reducer. This is to prevent
// merging of everything every time an action is dispatched. We only
// perform for the relevant section
if (action.section === sectionName) {
// Don't get confused now...
// - section = 'Errors'
// - property = section (i.e. 'identification')
// - subsection = subsection (i.e. 'contacts')
let sectionErrors = state[action.property] || []
if (action.clear === true) {
sectionErrors = clearErrors(sectionErrors, action)
} else {
updateErrors(sectionErrors, action)
}
return {
...state,
[action.property]: sectionErrors
}
}
return state
}
}
export default errorReducer