Created
October 25, 2017 18:12
-
-
Save JonJam/63ad0347ba81eb93aabd380f380a6227 to your computer and use it in GitHub Desktop.
react-redux-ts: Reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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