Skip to content

Instantly share code, notes, and snippets.

@JonJam
Created October 25, 2017 18:12
Show Gist options
  • Save JonJam/63ad0347ba81eb93aabd380f380a6227 to your computer and use it in GitHub Desktop.
Save JonJam/63ad0347ba81eb93aabd380f380a6227 to your computer and use it in GitHub Desktop.
react-redux-ts: Reducer
import ActionTypeKeys, { ActionTypeStates } from "../actions/ActionTypeKeys";
import ActionTypes from "../actions/ActionTypes";
import initialState from "./initialState";
export default function pendingActionsReducer(
state = initialState.pendingActions,
action: ActionTypes
) {
if (actionTypeEndsInInProgress(action.type)) {
return onInProgressAction(state);
} else if (
actionTypeEndsInSuccess(action.type) ||
actionTypeEndsInFail(action.type)
) {
return onSuccessOrFailAction(state);
} else {
return state;
}
}
function actionTypeEndsInInProgress(type: ActionTypeKeys) {
const inProgress = ActionTypeStates.INPROGRESS;
return type.substring(type.length - inProgress.length) === inProgress;
}
function actionTypeEndsInSuccess(type: ActionTypeKeys) {
const success = ActionTypeStates.SUCCESS;
return type.substring(type.length - success.length) === success;
}
function actionTypeEndsInFail(type: ActionTypeKeys) {
const fail = ActionTypeStates.FAIL;
return type.substring(type.length - fail.length) === fail;
}
function onInProgressAction(state: number) {
return state + 1;
}
function onSuccessOrFailAction(state: number) {
return state - 1;
}
import { combineReducers } from "redux";
import IStoreState from "../store/IStoreState";
import isAuthenticated from "./authenticationReducer";
import pendingActions from "./pendingActionsReducer";
const rootReducer = combineReducers<IStoreState>({
isAuthenticated,
pendingActions
});
export default rootReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment