-
Notifications
You must be signed in to change notification settings - Fork 9
/
namespacedConnect.js
57 lines (45 loc) · 1.34 KB
/
namespacedConnect.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
import { o, mergeDeepRight, binary, compose, map, useWith } from 'ramda';
import { isFunction, isObject } from 'ramda-extension';
import { withWidgetContext } from 'react-union';
import { connect } from 'react-redux';
import { getStateByNamespace } from '@redux-tools/extensible-store';
export const makeMapStateToProps = mapStateToProps => (state, ownProps) => {
if (!mapStateToProps) {
return {};
}
return mapStateToProps(
getStateByNamespace(ownProps.namespace, state),
ownProps,
state
);
};
export const makeMapDispatchToProps = mapDispatchToProps => (
dispatch,
ownProps
) => {
if (!mapDispatchToProps) {
return {};
}
const wrappedDispatch = o(
dispatch,
mergeDeepRight({ meta: { namespace: ownProps.namespace } })
);
if (isFunction(mapDispatchToProps)) {
return mapDispatchToProps(wrappedDispatch, ownProps);
}
if (isObject(mapDispatchToProps)) {
const wrapActionCreator = actionCreator =>
compose(
wrappedDispatch,
actionCreator
);
return map(wrapActionCreator, mapDispatchToProps);
}
throw new TypeError('mapDispatchToProps is not an object or a function');
};
const rawNamespacedConnect = binary(
useWith(connect, [makeMapStateToProps, makeMapDispatchToProps])
);
export const namespacedConnect = (...args) =>
o(withWidgetContext, rawNamespacedConnect(...args));
export default namespacedConnect;