|
1 | 1 | # redux-compose |
2 | 2 |
|
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 |
4 | 4 |
|
5 | 5 | ## Example |
6 | 6 |
|
| 7 | +### Reducks Module |
| 8 | +my-store.js |
7 | 9 | ```js |
8 | 10 | import { createActionCreator, createActionParents } from 'dominikstoetter/redux-compose/actions' |
9 | 11 | import { createDataReducer } from 'dominikstoetter/redux-compose/reducer' |
10 | 12 | import { createFetchSaga, createRootSaga } from 'dominikstoetter/redux-compose/sagas' |
11 | 13 |
|
12 | 14 | 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) |
14 | 16 |
|
15 | | -export const fetchFormMyApiSaga = createFetchSaga(({ uuid, iso8601 }) => ({ |
| 17 | +export const fetchFromMyApiSaga = createFetchSaga(({ uuid }) => ({ |
16 | 18 | action: FETCH_FROM_MY_API, |
17 | | - url: `//${process.env.api}/${iso8601}/${uuid}`, |
| 19 | + url: `//${process.env.api}/${uuid}`, |
18 | 20 | method: 'GET', |
19 | 21 | })) |
20 | 22 |
|
21 | | -export const fetchShopCredentialsRootSaga = createRootSaga({ |
22 | | - FETCH_FROM_MY_API: fetchFormMyApiSaga, |
| 23 | +export const rootSaga = createRootSaga({ |
| 24 | + FETCH_FROM_MY_API: fetchFromMyApiSaga, |
23 | 25 | }) |
24 | 26 |
|
25 | 27 | export default createDataReducer(FETCH_FROM_MY_API) |
26 | 28 | ``` |
| 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 | +``` |
0 commit comments