-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreducer.js
65 lines (61 loc) · 1.83 KB
/
reducer.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
import { createSelector } from 'reselect';
import createShallowArrayCompareSelector from './utils/createShallowArrayCompareSelector';
import { INIT_COMPONENT, SET_INIT_MODE } from './actions/actionTypes';
import extractValuesForProps from './utils/extractValuesForProps';
import { MODE_PREPARE } from './initMode';
/**
* Reducer function that manages the state for `react-redux-component-init`. This reducer should
* be included using Redux's `combineReducers()` under the `init` key in the root of
* the store state.
*/
export default (state = {
mode: MODE_PREPARE,
prepared: {},
selfInit: {},
}, action) => {
switch (action.type) {
case SET_INIT_MODE:
return {
...state,
mode: action.payload,
};
case INIT_COMPONENT:
if (action.payload.isPrepare) {
return {
...state,
prepared: {
...state.prepared,
[action.payload.prepareKey]: action.payload.complete,
},
};
}
return {
...state,
selfInit: {
...state.selfInit,
[action.payload.prepareKey]: action.payload.complete,
},
};
default:
return state;
}
};
const createComponentInitValuesSelector = ({ componentId, initProps, options: { getPrepareKey } }) =>
createShallowArrayCompareSelector(
(state, props) => extractValuesForProps(props, initProps),
initValues => ({
prepareKey: getPrepareKey(componentId, initValues),
initValues,
}),
);
export const createComponentInitStateSelector = initConfig => createSelector(
createComponentInitValuesSelector(initConfig),
state => state.selfInit,
state => state.prepared,
({ prepareKey, initValues }, selfInit, prepared) => ({
prepareKey,
initValues,
selfInitState: selfInit[prepareKey],
isPrepared: !!prepared[prepareKey],
}),
);