redux-sleep
Convention over configuration approach to redux facing RESTful endpoints.
Last updated 2 years ago by jackrobertscott .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install redux-sleep 
SYNC missed versions from official npm registry.

redux-sleep

Simple structure to redux reducers to help speed up development.

Build Status

Convention over configuration approach to redux reducers which face RESTful endpoints.

Install

Get started by installing hobson (and mongoose, if you haven't already).

npm install --save redux redux-sleep

To use the thunk functions; you must also install redux-thunk.

npm install --save redux-thunk

Usage

Create the resource.

const listingResource = new Resource({
  scope: 'appName',
  name: 'listing',
  key: '_id', // id key on the items
  state: {
    person: 'fred',
    extra: 'options',
    initial: ['state'],
  },
});

// code...

export default listingResource;

Define custom methods.

listingResource.addMethod('changePerson', (state, { payload = null }) => ({
  ...state,
  person: payload,
}))

Define custom thunks.

listingResource.addThunk('getPeople', () => resource => async (dispatch, getState) => {
  const { token } = getState().user.auth;
  const person = await apiGetRandomPerson(token);
  dispatch(resource.action('changePerson')(person));
  return { person };
});

Add the reducers to the redux state.

const reducers = {
  listing: listingResource.reducer,
};

Get the actions and thunks from the resource.

const mapDispatchToProps = {
  patchListing: listingResource.action('patch'),
  getListings: listingResource.thunk('getListings'),
};

Use the functions!

this.props.patchListing({ hello: 'there' });

Initial State

Initial state which is provided.

{
  [pluralName]: [], // e.g. listings: []
  [singularName]: null, // e.g. listing: null
  problem: null, // will be filled with error object
  loading: false,
  success: null,
}

Methods Provided

List of the methods which the resource provides by default.

Method Description
reset Resets the state back to the initial state.
loading Sets the loading state option.
success Sets the success state option.
errored Sets the errored state option, usually with object.
set Sets the manyName array e.g. listings.
replace Replaces an item in the manyName array.
remove Removes an item from the manyName array.
add Adds an item to the manyName array.
current Sets the singleName object, usually for editing or previewing.

Maintainers

Current Tags

  • 0.5.2                                ...           latest (2 years ago)

17 Versions

  • 0.5.2                                ...           2 years ago
  • 0.5.1                                ...           2 years ago
  • 0.5.0                                ...           2 years ago
  • 0.4.4                                ...           2 years ago
  • 0.4.3                                ...           2 years ago
  • 0.4.2                                ...           2 years ago
  • 0.4.1                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.4                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 19
Dependencies (4)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |