1 | import { bindActionCreators } from 'redux'
|
2 | import parseName from './parseName'
|
3 |
|
4 | export default function wrapMapDispatchToProps(fullName, actionCreators, reducer) {
|
5 | const parsedName = parseName(fullName),
|
6 | wrapActionCreators = {},
|
7 | keys = Object.keys(actionCreators)
|
8 |
|
9 | for (let i = 0; i < keys.length; i++) {
|
10 | let key = keys[i]
|
11 |
|
12 | if (key === 'directFuns')
|
13 | continue
|
14 |
|
15 | let wrapActionCreator = wrapAction(actionCreators[key],
|
16 | reducer, parsedName.fullName, parsedName.name,
|
17 | parsedName.query, parsedName.params)
|
18 | wrapActionCreators[key] = wrapActionCreator
|
19 | }
|
20 |
|
21 | return dispatch => {
|
22 | return {
|
23 | ...bindActionCreators(wrapActionCreators, dispatch),
|
24 | ...((actionCreators.getDirectFuns && actionCreators.getDirectFuns(parsedName)) || {})
|
25 | }
|
26 | }
|
27 | }
|
28 |
|
29 | function wrapAction(actionCreator, reducer, fullName, name, query, params) {
|
30 | return (...args) => {
|
31 | return function () {
|
32 | return {
|
33 | fullName,
|
34 | name,
|
35 | query,
|
36 | params,
|
37 | actionCreator,
|
38 | reducer,
|
39 | args
|
40 | }
|
41 | }
|
42 | }
|
43 | } |
\ | No newline at end of file |