11import { filterNonNullable } from "common/src/helpers" ;
2- import { addDays , addMinutes , isAfter , isBefore } from "date-fns" ;
2+ import { addDays , addMinutes , isAfter , isBefore , set } from "date-fns" ;
33import {
44 ReservableTimeSpanType ,
55 ReservationUnitNode ,
@@ -121,8 +121,8 @@ const getAvailableTimesForDay = ({
121121} ;
122122
123123// Returns the next available time, after the given time (Date object)
124- const getNextAvailableTime = ( props : AvailableTimesProps ) : Date | null => {
125- const { start, reservationUnit, slots , activeApplicationRounds } = props ;
124+ function getNextAvailableTime ( props : AvailableTimesProps ) : Date | null {
125+ const { start, reservationUnit } = props ;
126126 if ( reservationUnit == null ) {
127127 return null ;
128128 }
@@ -140,6 +140,7 @@ const getNextAvailableTime = (props: AvailableTimesProps): Date | null => {
140140 const interval = filterNonNullable ( reservableTimeSpans ) . find ( ( x ) =>
141141 isInTimeSpan ( minDay , x )
142142 ) ;
143+
143144 if ( ! interval ?. startDatetime || ! interval . endDatetime ) {
144145 return null ;
145146 }
@@ -155,33 +156,24 @@ const getNextAvailableTime = (props: AvailableTimesProps): Date | null => {
155156
156157 // Find the first possible time for that day, continue for each day until we find one
157158 for ( const singleDay of days ) {
158- const availableTimesForDay =
159- slots ??
160- ( getAvailableTimesForDay ( {
161- ...props ,
162- start : singleDay ,
163- fromStartOfDay : true ,
164- reservationUnit,
165- activeApplicationRounds,
166- } ) as ReservableTimeSpanType [ ] ) ;
167- if (
168- availableTimesForDay . length > 0 &&
169- ! ! availableTimesForDay [ 0 ] . startDatetime
170- ) {
171- const [ hours , minutes ] = availableTimesForDay [ 0 ] . startDatetime
172- ? availableTimesForDay [ 0 ] . startDatetime
173- . toString ( )
174- . split ( "T" ) [ 1 ]
175- . split ( ":" )
176- . map ( Number )
177- : [ 0 , 0 ] ;
178- singleDay . setHours ( hours , minutes , 0 , 0 ) ;
179- return singleDay ;
159+ // have to run this complex check to remove already reserved times
160+ const availableTimesForDay = getAvailableTimesForDay ( {
161+ ...props ,
162+ start : singleDay ,
163+ } ) ;
164+ const hasAvailableTimes = availableTimesForDay . length > 0 ;
165+ if ( hasAvailableTimes ) {
166+ const startDatetime = availableTimesForDay [ 0 ] ;
167+ const [ hours , minutes ] = startDatetime
168+ . split ( ":" )
169+ . map ( Number )
170+ . filter ( Number . isFinite ) ;
171+ return set ( singleDay , { hours, minutes } ) ;
180172 }
181173 }
182174
183175 return null ;
184- } ;
176+ }
185177
186178const isSlotReservable = (
187179 start : Date ,
0 commit comments