|
| 1 | +const validAttributes = [ |
| 2 | + 'visible', |
| 3 | + 'disabled', |
| 4 | + 'hidden', |
| 5 | + 'enabled', |
| 6 | + 'icon', |
| 7 | + 'help', |
| 8 | + 'colorBar', |
| 9 | + 'required', |
| 10 | + 'max', |
| 11 | + 'min', |
| 12 | +]; |
| 13 | + |
| 14 | +//enabled and hidden are not used internally, but accepted as input from the conditions object |
| 15 | +const inversedTypes = { |
| 16 | + enabled: 'disabled', |
| 17 | + hidden: 'visible', |
| 18 | +}; |
| 19 | + |
1 | 20 | const uiStateReducer = (state, action) => {
|
2 | 21 | switch (action.type) {
|
3 |
| - case 'conditionResult': { |
4 |
| - const { |
5 |
| - source, |
6 |
| - uiState: {add, remove}, |
7 |
| - } = action; |
8 |
| - |
9 |
| - const validAttributes = [ |
10 |
| - 'visible', |
11 |
| - 'disabled', |
12 |
| - 'hidden', |
13 |
| - 'enabled', |
14 |
| - 'icon', |
15 |
| - 'help', |
16 |
| - 'colorBar', |
17 |
| - 'required', |
18 |
| - 'max', |
19 |
| - 'min', |
20 |
| - ]; |
21 |
| - |
22 |
| - //enabled and hidden are not used internally, but accepted as input from the conditions object |
23 |
| - const inversedTypes = { |
24 |
| - enabled: 'disabled', |
25 |
| - hidden: 'visible', |
26 |
| - }; |
27 |
| - |
28 |
| - const removeItem = state => { |
29 |
| - if (!remove) return; |
30 |
| - |
31 |
| - Object.entries(remove).forEach(([key, item]) => { |
32 |
| - //If the field/section doesn't exist, we don't need to do anymore |
33 |
| - if (!state[key]) return; |
34 |
| - |
35 |
| - validAttributes.forEach(type => { |
36 |
| - //Don't process uiTypes if they don't exist in the dispatched message |
37 |
| - if (item[type] === undefined) return; |
38 |
| - |
39 |
| - const inversedType = inversedTypes[type]; |
40 |
| - type = inversedType || type; |
41 |
| - |
42 |
| - if (remove[key][type]) { |
43 |
| - const index = state[key][type].findIndex(item => item.source === source); |
44 |
| - if (index !== -1) state[key][type].splice(index, 1); |
45 |
| - |
46 |
| - //If this was the last item of this type, remove the type |
47 |
| - if (state[key][type].length === 0) delete state[key][type]; |
| 22 | + case 'addUIState': { |
| 23 | + const {source, uiState} = action; |
| 24 | + |
| 25 | + Object.entries(uiState).forEach(([key, item]) => { |
| 26 | + //Create the item object for this item if it doesn't exist |
| 27 | + if (!state.fields[key]) state.fields[key] = {}; |
| 28 | + |
| 29 | + validAttributes.forEach(type => { |
| 30 | + //Don't add uiTypes if they don't exist in the dispatched message |
| 31 | + if (item[type] === undefined) return; |
| 32 | + |
| 33 | + //Handle inversed types (disabled/enabled, visible/hidden) |
| 34 | + const inversedType = inversedTypes[type]; |
| 35 | + const value = inversedType ? !item[type] : item[type]; |
| 36 | + type = inversedType || type; |
| 37 | + |
| 38 | + if (!state.fields[key][type]) { |
| 39 | + //If this type doesn't exists for this item, we create a new array with only this source. No need to search fot the source |
| 40 | + state.fields[key][type] = [{source, value}]; |
| 41 | + } else { |
| 42 | + const index = state.fields[key][type].findIndex(item => item.source === source); |
| 43 | + if (index !== -1) { |
| 44 | + //If this type for this item from this source existed, update the state (value could change if condition went from "then" to "else") |
| 45 | + state.fields[key][type][index].value = value; |
| 46 | + } else { |
| 47 | + //Otherwise, add the state from this source at the begining of the array (i.e. this will supress result from other sources) |
| 48 | + state.fields[key][type].unshift({source, value}); |
48 | 49 | }
|
49 |
| - }); |
50 |
| - |
51 |
| - //If no more uiStateType keys exists for this field, remove the field |
52 |
| - if (Object.keys(state[key]).length === 0) delete state[key]; |
| 50 | + } |
53 | 51 | });
|
54 |
| - }; |
55 |
| - |
56 |
| - const addItem = state => { |
57 |
| - if (!add) return; |
58 | 52 |
|
59 |
| - Object.entries(add).forEach(([key, item]) => { |
60 |
| - //Create the item object for this item if it doesn't exist |
61 |
| - if (!state[key]) state[key] = {}; |
| 53 | + // Set-instructions are ephemeral and goes into a separate list which is emptied when processed |
| 54 | + if (item.set) { |
| 55 | + state.setFieldValues = {...state.setFieldValues, [key]: item.set}; |
| 56 | + } |
| 57 | + }); |
| 58 | + return {...state}; |
| 59 | + } |
62 | 60 |
|
63 |
| - validAttributes.forEach(type => { |
64 |
| - //Don't add uiTypes if they don't exist in the dispatched message |
65 |
| - if (item[type] === undefined) return; |
| 61 | + case 'removeUIState': { |
| 62 | + const {source, uiState} = action; |
66 | 63 |
|
67 |
| - //Handle inversed types (disabled/enabled, visible/hidden) |
68 |
| - const inversedType = inversedTypes[type]; |
69 |
| - const value = inversedType ? !item[type] : item[type]; |
70 |
| - type = inversedType || type; |
| 64 | + Object.entries(uiState).forEach(([key, item]) => { |
| 65 | + //If the field/section doesn't exist, we don't need to do anymore |
| 66 | + if (!state.fields[key]) return; |
71 | 67 |
|
72 |
| - if (!state[key][type]) { |
73 |
| - //If this type doesn't exists for this item, we create a new array with only this source. No need to search fot the source |
74 |
| - state[key][type] = [{source, value}]; |
75 |
| - } else { |
76 |
| - const index = state[key][type].findIndex(item => item.source === source); |
77 |
| - if (index !== -1) { |
78 |
| - //If this type for this item from this source existed, update the state (could change if condition went from "then" to "else") |
79 |
| - state[key][type][index].value = value; |
80 |
| - } else { |
81 |
| - //Otherwise, add the state from this source at the begining of the array (i.e. this will supress result from other sources) |
82 |
| - state[key][type].unshift({source, value}); |
83 |
| - } |
84 |
| - } |
85 |
| - }); |
| 68 | + Object.entries(item).forEach(([type, value]) => { |
| 69 | + if (!state.fields[key][type]) return; |
86 | 70 |
|
87 |
| - //Set-instructions are ephemeral and goes into a separate list which is emptied when processed |
88 |
| - if (item.set) { |
89 |
| - state.setFieldValues = {...state.fileldValues, [key]: item.set}; |
90 |
| - } |
| 71 | + const index = state.fields[key][type].findIndex(item => item.source === source); |
| 72 | + if (index !== -1) state.fields[key][type].splice(index, 1); |
| 73 | + if (state.fields[key][type].length === 0) delete state.fields[key][type]; |
91 | 74 | });
|
92 |
| - }; |
93 |
| - |
94 |
| - let mutatedState = state; |
95 |
| - if (remove) { |
96 |
| - removeItem(mutatedState); |
97 |
| - } |
98 |
| - //If uiStates should be added, go through all add fields and all possible types for these fields |
99 |
| - if (add) { |
100 |
| - addItem(mutatedState); |
101 |
| - } |
102 |
| - console.log(mutatedState); |
103 |
| - return {...mutatedState}; |
| 75 | + |
| 76 | + //If no more uiStateType keys exists for this field, remove the field |
| 77 | + if (Object.keys(state.fields[key]).length === 0) delete state.fields[key]; |
| 78 | + }); |
| 79 | + |
| 80 | + return {...state}; |
104 | 81 | }
|
| 82 | + |
105 | 83 | case 'fieldValuesUpdated': {
|
106 | 84 | return {...state, setFieldValues: {}};
|
107 | 85 | }
|
| 86 | + |
| 87 | + default: |
| 88 | + return state; |
108 | 89 | }
|
109 | 90 | };
|
110 | 91 |
|
|
0 commit comments