Another Redux Manager
Yes yet another attempt to reduce boilerplate for redux whilst still keeping granular control over actions and reducers.
Why
- Reduces boilerplate
- No overblown middleware required
- Less opinionated
- plays nice with existing codebase (use as much or as little as your want)
- More control over shape of your store and reducers
Dependencies
- axios
Install
$ npm i another-redux-manager axios
Example
; // create a redux manager instanceconst getContentManager = ; // example redux thunk which dispatches actions { return { ; return getContentManager ; };} // example reducerconst INITIAL_STATE = getContentManagername: status: getContentManageractionTypesinitial error: null results: null ; { }
NOTE: The default success reducer merges the results object with the data you fetch. If you require a full replace then use the advanced reducer configuration below.-
Getting Started
const contentReduxManager = ;
returns the following object:
name:'CONTENT' actionTypes: initial:'CONTENT_FETCH_INITIAL' inProgress:'CONTENT_FETCH_IN_PROGRESS' success:'CONTENT_FETCH_SUCCESS' failure:'CONTENT_FETCH_FAILED' actionTypeKeys: CONTENT_FETCH_INITIAL:'CONTENT_FETCH_INITIAL' CONTENT_FETCH_IN_PROGRESS:'CONTENT_FETCH_IN_PROGRESS' CONTENT_FETCH_SUCCESS:'CONTENT_FETCH_SUCCESS' CONTENT_FETCH_FAILED:'CONTENT_FETCH_FAILED' actions: {...} {...} {...} {...} reducerMethods: {...} {...} {...} {...} {...} {...} {...} {...}
Access ActionCreators
const initial inProgress success failure = contentReduxManageractions; or also available as shorthand references: const initial inProgress success failure = contentReduxManager;
Access Action types
const initial inProgress success failure = contentReduxManageractionTypes;const CONTENT_FETCH_INITIAL CONTENT_FETCH_IN_PROGRESS CONTENT_FETCH_SUCCESS CONTENT_FETCH_FAILED = contentReduxManageractionTypeKeys; // plain action types
Access Reducer Methods (for use in your switch statement in your reducer)
const initial inProgress success failure = contentReduxManagerreducerMethods; // reducer methods
You can also define multiple arguments to your actions (if you need to)
const contentReduxManager = ; console;// { type: 'CONTENT_FETCH_IN_PROGRESS', payload: 'test', anotherProp: 'test' }
Usage
Create a redux helper
const contentReduxManager = ;
Properties:
prop | desc |
---|---|
name | unique prefix for actions and reducers (RECOMMENDED: all uppercase and separated by underscore to match async postfixes) |
argNames | array of argument names for actions (OPTIONAL: defaults to ['payload']) |
resultsPropsName | name of property in the reducer to place fetched data (OPTIONAL: defaults to ['results']) |
reducerMethods | function that allows customising of shape of reducer (OPTIONAL: see advanced usage) |
Call async http endpoint (axios)
const result = await contentReduxManager;
prop | desc |
---|---|
type | 'POST', 'DELETE', 'PUT', 'PATCH' .. defaults to 'GET' |
logger | your choice of logging util. Must implement logger.error() method. Defaults to console.error |
config | axios config for post params, headers etc.. |
name | used when logging errors. outputs 'Fetch Failed |
Advanced Usage
by default the shape of each reducer method looks something like:
const makeReducerMethods = { return { return ...state ... reduxManagername: ... resultsPropName: initialData status: reduxManageractionTypesinitial error: null ; } { return ...state ... reduxManagername: ... resultsPropName: ...statereduxManagernameresultsPropName ...actionpayload status: reduxManageractionTypessuccess error: null ; } { return ...state ... reduxManagername: ...statereduxManagername ... status: reduxManageractionTypesinProgress error: null ; } { return ...state ... reduxManagername: ...statereduxManagername ... status: reduxManageractionTypesfailure error: actionpayload ; } ;}; ;
However if you require custom shape for your state you can pass your own in as a property to the createReduxManager method
const makeReducerMethods = { return { return ...state ... resultsPropName: actionpayload reduxManagername: status: reduxManageractionTypessuccess error: null ; } { return ...state ... resultsPropName: initial reduxManagername: status: reduxManageractionTypesinProgress error: null ; } { return ...state ... resultsPropName: initial reduxManagername: status: reduxManageractionTypesfailure error: actionpayload ; } ;}; const getContentReduxManager = ;
You can also create a single action creator
; const inProgress = ;