Skip to content

Commit ce9ba2c

Browse files
dominikstoetterdomis4
authored andcommitted
Update README.md
1 parent 6b34c2a commit ce9ba2c

File tree

5 files changed

+88
-19
lines changed

5 files changed

+88
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.history

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.history

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: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
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 => ({
12+
[actionName]: {
13+
action: actionName,
14+
},
15+
})
16+
17+
const createActionFetchParent = actionName => ({
1218
[actionName]: {
1319
action: actionName,
1420
success: `${actionName}_SUCCESS`,
1521
error: `${actionName}_ERROR`,
1622
},
1723
})
1824

19-
const createActionParents = actionNames =>
25+
const createActionFetchParents = actionNames =>
2026
actionNames.reduce(
2127
(result, actionName) => ({
2228
...result,
23-
...createActionParent(actionName),
29+
...createActionFetchParent(actionName),
2430
}),
2531
{}
2632
)
2733

2834
module.exports = {
2935
createActionCreator,
3036
createActionParent,
31-
createActionParents,
37+
createActionFetchParent,
38+
createActionFetchParents,
39+
promisifyActionCreator,
3240
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-compose",
3-
"version": "1.0.0",
3+
"version": "0.1.4",
44
"main": "src/index.js",
55
"repository": "git@github.com:dominikstoetter/redux-compose.git",
66
"author": "<dominik.stoetter@8select.de>",

0 commit comments

Comments
 (0)