import { Action , initialState , CalendarState as State , } from './Calendar.types' ; import { getDateFormat , getDateFormatRegex , } from '../DateRange/DateFormat' ; /**
 * Internal component state
 */ const dateFormat = getDateFormat ( ) ; const dateFormatRegex = getDateFormatRegex ( dateFormat ) ; export function init ( state ) { const now = new Date ( ) ; const viewportDate = new Date ( now . getFullYear ( ) , now . getMonth ( ) , 1 ) ; return { ... state , viewportDate , } ; } export function reducer ( state , action ) { switch ( action . type ) { case 'OPEN' : { const [ rangeStart ] = state . range ; const viewportDate = rangeStart ? new Date ( rangeStart . getFullYear ( ) , rangeStart . getMonth ( ) , 1 ) : state . viewportDate ; return { ... state , open : true , viewportDate , draft : [ state . range [ 0 ] , state . range [ 1 ] ] , } ; } case 'SAVE' : return { ... state , open : false , range : [ state . draft [ 0 ] , state . draft [ 1 ] ] , draft : [ null , null ] , } ; case 'CLOSE' : return { ... state , open : false , draft : [ null , null ] , startLabel : null , startDateError : null , endLabel : null , endDateError : null , } ; case 'CLEAR' : return { ... initialState , open : true , viewportDate : state . viewportDate , } ; case 'CHANGE_VIEWPORT' : return { ... state , viewportDate : action . payload } ; case 'CHANGE_START' : { const draftEnd = state . draft [ 1 ] ; const viewportDate = state . viewportDate ; const label = action . payload . label ; if ( ! dateFormatRegex . test ( label ) ) { return { ... state , startLabel : label , startDateError : `Try this format: ${ dateFormat }` , } ; } const newStartDate = new Date ( label ) ; const minDate = action . payload . minDate ; if ( isNaN ( newStartDate . getTime ( ) ) ) { return { ... state , startLabel : action . payload . label , startDateError : 'Invalid date' , } ; } if ( minDate && new Date ( minDate . toDateString ( ) ) > new Date ( newStartDate . toDateString ( ) ) ) { return { ... state , startLabel : action . payload . label , startDateError : "You can't start in the past :(" , } ; } let newViewportDate = new Date ( viewportDate ) ; const tempViewportDate = new Date ( newStartDate . getFullYear ( ) , newStartDate . getMonth ( ) , 1 ) ; if ( tempViewportDate < viewportDate ) { newViewportDate = tempViewportDate ; } viewportDate . setUTCMonth ( viewportDate . getUTCMonth ( ) + 1 ) ; if ( tempViewportDate > viewportDate ) { newViewportDate = tempViewportDate ; } if ( newStartDate > draftEnd ) { return { ... state , startLabel : action . payload . label , startDateError : null , draft : [ newStartDate , newStartDate ] , viewportDate : newViewportDate , } ; } return { ... state , startLabel : action . payload . label , startDateError : null , draft : [ newStartDate , draftEnd ] , viewportDate : newViewportDate , } ; } case 'CHANGE_END' : const draftStart = state . draft [ 0 ] ; const newEndDate = new Date ( action . payload . label ) ; const maxDate = action . payload . maxDate ; const label = action . payload . label ; if ( ! draftStart ) { return { ... state , endDateError : 'Must select start date first' , } ; } if ( ! dateFormatRegex . test ( label ) ) { return { ... state , endLabel : label , endDateError : `Try this format: ${ dateFormat }` , } ; } if ( isNaN ( newEndDate . getTime ( ) ) ) { return { ... state , endLabel : label , endDateError : 'Invalid date' , } ; } if ( maxDate && new Date ( maxDate . toDateString ( ) ) < new Date ( newEndDate . toDateString ( ) ) ) { return { ... state , endLabel : action . payload . label , endDateError : 'Invalid end date :(' , } ; } if ( newEndDate < draftStart ) { return { ... state , endLabel : label , endDateError : 'Make sure the start comes before the end.' , } ; } return { ... state , endLabel : label , endDateError : null , draft : [ draftStart , newEndDate ] , } ; case 'SELECT_DATE' : { const [ draftStart , draftEnd ] = state . draft ; const [ hoverStart , hoverEnd ] = state . hoverDraft ; if ( draftStart && draftEnd && action . payload > draftStart && action . payload < draftEnd ) { return { ... state , draft : [ action . payload , null ] , startLabel : null , startDateError : null , endLabel : null , endDateError : null , } ; } return { ... state , draft : [ hoverStart ? hoverStart : draftStart , hoverEnd ? hoverEnd : draftEnd , ] , startLabel : null , startDateError : null , endLabel : null , endDateError : null , } ; } case 'SET_DATE_FROM_PRESET' : { const [ start , end ] = action . payload ; return { ... state , draft : [ start , end ? end : start ] , startLabel : null , startDateError : null , endLabel : null , endDateError : null , } ; } case 'HOVER_DATE' : { const [ draftStart , draftEnd ] = state . draft ; const hoverDate = action . payload ; if ( hoverDate === null ) { return { ... state , hoverDraft : [ null , null ] , } ; } if ( draftStart && ! draftEnd && hoverDate < draftStart ) { return { ... state , hoverDraft : [ hoverDate , draftStart ] , } ; } if ( ! draftStart || ( draftEnd && hoverDate < draftEnd ) ) { return { ... state , hoverDraft : [ hoverDate , null ] , } ; } if ( ( draftStart && ! draftEnd && hoverDate > draftStart ) || ( draftEnd && hoverDate > draftEnd ) ) { return { ... state , hoverDraft : [ null , hoverDate ] , } ; } return { ... state , hoverDraft : [ null , null ] , } ; } default : return state ; } }