Skip to content

Pass error/meta to case reducers, support non-FSAs #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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`)
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`)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions src/createReducer.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 7 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down