license import { ActionCreator , Action } from 'redux' ; import { ThunkAction } from 'redux-thunk' ; import { RootState } from '../store.js' ; export const UPDATE_LOCATION = 'UPDATE_LOCATION' ; export interface AppActionUpdateLocation extends Action < 'UPDATE_LOCATION' > { view : string ; list ? : string ; page : number ; id : string ; } ; export type AppAction = AppActionUpdateLocation ; type ThunkResult = ThunkAction < void , RootState , undefined , AppAction > ; export const updateLocation = ( location ) => ( dispatch , getState ) => { const path = decodeURIComponent ( location . pathname ) ; const splitPath = ( path || '' ) . slice ( 1 ) . split ( '/' ) ; const params = location . search . slice ( 1 ) . split ( '&' ) . reduce ( ( acc , item ) => { const pair = item . split ( '=' ) ; acc [ decodeURIComponent ( pair [ 0 ] ) ] = decodeURIComponent ( pair [ 1 ] ) ; return acc ; } , { } as { [ key ] : string ; } ) ; const pageStr = params [ 'page' ] ; const page = pageStr ? parseInt ( pageStr , 10 ) : 1 ; const id = params [ 'id' ] ; let list ; let view ; switch ( splitPath [ 0 ] ) { case '' : case 'new' : case 'ask' : case 'show' : case 'jobs' : case 'favorites' : switch ( splitPath [ 0 ] ) { case '' : list = 'news' ; break ; case 'new' : list = 'newest' ; break ; case 'ask' : case 'show' : case 'jobs' : case 'favorites' : list = splitPath [ 0 ] ; } view = 'list' ; import ( '../components/hn-list.js' ) . then ( module => { const state = getState ( ) ; dispatch ( module . fetchListIfNeeded ( module . currentListSelector ( state ) , page ) ) ; } ) ; break ; case 'user' : view = 'user' ; import ( '../components/hn-user.js' ) . then ( module => { const state = getState ( ) ; dispatch ( module . fetchUserIfNeeded ( module . currentUserSelector ( state ) ) ) ; } ) ; break ; case 'item' : view = 'item' ; import ( '../components/hn-item.js' ) . then ( module => { const state = getState ( ) ; dispatch ( module . fetchItemIfNeeded ( module . currentItemSelector ( state ) ) ) ; } ) ; break ; default : view = 'invalid-page' ; import ( '../components/hn-invalid-page.js' ) ; } dispatch ( { type : UPDATE_LOCATION , view , list , page , id } ) ; } ;