1 | /** |
||
2 | * utility classed used by all reducers to |
||
3 | * update their internal Immutable records |
||
4 | * @param {object} state |
||
5 | * @param {string} stateKey |
||
6 | * @param {object} values |
||
7 | * @param {Immutable.Record} type |
||
8 | * @param {string} operation |
||
9 | **/ |
||
10 | |||
11 | import { fromJS } from 'immutable'; |
||
12 | |||
13 | export const getUpdatedDataRecord = ( |
||
14 | state, stateKey, values = {}, type, operation = 'setIn' |
||
15 | ) => { |
||
16 | if (operation === 'setIn') { |
||
17 | return state[operation]([stateKey], new type(values)); |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
Loading history...
|
|||
18 | } |
||
19 | |||
20 | else if (operation === 'mergeIn') { |
||
21 | const p = state.get(stateKey) |
||
22 | ? state.get(stateKey) |
||
23 | : fromJS({}); |
||
24 | |||
25 | return state[operation]([stateKey], p.merge(values)); |
||
26 | } |
||
27 | |||
28 | throw new Error('Update operation has not been implemented!'); |
||
29 | }; |
||
30 | |||
31 | export default getUpdatedDataRecord; |
||
32 |