1.0.1 • Published 6 years ago

simple-simple-action-types v1.0.1

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

simple-action-types

Creating simple action types for react-redux like piece of cake!

Installation

npm:

npm install simple-action-types --save

yarn:

yarn add simple-action-types

Example

// utils/constants.js
import { createActionTypes } from 'simple-action-types';

export const actionTypes = createActionTypes({
    group: ['users_load'],
    single: ['server']
});

console.log(actionTypes); // { USERS: 'USERS_LOAD', USERS: 'USERS_LOAD_SUCCESS', USERS: 'USERS_LOAD_FAIL', SERVER: 'SERVER' }



// actions/users.actions.js
import actionTypes from 'utils/constants';

export function loadUsers() {
    ...
    type: actionTypes.USERS_LOAD
}

export function loadUsersSuccess() {
    ...
    type: actionTypes.USERS_LOAD_SUCCESS
}

export function loadUsersFail() {
    ...
    type: actionTypes.USERS_LOAD_FAIL
}


// reducers/users.reducers.js
import actionTypes from 'utils/constants';

export default (state = {}, action = {}) => {
  switch (action.type) {
    case actionTypes.USERS_LOAD:
        return {
            ...state,
            loading: true
        };
    case actionTypes.USERS_LOAD_SUCCESS:
        return {
            ...state,
            loading: false,
            data: action.result
        };
   case actionTypes.USERS_LOAD_FAIL:
        return {
            ...state,
            loading: false,
            error: action.err
        };
   
    default:
      return state;
  }
};