Skip to content

Commit eb77db5

Browse files
Update README.md
1 parent 6b34c2a commit eb77db5

File tree

2 files changed

+74
-15
lines changed

2 files changed

+74
-15
lines changed

README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,84 @@
11
# redux-compose
22

3-
create a [reducks module](https://github.com/erikras/ducks-modular-redux) in seconds
3+
create a [reducks module](https://github.com/erikras/ducks-modular-redux) in seconds with these little helpers
44

55
## Example
66

7+
### Reducks Module
8+
my-store.js
79
```js
810
import { createActionCreator, createActionParents } from 'dominikstoetter/redux-compose/actions'
911
import { createDataReducer } from 'dominikstoetter/redux-compose/reducer'
1012
import { createFetchSaga, createRootSaga } from 'dominikstoetter/redux-compose/sagas'
1113

1214
export const { FETCH_FROM_MY_API } = createActionParents([ 'FETCH_FROM_MY_API' ])
13-
export const fetchShopCredentials = createActionCreator(FETCH_FROM_MY_API)
15+
export const fetchFromMyApi = createActionCreator(FETCH_FROM_MY_API)
1416

15-
export const fetchFormMyApiSaga = createFetchSaga(({ uuid, iso8601 }) => ({
17+
export const fetchFromMyApiSaga = createFetchSaga(({ uuid }) => ({
1618
action: FETCH_FROM_MY_API,
17-
url: `//${process.env.api}/${iso8601}/${uuid}`,
19+
url: `//${process.env.api}/${uuid}`,
1820
method: 'GET',
1921
}))
2022

21-
export const fetchShopCredentialsRootSaga = createRootSaga({
22-
FETCH_FROM_MY_API: fetchFormMyApiSaga,
23+
export const rootSaga = createRootSaga({
24+
FETCH_FROM_MY_API: fetchFromMyApiSaga,
2325
})
2426

2527
export default createDataReducer(FETCH_FROM_MY_API)
2628
```
29+
30+
### Higher Order Store Connection
31+
with-my-store.js
32+
```js
33+
import { connect } from 'react-redux'
34+
import { fetchFromMyApi } from './my-store.js'
35+
import { promisifyActionCreator } from 'dominikstoetter/redux-compose/actions'
36+
37+
export default connect(
38+
({ myStore }) => ({ myStore: { ...myStore } }),
39+
dispatch => ({
40+
fetchFromMyApi: promisifyActionCreator(dispatch, fetchFromMyApi)
41+
})
42+
)
43+
```
44+
45+
### Example Component with async usage
46+
my-component/index.js
47+
```js
48+
import { compose } from 'recompose'
49+
import withLogic from './with-logic'
50+
import withData from './with-data'
51+
import View from './View'
52+
53+
export default compose(withData, withLogic)(View)
54+
55+
```
56+
my-component/with-data.js
57+
```js
58+
import withMyStore from './with-my-store'
59+
60+
export default withMyStore
61+
```
62+
63+
my-component/with-logic.js
64+
```js
65+
import { compose, withHandlers } from 'recompose'
66+
67+
export default withHandlers({
68+
fetchHandler: ({ fetchFromMyApi }) => async ({ uuid }) => {
69+
try {
70+
await fetchFromMyApi({ uuid })
71+
console.log('resolve')
72+
} catch (e) {
73+
console.log('reject')
74+
}
75+
},
76+
})
77+
```
78+
79+
my-component/View.js
80+
```js
81+
export default ({ fetchHandler }) => (
82+
<div {...{ onClick: () => fetchHandler({uuid: '2fe17f6b-c083-4603-8a45-eaa26a422f31' })}} />
83+
)
84+
```

lib/actions.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const createActionCreator = ({ action }) => (dispatch, actionCreator) => payload =>
2-
new Promise((resolve, reject) =>
3-
dispatch((payload, resolve, reject) => ({
4-
type: action,
5-
payload,
6-
resolve,
7-
reject,
8-
}))
9-
)
1+
const createActionCreator = ({ action }) => (payload, resolve, reject) => ({
2+
type: action,
3+
payload,
4+
resolve,
5+
reject,
6+
})
7+
8+
const promisifyActionCreator = (dispatch, actionCreator) => payload =>
9+
new Promise((resolve, reject) => dispatch(actionCreator(payload, resolve, reject)))
1010

1111
const createActionParent = actionName => ({
1212
[actionName]: {
@@ -29,4 +29,5 @@ module.exports = {
2929
createActionCreator,
3030
createActionParent,
3131
createActionParents,
32+
promisifyActionCreator,
3233
}

0 commit comments

Comments
 (0)