-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (27 loc) · 1.06 KB
/
index.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
function createTypesAndActions(namespace, actionTypes) {
const types = actionTypes.reduce((typeDict, type) => {
typeDict[type] = `${namespace}/${type}`;
return typeDict;
}, {});
const actions = actionTypes.reduce((actionDict, type) => {
actionDict[type] = payload => ({ type: types[type], payload });
return actionDict;
}, {});
return { types, actions };
}
function createAsyncTypesAndActions(namespace, types, options = { phases: [ 'request', 'success', 'failure' ] }) {
const { phases } = options;
return types.reduce((dict, type) => {
const { types, actions } = dict;
types[type] = phases.reduce((phaseDict, phase) => {
phaseDict[phase] = `${namespace}/${type}_${phase.toUpperCase()}`;
return phaseDict;
}, {});
actions[type] = phases.reduce((phaseDict, phase) => {
phaseDict[phase] = payload => ({ type: types[type][phase], payload });
return phaseDict;
}, {});
return { types, actions };
}, { types: {}, actions: {} });
}
module.exports = { createTypesAndActions, createAsyncTypesAndActions };