Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 2ca71d6

Browse files
committed
fix: invalid date causing browser to freeze
1 parent 8fd0fdd commit 2ca71d6

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

apps/ui/components/reservation-unit/QuickReservation.tsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { maxBy, padStart } from "lodash";
1616
import { useTranslation } from "next-i18next";
1717
import { useLocalStorage } from "react-use";
1818
import styled from "styled-components";
19-
import { getLocalizationLang } from "common/src/helpers";
19+
import { filterNonNullable, getLocalizationLang } from "common/src/helpers";
2020
import { fontBold, fontMedium, H4 } from "common/src/common/typography";
2121
import ClientOnly from "common/src/ClientOnly";
2222
import { 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+
217223
const 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

Comments
 (0)