import { Action , Location } from 'history' ; import { AnyAction , Reducer } from 'redux' ; import { LOCATION_CHANGE } from './actions' ; export type RouterState = { location ? : Location | null ; action ? : Action | null ; previousLocations ? : { location ? : Location | null ; action ? : Action | null ; } [ ] ; } ; export const createRouterReducer = ( { savePreviousLocations = 0 } ) : Reducer < RouterState > => { const initialState = { location : null , action : null , } ; const numLocationToTrack = isNaN ( savePreviousLocations ) ? 0 : savePreviousLocations ; if ( numLocationToTrack ) initialState . previousLocations = [ ] ; return ( state = initialState , { type , payload } = { } as AnyAction ) => { if ( type === LOCATION_CHANGE ) { const { location , action } = payload || { } ; const previousLocations = numLocationToTrack ? [ { location , action } , ... state . previousLocations . slice ( 0 , numLocationToTrack ) ] : undefined ; return { ... state , location , action , previousLocations } ; } return state ; } ; } ;