redux-toolbelt
Advanced tools
Comparing version 2.0.14 to 2.0.15
@@ -16,7 +16,14 @@ 'use strict'; | ||
* @param actionCreator | ||
* @param options = {dataProp , shouldDestroyData, defaultData, shouldSpread}) | ||
* @param options = {dataProp , shouldDestroyData, defaultData, shouldSpread, shouldSetData, dataGetter}) | ||
* @returns {Function} | ||
*/ | ||
function makeAsyncReducer(actionCreator, options) { | ||
var defaults = { dataProp: 'data', shouldDestroyData: true, defaultData: undefined, shouldSpread: false, shouldSetData: true }; | ||
var defaults = { | ||
dataProp: 'data', | ||
shouldDestroyData: true, | ||
defaultData: undefined, | ||
shouldSpread: false, | ||
shouldSetData: true, | ||
dataGetter: undefined | ||
}; | ||
options = _extends(defaults, options); | ||
@@ -30,3 +37,4 @@ | ||
var type = _ref2.type, | ||
payload = _ref2.payload; | ||
payload = _ref2.payload, | ||
meta = _ref2.meta; | ||
@@ -42,3 +50,4 @@ switch (type) { | ||
var progress = state && state.progress === undefined ? {} : { progress: 0 }; | ||
return options.shouldSpread ? _extends({ loading: false }, progress, payload) : _extends({ loading: false }, progress, _defineProperty({}, options.dataProp, payload)); | ||
var data = typeof options.dataGetter === 'function' ? options.dataGetter(state, { type: type, payload: payload, meta: meta }) : payload; | ||
return options.shouldSpread ? _extends({ loading: false }, progress, data) : _extends({ loading: false }, progress, _defineProperty({}, options.dataProp, data)); | ||
} | ||
@@ -45,0 +54,0 @@ case actionCreator.progress.TYPE: |
{ | ||
"name": "redux-toolbelt", | ||
"version": "2.0.14", | ||
"version": "2.0.15", | ||
"description": "Async Actions helpers for redux-toolbelt", | ||
@@ -11,3 +11,7 @@ "main": "lib/index.js", | ||
"repository": "git+https://github.com/welldone-software/redux-toolbelt.git", | ||
"author": "Arik Maor <arikmaor@welldone-software.com>", | ||
"authors": [ | ||
"Dani Kenan (dani@welldone-software.com)", | ||
"Arik Maor <arikm@welldone-software.com>", | ||
"Vitalik Zaidman (vitalikz@welldone-software.com)" | ||
], | ||
"license": "MIT", | ||
@@ -14,0 +18,0 @@ "bugs": { |
@@ -16,2 +16,3 @@ # Redux-Toolbelt | ||
- [Usage](#usage) | ||
- [Demo](#demo) | ||
- [API Reference](#api-reference) | ||
@@ -22,3 +23,4 @@ * [`composeReducers()`](#composereducers) | ||
+ [Adding payload and metadata to actions](#adding-payload-and-metadata-to-actions) | ||
+ [Actions Prefixes](#actions-prefixes) | ||
+ [Actions Defaults](#actions-defaults) | ||
* [`makeReducer()`](#makereducer) | ||
* [`makeAsyncActionCreator()`](#makeasyncactioncreator) | ||
@@ -312,3 +314,3 @@ * [`makeAsyncReducer()`](#makeasyncreducer) | ||
Reducers created with `makeAsyncReducer()` respond to the request, progree, success and failure actions. | ||
Reducers created with `makeAsyncReducer()` respond to the request, progress, success and failure actions. | ||
@@ -343,2 +345,21 @@ ##### Initialization | ||
You can customize the `data` field value that is assigned during the success reducer using the `dataGetter` option. | ||
```js | ||
const asyncReducer = makeAsyncReducer(asyncAction, { | ||
dataGetter: ({data}, {payload}) => ([...data, payload]), | ||
}) | ||
const state = {data: ['a']} | ||
asyncReducer(state, asyncAction.success('b')) | ||
// ==> { | ||
// loading: false, | ||
// data: ['a', 'b'] | ||
// } | ||
``` | ||
> Without the `dataGetter` the `payload` replaces the old data. If you provide a `dataGetter`, it is called and the data | ||
it returns is the one that is used to replace the old data. This allows you to add items, remove and do what ever you | ||
need to create the new `data` from the current `state` and `action`. | ||
You can remove the use of the `dataProp`. | ||
@@ -363,2 +384,7 @@ ```js | ||
> Please note however that we do not recommend to use `shouldSpread`. We have gained a lot from using the `data` member. | ||
The separation of the state to *maintainance* information and *actual* `data`, as well as adhering to such naming convention proves invaluable in terms of readability and consistency and | ||
highly increases the number of opportunities for logic reuse especially by making it clearer and easier to apply logic | ||
on state generically. | ||
##### Request | ||
@@ -365,0 +391,0 @@ When the reducer gets the `request` action it updates the `loading` field. |
/** | ||
* | ||
* @param actionCreator | ||
* @param options = {dataProp , shouldDestroyData, defaultData, shouldSpread}) | ||
* @param options = {dataProp , shouldDestroyData, defaultData, shouldSpread, shouldSetData, dataGetter}) | ||
* @returns {Function} | ||
*/ | ||
export default function makeAsyncReducer(actionCreator, options) { | ||
const defaults = { dataProp: 'data', shouldDestroyData: true, defaultData: undefined, shouldSpread: false, shouldSetData: true } | ||
const defaults = { | ||
dataProp: 'data', | ||
shouldDestroyData: true, | ||
defaultData: undefined, | ||
shouldSpread: false, | ||
shouldSetData: true, | ||
dataGetter: undefined, | ||
} | ||
options = Object.assign(defaults, options) | ||
@@ -15,3 +22,3 @@ | ||
return function (state = defaultState, { type, payload }) { | ||
return function (state = defaultState, { type, payload, meta }) { | ||
switch (type) { | ||
@@ -27,5 +34,7 @@ case actionCreator.TYPE: | ||
const progress = state && state.progress === undefined ? {} : {progress: 0} | ||
const data = typeof(options.dataGetter) === 'function' ? | ||
options.dataGetter(state, {type, payload, meta}) : payload | ||
return options.shouldSpread ? | ||
{loading: false, ...progress, ...payload} : | ||
{loading: false, ...progress, [options.dataProp]: payload} | ||
{loading: false, ...progress, ...data} : | ||
{loading: false, ...progress, [options.dataProp]: data} | ||
} | ||
@@ -32,0 +41,0 @@ case actionCreator.progress.TYPE: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39917
569
467
1