Permalink
Browse files

Add clearErrors action creator

  • Loading branch information...
MrLeebo committed Sep 14, 2017
1 parent 4783e38 commit d3a41cb9722b85c7d8e734efb25ff1fb06eb2d8c
Showing with 43 additions and 4 deletions.
  1. +8 −3 docs/resources.md
  2. +1 −0 lib/actionCreators.js
  3. +1 −0 lib/actionTypes.js
  4. +8 −0 lib/reducer.js
  5. +1 −1 package.json
  6. +7 −0 test/actionCreators.test.js
  7. +17 −0 test/reducer.test.js
View
@@ -180,8 +180,9 @@ const actions = {
fetch: todos.fetch,
create: todos.create,
update: todos.update,
destroy: todos.destroy
reset: todos.reset
destroy: todos.destroy,
reset: todos.reset,
clearErrors: todos.clearErrors
}
export default connect(mapStateToProps, actions)(MyComponent)
@@ -198,11 +199,15 @@ The code above is equivalent to using `this.props.dispatch(todos.fetch())` from
|resource.update|Performs a PUT request|
|resource.destroy|Performs a DELETE request|
|resource.reset|See below.|
|resource.clearErrors|See below.|
Unlike the other action creators, **reset** and **clearErrors** are NOT promises nor does they dispatch an AJAX request.
Unlike the other action creators, **reset** is NOT a promise nor does it dispatch an AJAX request.
Calling `todos.reset(payload)` will assign the new value to your resource's payload without interacting with the API at all.
If you call `reset()` without any parameters, it will restore your resources' redux state to its initial state, as if it were brand new.
`todos.clearErrors()` will null out the `error` state, in case you are rendering your resource to a form and the server responded with an error that you want to give the user the ability to suppress.
### Building Resource URLs
When you define a `urlRoot`, any requests that send data containing an `id` attribute will have that value appended to the URL.
View
@@ -101,6 +101,7 @@ export function resourceActionCreators (baseUrl, resourceName, definition, optio
meta: { resourceName, definition }
}
},
clearErrors: () => ({ type: types.CLEAR_ERRORS, meta: { resourceName } }),
propType
}
View
@@ -3,6 +3,7 @@ export const PREFIX = '@@redux-supermodel/'
export const NUKE = `${PREFIX}NUKE`
export const REQUEST = `${PREFIX}REQUEST`
export const RESET = `${PREFIX}RESET`
export const CLEAR_ERRORS = `${PREFIX}CLEAR_ERRORS`
// AJAX Action Types
export const PENDING = `${PREFIX}REQUEST_PENDING`
View
@@ -71,6 +71,13 @@ function rejected (state, action) {
return { ...state, [meta.resourceName]: resource }
}
function clearErrors(state, action) {
const { resourceName } = action.meta
const resource = state[resourceName]
return { ...state, [resourceName]: { ...resource, error: null } }
}
export default function reducer (state = initialState, action) {
const { type, meta } = action
if (!type || !type.startsWith(types.PREFIX)) return state
@@ -84,6 +91,7 @@ export default function reducer (state = initialState, action) {
case types.PENDING: return pending(state, action)
case types.FULFILLED: return fulfilled(state, action)
case types.REJECTED: return rejected(state, action)
case types.CLEAR_ERRORS: return clearErrors(state, action)
default: throw new Error(`Unrecognized action type: ${type}`)
}
}
View
@@ -1,6 +1,6 @@
{
"name": "redux-supermodel",
"version": "0.16.1",
"version": "0.17.0",
"description": "A package of action creator functions and reducers that deal with the state management of REST-like APIs for you... all you need is a URL!",
"repository": "https://github.com/MrLeebo/redux-supermodel",
"main": "dist/index.js",
@@ -133,6 +133,13 @@ describe('actionCreators', () => {
assertPath(result, '/blogs/latest')
})
})
describe('errors', () => {
it('should clear errors', () => {
const { type } = $subject.clearErrors()
assert.equal(type, types.CLEAR_ERRORS)
})
})
})
describe('nuke', () => {
View
@@ -266,6 +266,23 @@ describe('reducer', () => {
})
})
describe('clearErrors', () => {
def('state', () => ({ blogs: { initialized: true, payload: { id: 123 }, error: "Something broke" } }))
it('should clear errors', () => {
const meta = { resourceName: 'blogs' }
const action = { type: types.CLEAR_ERRORS, meta }
const expected = {
blogs: {
initialized: true, payload: { id: 123 }, error: null
}
}
assert.deepEqual($subject(action), expected)
})
})
describe('nuke', () => {
it('should return blank state', () => {
const action = { type: types.NUKE }

0 comments on commit d3a41cb

Please sign in to comment.