Skip to content
This repository was archived by the owner on Jun 23, 2023. It is now read-only.

Commit 996a014

Browse files
committed
Update connect to accept a function or object.
1 parent 63f9b4a commit 996a014

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

app/util/connect.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
import {pipe} from 'wasmuth'
2-
import withStateFn from '/util/withState'
3-
import withRequestFn from '/util/withRequest'
2+
import withState from '/util/withState'
3+
import withRequests from '/util/withRequests'
44

5-
export const connect = ({withState, withRequest}) =>
6-
pipe.apply(this, [
7-
...!!withRequest && [withRequestFn(withRequest)],
8-
...!!withState && [withStateFn(withState)]
9-
])
5+
/**
6+
* It is very common to need a component that
7+
* - uses (and rerenders based on) some values in state
8+
* - gets some data from an api
9+
*
10+
* This HOC is meant to accomplish these two things with
11+
* a minimum amount of boilerplate
12+
*
13+
* The mapper argument can either be:
14+
* 1. a function like `(state, props) => ({withState: {...}, withRequests: {...}})`
15+
* 2. or an object like `{withState: (state, props) => ({...}), withRequests: (state, props) => ({...})}`
16+
*
17+
* See withState and withRequests for instructions on what should be inside the objects `{...}`
18+
*
19+
* use option 1 when you need to use some value calculated in the mapper in both withState and withRequests,
20+
* use option 2 when this is not the case, or you just need one of withState or withRequests
21+
*
22+
*/
23+
export const connect = mapper => {
24+
if (typeof mapper === 'function') {
25+
return pipe.apply(this, [
26+
withRequests((state, props) => mapper(state, props).withRequests || {}),
27+
withState((state, props) => mapper(state, props).withState || {}),
28+
])
29+
} else {
30+
const {withRequests: withRequestsMapper, withState: withStateMapper} = mapper
31+
return pipe.apply(this, [
32+
...!!withRequests && [withRequests(withRequestsMapper)],
33+
...!!withState && [withState(withStateMapper)]
34+
])
35+
}
36+
}
1037

1138
export default connect

app/util/withRequest.js renamed to app/util/withRequests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import compose from '/util/compose'
66
export const invalidate = url =>
77
dispatch(update(['invalidatedRequests'], {[url]: true}))
88

9-
export const withRequest = mapper => Component => compose({
9+
export const withRequests = mapper => Component => compose({
1010
componentWillMount () {
1111
const syncState = () => {
1212
const requests = mapper(getState(), this.props)
@@ -42,7 +42,7 @@ export const withRequest = mapper => Component => compose({
4242
}
4343
})
4444

45-
export default withRequest
45+
export default withRequests
4646

4747
const performRequests = requests =>
4848
map(

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"redux-devtools-extension": "2.13.2",
3939
"replacer-brunch": "1.0.1",
4040
"throttle-debounce": "1.0.1",
41+
"to-camel-case": "^1.0.0",
42+
"to-snake-case": "^1.0.0",
4143
"uglify-js-brunch": "2.10.0",
4244
"wasmuth": "1.6.0"
4345
},

0 commit comments

Comments
 (0)