|
1
|
|
|
/* |
|
2
|
|
|
* central function to retrieve state from reducer |
|
3
|
|
|
* used inside of mapStateToProps by grid and other plugins |
|
4
|
|
|
* @returns {object} state |
|
5
|
|
|
|
|
6
|
|
|
* if a dynamic reducerKey is passed, it will favor that key |
|
7
|
|
|
* over the build in grid keys |
|
8
|
|
|
|
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
export const stateGetter = (state, props, key, entry) => { |
|
12
|
|
|
|
|
13
|
|
|
if (props && props.reducerKeys) { |
|
14
|
|
|
if (typeof props.reducerKeys === 'string') { |
|
15
|
|
|
const nestedInImmutable = typeof state.get === 'function'; |
|
16
|
|
|
const nestedState = nestedInImmutable |
|
17
|
|
|
? state.get(props.reducerKeys) |
|
18
|
|
|
: state[props.reducerKeys]; |
|
19
|
|
|
return get(nestedState , key, entry); |
|
20
|
|
|
} |
|
21
|
|
|
else if (typeof props.reducerKeys === 'object' |
|
22
|
|
|
&& Object.keys(props.reducerKeys).length > 0 |
|
23
|
|
|
&& props.reducerKeys[key]) { |
|
24
|
|
|
|
|
25
|
|
|
const dynamicKey = props.reducerKeys[key]; |
|
26
|
|
|
return get(state, dynamicKey, entry); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
const val = get(state, key, entry); |
|
31
|
|
|
|
|
32
|
|
|
if (val) { |
|
33
|
|
|
return val; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return null; |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
export const get = (state, key, entry) => { |
|
40
|
|
|
|
|
41
|
|
|
if (!state) { |
|
42
|
|
|
return null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
const isImmutable = typeof state.get === 'function'; |
|
46
|
|
|
const stateItem = isImmutable |
|
47
|
|
|
? state.get(key) |
|
48
|
|
|
: state[key]; |
|
49
|
|
|
|
|
50
|
|
|
if (!stateItem) { |
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return stateItem.get(entry); |
|
55
|
|
|
}; |
|
56
|
|
|
|