redux-status-helpers

5.0.0 • Public • Published

Redux Status Helpers

travis npm version npm downloads Standard - JavaScript Style Guide

Structure Redux Better with Different Statuses

Why?

Typically when working with redux I work with API's or other things that may succeed, fail or do something in the meantime. By utilising statuses and updating the state based on the status using this package we don't need to continuously write SUCCESS, FAILURE or anything else.

  • SUCCESS. The success helper is dispatched when an API has been called and succeeded or something has worked e.g. Login Success, Fetch Result Success (with data as payload)
  • FAILURE The Fail helper is dispatched when something has failed e.g. Login Failure, Failed to Fetch result. (With Error message as payload)
  • CANCELLED The Cancelled helper is dispatched when you cancel an action (this would stop your spinner loading or whatever else you have done while in progress)

Table of Contents

Install

Add redux-status-helpers to your project using npm or yarn.

Install (NPM):

$ npm install redux-status-helpers --save

Install (Yarn):

$ yarn add redux-status-helpers

Usage

Import Statuses you require

import { success, failure, cancelled } from 'redux-status-helpers';

Dispatch your type

dispatch({ type: 'LOGIN' });

Dispatch the status depending on the result

dispatch({ type: success('LOGIN') }); //LOGIN__SUCCESS
dispatch({ type: failure('LOGIN') }); //LOGIN__FAILURE

In your reducer add a case for your statuses

const reducer = (
  state = { logged_in: false, logging_in: false, error: null },
  action
) => {
  switch (action.type) {
    case 'LOGIN':
      return { ...state, logging_in: true };
    case success('LOGIN'):
      return { ...state, logging_in: false, logged_in: true };
    case failure('LOGIN'):
      return { ...state, logging_in: false, error: true };
    case cancelled('LOGIN'):
      return { ...state, logging_in: false };
    default:
      return state;
  }
};

Contributing

Pull requests for new features, bug fixes, and suggestions are welcome!

Licence

MIT

Package Sidebar

Install

npm i redux-status-helpers

Weekly Downloads

0

Version

5.0.0

License

MIT

Unpacked Size

7.14 kB

Total Files

10

Last publish

Collaborators

  • jxsh