0.5.2 • Published 6 years ago

redux-sleep v0.5.2

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

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.

MethodDescription
resetResets the state back to the initial state.
loadingSets the loading state option.
successSets the success state option.
erroredSets the errored state option, usually with object.
setSets the manyName array e.g. listings.
replaceReplaces an item in the manyName array.
removeRemoves an item from the manyName array.
addAdds an item to the manyName array.
currentSets the singleName object, usually for editing or previewing.

Maintainers

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago