@@ -16,7 +16,7 @@ import { maxBy, padStart } from "lodash";
1616import { useTranslation } from "next-i18next" ;
1717import { useLocalStorage } from "react-use" ;
1818import styled from "styled-components" ;
19- import { getLocalizationLang } from "common/src/helpers" ;
19+ import { filterNonNullable , getLocalizationLang } from "common/src/helpers" ;
2020import { fontBold , fontMedium , H4 } from "common/src/common/typography" ;
2121import ClientOnly from "common/src/ClientOnly" ;
2222import { ReservationUnitByPkType } from "common/types/gql-types" ;
@@ -214,6 +214,12 @@ const ActionWrapper = styled.div`
214214 justify-content: flex-end;
215215` ;
216216
217+ const dayMin = ( days : Array < Date | undefined > ) : Date | undefined => {
218+ return filterNonNullable ( days ) . reduce < Date | undefined > ( ( acc , day ) => {
219+ return acc ? ( isBefore ( acc , day ) ? acc : day ) : day ;
220+ } , undefined ) ;
221+ } ;
222+
217223const QuickReservation = ( {
218224 isSlotReservable,
219225 isReservationUnitReservable,
@@ -410,7 +416,7 @@ const QuickReservation = ({
410416
411417 // TODO refactor
412418 let nextAvailableTime : Date | null | undefined ;
413- for ( let i = 0 ; nextAvailableTime === undefined ; i ++ ) {
419+ for ( let i = 0 ; nextAvailableTime === undefined && i < 1000 ; i ++ ) {
414420 const day = addDays ( after , i ) ;
415421 day . setHours ( 0 , 0 , 0 , 0 ) ;
416422 const availableTimesForDay = availableTimes ( day , true ) ;
@@ -427,7 +433,7 @@ const QuickReservation = ({
427433 }
428434 }
429435
430- return nextAvailableTime ;
436+ return nextAvailableTime ?? null ;
431437 } ,
432438 [ availableTimes , reservationUnit ]
433439 ) ;
@@ -459,6 +465,23 @@ const QuickReservation = ({
459465 return null ;
460466 }
461467
468+ const lastPossibleReservationDate =
469+ reservationUnit . reservationsMaxDaysBefore != null &&
470+ reservationUnit . reservationsMaxDaysBefore > 0
471+ ? addDays ( new Date ( ) , reservationUnit . reservationsMaxDaysBefore )
472+ : undefined ;
473+ const reservationUnitNotReservable = reservationUnit . reservationEnds
474+ ? new Date ( reservationUnit . reservationEnds )
475+ : undefined ;
476+ const lastOpenDate = lastOpeningDate ?. date
477+ ? new Date ( lastOpeningDate . date )
478+ : new Date ( ) ;
479+ const lastPossibleDate = dayMin ( [
480+ reservationUnitNotReservable ,
481+ lastPossibleReservationDate ,
482+ lastOpenDate ,
483+ ] ) ;
484+
462485 return (
463486 < Wrapper id = { `quick-reservation-${ idPrefix } ` } >
464487 < Heading > { t ( "reservationCalendar:quickReservation.heading" ) } </ Heading >
@@ -480,9 +503,7 @@ const QuickReservation = ({
480503 } }
481504 value = { toUIDate ( date ) }
482505 minDate = { new Date ( ) }
483- maxDate = {
484- lastOpeningDate ?. date ? new Date ( lastOpeningDate . date ) : new Date ( )
485- }
506+ maxDate = { lastPossibleDate }
486507 />
487508 < StyledTimeInput
488509 key = { `timeInput-${ time } ` }
0 commit comments