diff --git a/README.md b/README.md index 203eb4eef0..1b244eb898 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The `redux-starter-kit` package is intended to help address three common complai We can't solve every use case, but in the spirit of [`create-react-app`](https://github.com/facebook/create-react-app) and [`apollo-boost`](https://dev-blog.apollodata.com/zero-config-graphql-state-management-27b1f1b3c2c3), we can try to provide some tools that abstract over the setup process and handle the most common use cases, as well as include some useful utilities that will let the user simplify their application code. -This package is _not_ intended to solve every possible complaint about Redux, and is deliberately limited in scope. It does _not_ address concepts like "reusable encapsulated Redux modules", data fetching, folder or file structures, managing entity relationships in the store, and so on. +This package is _not_ intended to solve every possible complaint about Redux, and is deliberately limited in scope. It does _not_ address concepts like "reusable encapsulated Redux modules", data fetching, folder or file structures, managing entity relationships in the store, and so on. ### What's Included @@ -141,7 +141,36 @@ const todosReducer = createReducer([], { }); ``` +`createReducer` supports [Flux Standand Actions (FSA)](https://github.com/redux-utilities/flux-standard-action): +```js +const notifications = (state, payload, { error, meta }) => { + // As stated by FSA docs, by convention, if error is true, the payload SHOULD be an error object. + if (payload instanceof Error) { + state.push({ intent: "danger", message: payload.message }); + } + + // You can also use the error and meta properties, they are useful if you don't want to follow this convention. + // (Of course you are free to use those properties for other use cases. Error handling is just an example.) + if (error) { + state.push({ intent: meta.intent, message: payload }); + } +}; +``` + +If you don't use FSA in your project, that's not an issue: + +```js +const addInvoice = (date, dueDate, amount) => ({ + type: ADD_INVOICE, + date, + dueDate, + amount +}); + +const addInvoiceCaseReducer = (state, { date, dueDate, amount }) => + state.push({ date, dueDate, amount }); +``` #### `createSelector` @@ -179,4 +208,4 @@ const getContents = createSelector({foo : "foo", bar : "nested.bar" }); #### `createNextState` -The default immutable update function from the [`immer` library](https://github.com/mweststrate/immer#api), re-exported here as `createNextState` (also commonly referred to as `produce`) \ No newline at end of file +The default immutable update function from the [`immer` library](https://github.com/mweststrate/immer#api), re-exported here as `createNextState` (also commonly referred to as `produce`) diff --git a/package.json b/package.json index 3aa4852029..0d6fd1a26e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "dist" ], "dependencies": { + "flux-standard-action": "^2.0.1", "immer": "^1.1.1", "redux": "^3.7.2", "redux-devtools-extension": "^2.13.2", diff --git a/src/createReducer.js b/src/createReducer.js index 4e02634022..56d4e20d47 100644 --- a/src/createReducer.js +++ b/src/createReducer.js @@ -1,14 +1,20 @@ import createNextState from "immer"; +import { isFSA } from "flux-standard-action"; export function createReducer(initialState, actionsMap) { return function(state = initialState, action) { - const {type, payload} = action; + const {type, ...rest} = action; return createNextState(state, draft => { const caseReducer = actionsMap[type]; if(caseReducer) { - return caseReducer(draft, payload); + if (isFSA(action)) { + const {payload, error, meta} = rest + return caseReducer(draft, payload, { error, meta }); + } + + return caseReducer(draft, rest); } return draft; diff --git a/yarn.lock b/yarn.lock index 11f59aa1d6..af4b423d8c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -614,6 +614,12 @@ fill-range@^2.1.0: repeat-element "^1.1.2" repeat-string "^1.5.2" +flux-standard-action@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/flux-standard-action/-/flux-standard-action-2.0.1.tgz#9e2947f5904edd48f9fab7516ddb8dea6f612075" + dependencies: + lodash "^4.0.0" + for-in@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" @@ -770,7 +776,7 @@ lodash-es@^4.2.1: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.5.tgz#9fc6e737b1c4d151d8f9cae2247305d552ce748f" -lodash@^4.17.4, lodash@^4.2.1: +lodash@^4.0.0, lodash@^4.17.4, lodash@^4.2.1: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"