Skip to content

Commit

Permalink
Renamed requestsReducer getDefault to getDefaultData and added it to …
Browse files Browse the repository at this point in the history
…types and docs
  • Loading branch information
klis87 committed Feb 28, 2019
1 parent 5b75c61 commit db88e1d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/redux-saga-requests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ In order to be flexible, apart from `actionType` passed in `requestsReducer` con
following attributes:
- `multiple: boolean`: default to `false`, change it to `true` if you want your not loaded data to be stored as `[]`
instead of `null`
- `getDefaultData: (multiple: boolean) => any`: use instead of `multiple`, if you want your initial data to be
something else than `null` or `[]`, for instance `() => ({ value: null })`, `multiple => (multiple ? [] : null)`
by default
- `getData: (state, action, config) => data`: describes how to get data from `action` object, by default returns `action.data` or `action.payload.data` when action is FSA compliant
- `updateData: (state, action, config) => data`: optional, useful together with operations to overwrite `getData` default, see more
information in `operations`
Expand Down
8 changes: 4 additions & 4 deletions packages/redux-saga-requests/src/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const normalizeActionType = actionType =>
const operationConfigHasRequestKey = config =>
typeof config !== 'boolean' && !!config.getRequestKey;

const getInitialRequestState = ({ getDefault, multiple, operations }) => ({
data: getDefault(multiple),
const getInitialRequestState = ({ getDefaultData, multiple, operations }) => ({
data: getDefaultData(multiple),
pending: 0,
error: null,
operations:
Expand Down Expand Up @@ -59,7 +59,7 @@ const getDataUpdaterForSuccess = (reducerConfig, operationConfig) => {

const defaultConfig = {
multiple: false,
getDefault: (multiple) => multiple ? [] : null,
getDefaultData: multiple => (multiple ? [] : null),
getData: (state, action) =>
action.payload ? action.payload.data : action.data,
updateData: null,
Expand All @@ -77,7 +77,7 @@ const defaultConfig = {
}),
onError: (state, action, config) => ({
...state,
data: config.getDefault(config.multiple),
data: config.getDefaultData(config.multiple),
pending: state.pending - 1,
error: config.getError(state, action, config),
}),
Expand Down
29 changes: 20 additions & 9 deletions packages/redux-saga-requests/src/reducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ describe('reducers', () => {
});

describe('providing the default value of the reducer', () => {
const emptyFeatureCollection = { type: 'FeatureCollection', features: [] };
const defaultProvider = jest.fn(() => (emptyFeatureCollection));
const reducer = requestsReducer({ actionType, getDefault: defaultProvider });
const emptyFeatureCollection = {
type: 'FeatureCollection',
features: [],
};
const defaultProvider = jest.fn(() => emptyFeatureCollection);
const reducer = requestsReducer({
actionType,
getDefaultData: defaultProvider,
});

afterEach(() => {
defaultProvider.mockClear();
Expand All @@ -105,12 +111,17 @@ describe('reducers', () => {
it('resets data via getDefault on errors', () => {
const someError = 'asdasda';
const action = { type: error(actionType), error: someError };
expect(reducer({
data: {},
error: someError,
pending: 1,
operations: null,
}, action)).toEqual({
expect(
reducer(
{
data: {},
error: someError,
pending: 1,
operations: null,
},
action,
),
).toEqual({
data: emptyFeatureCollection,
error: someError,
pending: 0,
Expand Down
1 change: 1 addition & 0 deletions packages/redux-saga-requests/types/index.d.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ watchRequests({ abortOn: ['TYPE'] });

const globalConfig = {
multiple: false,
getDefaultData: multiple => (multiple ? [] : null),
getData: (state, action) => action.payload.data,
getError: (state, action) => action.payload,
onRequest: (state, action, { multiple }) => ({
Expand Down
2 changes: 2 additions & 0 deletions packages/redux-saga-requests/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Operations = {

type GlobalReducerConfig = {
multiple?: boolean;
getDefaultData?: (multiple: boolean) => any;
getData?: OnActionCallback;
updateData?: OnActionCallback;
getError?: OnActionCallback;
Expand All @@ -124,6 +125,7 @@ type LocalReducerConfig = GlobalReducerConfig & ActionTypeReducerConfig;
type MergedReducerConfig = {
actionType: string;
multiple: boolean;
getDefaultData: (multiple: boolean) => any;
getData: OnActionCallback;
updateData?: OnActionCallback;
getError: OnActionCallback;
Expand Down

0 comments on commit db88e1d

Please # to comment.