0.1.26 • Published 5 years ago

redux-action-generate v0.1.26

Weekly downloads
87
License
-
Repository
-
Last release
5 years ago

redux-action-generate

Generates reducers and actions from one reducers list.

Install:

yarn add redux-action-generate

import reduxActionGenerate from 'redux-action-generate';

'reduxActionGenerate' function signature:

_

Common object argument that is passed to 'reduxActionGenerate' function:
{
// *required 
// array of reducers;
 reducers = [
   {
      // *required
      // name of reducer
      name: 'users',
      // *required
      // name for action's dispatch type 
      basicType: constants.COMMON,
      // initial state for this specific reducer
      initialState: authInitialState,
      // specific config for redux-persist lib
      config: usersPersistConfig,
      // reducer's handlers for some specific action's type 
      handlers: {
         [constants.LOGIN + '_CUSTOM']: (state, action) => ({ ...state }),
         [constants.LOGIN + '_CUSTOM2']: (state, action) => {
           return { ...state };
         },
         [constants.LOGIN + '_CUSTOM3']: (state, action) => {
           return { ...state };
         },
       },
     },
    },
    {
    ...
    }
 ],
 
 // any redux middlewares;
 middlewares = [],
 
 // initial state for 'configureStore' redux api funcrtion;
 initialState = {},
 
 // config object for redux-persist library
 persistConfig = defaultPersistConfig,
 
 // function which inserts custom headers to 
 // api calls made with A.dispatch[<reducer name>][<Get/Post/Put/Patch/Delete>] 
 // returns an object
 // i.e. A.dispatchCommonGet({url: 'http...'})
 apiCallCustomHeaders,
 
 // function which inserts custom 'catch' behaviour on each http request
 commonCatchFunc,
}

Example:

_

// receives error as an argument
function commonCatchFunc(err) {
 if (err.response.status) === 401) {
   logout();
 }
}

// receives store where you can get 'store.getState().token' to insert it in each request
// must return an object
function apiCallCustomHeaders(store) {
 let headers = {
   'Content-Type': 'application/json',
   'Authorization': store.getState().token
 };
 return headers;
}

// common config for redux-persist - to save info locally - is not required
const persistConfig = {
 key: 'root',
 whitelist: ['token'],
 storage: storage,
 stateReconciler: autoMergeLevel2,
};

export const { 
   store, 
   persistor, 
   A 
} = reduxActionGenerate({
 reducers,
 apiCallCustomHeaders,
 commonCatchFunc,
 persistConfig,
});

Features

  • all reducers are generated from an array of objects - each for specific reducer - no need for separate files;
  • for each reducer there is a bunch of default actions generated:
  • default actions image naming convention - A. + 'dispatch' + name of reducer (in this case name was 'common') /+ possible actions
  • no mess with files for each reducer + file for each action creator
  • each bunch of actions for each reducer has:
  1. common dispatcher - A.dispatchCommon({ 'config[0].locale': 'en'}) - can set values on any depth (thanks to lodash set with no mutation under the hood)
  2. A.dispatchCommonClear() - set initial state on reducer
  3. A.dispatchCommonError({error: error}) - set error on reducer (but can be done with the first method)
  4. Bunch of methods for ajax request (with axios)
A.dispatchCommonPost({
    url: 'https://...',
    data: data,
    
    // may be omitted - if 'format' function is not provided - all response will 
    // be injected in reducer as is
    format: (response) => { 
        // do what uyou need with the response
        // if don't want to inject anything in reducer - return empty object
    }
})
0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.23

5 years ago

0.1.22

5 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago