@dabapps/redux-requests
Version:
Library for simple redux requests
60 lines (57 loc) • 1.42 kB
text/typescript
import { isFSA } from 'flux-standard-action';
import { AnyAction } from 'redux';
import { REQUEST_STATE, RESET_REQUEST_STATE } from './actions';
import {
ResetRequestStatePayload,
ResponsesReducerState,
SetRequestStatePayload,
} from './types';
export function responsesReducer<T = any>(
state: ResponsesReducerState<T> = {},
action: AnyAction
): ResponsesReducerState<T> {
switch (action.type) {
case REQUEST_STATE:
if (isFSA(action)) {
const {
actionSet,
requestState,
tag,
data,
meta,
} = action.payload as SetRequestStatePayload;
const existing = state[actionSet.REQUEST] || {};
return {
...state,
[actionSet.REQUEST]: {
...existing,
[tag || '']: {
requestState,
data,
meta,
},
},
};
}
break;
case RESET_REQUEST_STATE:
if (isFSA(action)) {
const { actionSet, tag } = action.payload as ResetRequestStatePayload;
const existing = state[actionSet.REQUEST] || {};
return {
...state,
[actionSet.REQUEST]: {
...existing,
[tag || '']: {
requestState: null,
data: null,
},
},
};
}
break;
default:
return state;
}
return state;
}