@@ -4,48 +4,23 @@ import {
44 ReservationTypeChoice ,
55 useReservationTimesInReservationUnitQuery ,
66} from "@gql/gql-types" ;
7- import { toApiDate } from "common/src/common/util" ;
8- import { addDays } from "date-fns" ;
7+ import { isValidDate , toApiDate } from "common/src/common/util" ;
8+ import { addDays , addMinutes , startOfDay } from "date-fns" ;
99import {
1010 type CollisionInterval ,
1111 doesIntervalCollide ,
1212 reservationToInterval ,
1313} from "@/helpers" ;
1414import { type NewReservationListItem } from "@/component/ReservationsList" ;
15- import { convertToDate } from "../utils" ;
16- import { base64encode , filterNonNullable } from "common/src/helpers" ;
15+ import {
16+ base64encode ,
17+ filterNonNullable ,
18+ timeToMinutes ,
19+ } from "common/src/helpers" ;
1720import { RELATED_RESERVATION_STATES } from "common/src/const" ;
1821import { gql } from "@apollo/client" ;
1922import { errorToast } from "common/src/common/toast" ;
2023
21- // TODO this is only used for RecurringReservationForm, why? (the above query + hook also)
22-
23- function listItemToInterval (
24- item : NewReservationListItem ,
25- type : ReservationTypeChoice
26- ) : CollisionInterval | null {
27- const start = convertToDate ( item . date , item . startTime ) ;
28- const end = convertToDate ( item . date , item . endTime ) ;
29- if ( start && end ) {
30- return {
31- start,
32- end,
33- buffers : {
34- before :
35- type !== ReservationTypeChoice . Blocked
36- ? ( item . buffers ?. before ?? 0 )
37- : 0 ,
38- after :
39- type !== ReservationTypeChoice . Blocked
40- ? ( item . buffers ?. after ?? 0 )
41- : 0 ,
42- } ,
43- type,
44- } ;
45- }
46- return null ;
47- }
48-
4924// TODO there is multiples of these fragments (for each Calendar), should be unified
5025const RESERVATIONS_IN_INTERVAL_FRAGMENT = gql `
5126 fragment ReservationsInInterval on ReservationNode {
@@ -148,17 +123,23 @@ function useReservationsInInterval({
148123 return { reservations, loading, refetch } ;
149124}
150125
126+ // Could also be reworked to work the other way around, return list of collisions, not list of items
127+ // TODO this should be debounced because keypress message handler is taking 200ms+
151128export function useFilteredReservationList ( {
152129 items,
153130 reservationUnitPk,
154131 begin,
155132 end,
133+ startTime,
134+ endTime,
156135 reservationType,
157136} : {
158137 items : NewReservationListItem [ ] ;
159138 reservationUnitPk ?: number ;
160139 begin : Date ;
161140 end : Date ;
141+ startTime : string ;
142+ endTime : string ;
162143 reservationType : ReservationTypeChoice ;
163144} ) {
164145 const { reservations, refetch } = useReservationsInInterval ( {
@@ -172,12 +153,24 @@ export function useFilteredReservationList({
172153 if ( reservations . length === 0 ) {
173154 return items ;
174155 }
156+ if ( items . length === 0 ) {
157+ return [ ] ;
158+ }
159+
160+ const startMin = timeToMinutes ( startTime ) ;
161+ const endMin = timeToMinutes ( endTime ) ;
175162 const isReservationInsideRange = (
176163 reservationToMake : NewReservationListItem ,
177164 interval : CollisionInterval
178165 ) => {
179166 const type = interval . type ?? ReservationTypeChoice . Blocked ;
180- const interval2 = listItemToInterval ( reservationToMake , type ) ;
167+ const interval2 = listItemToInterval (
168+ reservationToMake . date ,
169+ startMin ,
170+ endMin ,
171+ type ,
172+ reservationToMake . buffers
173+ ) ;
181174 if ( interval2 && interval ) {
182175 return doesIntervalCollide ( interval2 , interval ) ;
183176 }
@@ -189,7 +182,35 @@ export function useFilteredReservationList({
189182 ? { ...x , isOverlapping : true }
190183 : x
191184 ) ;
192- } , [ items , reservations ] ) ;
185+ } , [ items , reservations , startTime , endTime ] ) ;
193186
194187 return { reservations : data , refetch } ;
195188}
189+
190+ // unsafe
191+ function listItemToInterval (
192+ date : Date ,
193+ startTime : number ,
194+ endTime : number ,
195+ type : ReservationTypeChoice ,
196+ buffers ?: { before ?: number ; after ?: number }
197+ ) : CollisionInterval {
198+ const start = addMinutes ( startOfDay ( date ) , startTime ) ;
199+ const end = addMinutes ( startOfDay ( date ) , endTime ) ;
200+ if ( ! isValidDate ( start ) && ! isValidDate ( end ) ) {
201+ throw new Error ( "Invalid date" ) ;
202+ }
203+ const before =
204+ type !== ReservationTypeChoice . Blocked ? ( buffers ?. before ?? 0 ) : 0 ;
205+ const after =
206+ type !== ReservationTypeChoice . Blocked ? ( buffers ?. after ?? 0 ) : 0 ;
207+ return {
208+ start,
209+ end,
210+ buffers : {
211+ before,
212+ after,
213+ } ,
214+ type,
215+ } ;
216+ }
0 commit comments