Defaults
The library expose some functions:
import {
overrideDefaultTypeSuffixes,
overrideGenerateInitialState,
overrideGenerateReducer,
overrideActionTransformer,
} from 'redux-autoreducers'
overrideDefaultTypeSuffixes
Description: Function to change default type suffixes.
Defaults are: PENDING
, FULFILLED
, REJECTED
.
Params:
Param | Type | Required |
---|---|---|
newDefaultTypeSuffixes | Object | True |
Example:
overrideDefaultTypeSuffixes({
PENDING: 'PENDING',
FULFILLED: 'FULFILLED',
REJECTED: 'REJECTED',
})
overrideGenerateReducer
Description: Function to change default reducer generator.
Params:
Param | Type | Required |
---|---|---|
newGenerateReducer | Function | True |
newGenerateReducer
have these params:
Param | Type | Description | Required | Schema |
---|---|---|---|---|
type | String | Rule type (example: 'GET_ALL') | True | String |
lowerCamelCaseType | String | Rule in lower camel case (example: 'getAll') | True | String |
typeSuffixes | Object | Type suffixes | True | for example { PENDING: String, FULFILLED: String, REJECTED: String } |
newGenerateReducer
must return an object that have schema of https://github.com/tomatau/type-to-reducer.
Example of code (default code):
overrideGenerateReducer((type, lowerCamelCaseType, typeSuffixes) => ({
[type]: {
[typeSuffixes.PENDING]: state => ({
...state,
[`is${capitalize(lowerCamelCaseType)}Pending`]: true,
[`${lowerCamelCaseType}Data`]: null,
[`${lowerCamelCaseType}Error`]: null,
}),
[typeSuffixes.FULFILLED]: (state, action) => ({
...state,
[`is${capitalize(lowerCamelCaseType)}Pending`]: false,
[`${lowerCamelCaseType}Data`]: action.data,
[`${lowerCamelCaseType}Error`]: null,
}),
[typeSuffixes.REJECTED]: (state, action) => ({
...state,
[`is${capitalize(lowerCamelCaseType)}Pending`]: false,
[`${lowerCamelCaseType}Data`]: null,
[`${lowerCamelCaseType}Error`]: action.error,
}),
},
}))
Example of return for 'GET_ALL' type:
{
GET_ALL: {
PENDING: (state, action) => {...},
FULLFILLED: (state, action) => {...},
REJECTED: (state, action) => {...},
}
}
overrideGenerateInitialState
Description: Function to change default initial state generator
Params:
Param | Type | Required |
---|---|---|
newGenerateInitialState | Function | True |
newGenerateInitialState
have these params:
Param | Type | Description | Required | Schema |
---|---|---|---|---|
lowerCamelCaseType | String | Rule in lower camel case (example: 'getAll') | True | String |
newGenerateInitialState
must return an object.
Example of code:
overrideGenerateReducer((lowerCamelCaseType) => ({
[`is${capitalize(lowerCamelCaseType)}Pending`]: false,
[`${lowerCamelCaseType}Data`]: null,
[`${lowerCamelCaseType}Error`]: null,
}))
Example of return for 'GET_ALL' type:
{
isGetAllPending: false,
getAllData: null,
getAllError: null,
}
overrideActionTransformer
Description: Function to change default action transformer
Params:
Param | Type | Required |
---|---|---|
actionTransformer | Function | True |
actionTransformer
have these params:
Param | Type | Description | Required | Schema |
---|---|---|---|---|
action | Object | Action to transformer before entering in reducer | True | { type: String, ... } |
actionTransformer
must return another action that will go into reducer.
Example of code to transform payload into a flatten action:
overrideActionTransformer((action) => {
if (!action.payload) {
return action;
}
const { type, payload: { error, data } } = action;
return {
type,
error,
data,
};
})