import { reducerWithInitialState , ReducerBuilder } from 'typescript-fsa-reducers' ; import { CalendarState } from '../types' ; import { calendarActions } from '../actions/CalendarActions' ; let calendarReducer = null as any ; export async function getCalendarReducer ( ) { if ( ! calendarReducer ) { const initialState = { activeMonth : new Date ( ) , } ; calendarReducer = reducerWithInitialState ( initialState ) . case ( calendarActions . showToday , ( state ) => { const now = new Date ( ) ; const m = new Date ( now . getFullYear ( ) , now . getMonth ( ) , 1 ) ; return Object . assign ( { } , state , { activeMonth : m } ) ; } ) . case ( calendarActions . showNextMonth , ( state ) => { const now = state . activeMonth ; const m = new Date ( now . getFullYear ( ) , now . getMonth ( ) + 1 , 1 ) ; return Object . assign ( { } , state , { activeMonth : m } ) ; } ) . case ( calendarActions . showPreviousMonth , ( state ) => { const now = state . activeMonth ; const m = new Date ( now . getFullYear ( ) , now . getMonth ( ) - 1 , 1 ) ; return Object . assign ( { } , state , { activeMonth : m } ) ; } ) . case ( calendarActions . showNextYear , ( state ) => { const now = state . activeMonth ; const m = new Date ( now . getFullYear ( ) + 1 , now . getMonth ( ) , 1 ) ; return Object . assign ( { } , state , { activeMonth : m } ) ; } ) . case ( calendarActions . showPreviousYear , ( state ) => { const now = state . activeMonth ; const m = new Date ( now . getFullYear ( ) - 1 , now . getMonth ( ) , 1 ) ; return Object . assign ( { } , state , { activeMonth : m } ) ; } ) ; } return calendarReducer ; }