-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducers.js
96 lines (82 loc) · 3.02 KB
/
reducers.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
import {combineReducers} from 'redux'
import {uniqBy, without} from 'lodash'
const editorData = () => [
{
site: {value: 1, label: '1) lion.com'},
project: {value: 1, label: '1) lorem ipsum'},
status: {value: 1, label: '1) pending'}
}, {
site: {value: 2, label: '2) dragonfly.com'},
project: {value: 2, label: '2) dolor sit amet'},
status: {value: 2, label: '2) done'}
}, {
site: {value: 3, label: '3) zebra.com'},
project: {value: 3, label: '3) maximus sapien'},
status: {value: 1, label: '1) pending'}
}, {
site: {value: 4, label: '4) monkey.com'},
project: {value: 4, label: '4) dapibus turpis'},
status: {value: 2, label: '2) done'}
}, {
site: {value: 5, label: '5) parrot.com'},
project: {value: 5, label: '5) morbi tristique'},
status: {value: 1, label: '1) pending'}
}, {
site: {value: 3, label: '3) zebra.com'},
project: {value: 5, label: '5) morbi tristique'},
status: {value: 1, label: '1) pending'}
}
]
const testData = () => [
{site: 1, project: 2, status: 1},
{site: 2, project: 2, status: 2},
{site: 2, project: 3, status: 1},
{site: 4, project: 4, status: 2},
{site: 5, project: 5, status: 1},
{site: 3, project: 5, status: 3}
]
export const inclusionRule = (state = {site: null, project: null}, action) => {
return {
...state,
site: action.rule.site === undefined ? state.site : action.rule.site,
project: action.rule.project === undefined ? state.project : action.rule.project
}
}
export const inclusionRules = (state = [{site: null, project: null}], action) => {
switch (action.type) {
case 'INS_INCLUSION_RULE':
return [
...state.slice(0, action.index + 1),
{site: action.rule.site, project: action.rule.project},
...state.slice(action.index + 1)
]
case 'UPD_INCLUSION_RULE':
return [
...state.slice(0, action.index),
inclusionRule(state[action.index], action),
...state.slice(action.index + 1)
]
case 'REM_INCLUSION_RULE':
return state.filter((_, i) => i !== action.index)
default:
return state
}
}
export const exclusionRule = (state = null, action) => {
switch (action.type) {
case 'UPD_EXCLUSION_RULE':
return action.rule.status
default:
return state
}
}
export default combineReducers({editorData, inclusionRules, exclusionRule, testData})
export const getInclusionRules = (state) => state.inclusionRules
export const getSites = (state) => uniqBy(state.editorData.map(entry => entry.site), 'value')
export const getProjects = (state) => uniqBy(state.editorData.map(entry => entry.project), 'value')
export const getStatuses = (state) => uniqBy(state.editorData.map(entry => entry.status), 'value')
export const getExclusionRule = (state) => state.exclusionRule
export const isRuleFulfilled = (state) => state.testData
.filter(entry => state.exclusionRule && entry.status !== state.exclusionRule)
.some(entry => state.inclusionRules.some(rule =>
entry.site === rule.site && entry.project === rule.project))