From 344f16d6ccf3f7f1907341babff0e262229ceae6 Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Tue, 20 Dec 2022 14:49:24 -0300 Subject: [PATCH] Fixing multiple duration default issues (#6114) * Fixing multiple duration default issues * Ensure that when calculation time slots we use 1 as a minimum interval, since 0 min meetings would result in an infinite loop. Co-authored-by: Jeroen Reumkens Co-authored-by: Peer Richelsen --- apps/web/components/eventtype/EventSetupTab.tsx | 2 ++ apps/web/pages/event-types/[type]/index.tsx | 2 +- packages/lib/slots.ts | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/web/components/eventtype/EventSetupTab.tsx b/apps/web/components/eventtype/EventSetupTab.tsx index e957442460..fae1b20c27 100644 --- a/apps/web/components/eventtype/EventSetupTab.tsx +++ b/apps/web/components/eventtype/EventSetupTab.tsx @@ -262,12 +262,14 @@ export const EventSetupTab = ( if (!newOptions.find((opt) => opt.value === defaultDuration?.value)) { if (newOptions.length > 0) { setDefaultDuration(newOptions[0]); + formMethods.setValue("length", newOptions[0].value); } else { setDefaultDuration(null); } } if (newOptions.length === 1 && defaultDuration === null) { setDefaultDuration(newOptions[0]); + formMethods.setValue("length", newOptions[0].value); } formMethods.setValue("metadata.multipleDuration", values); }} diff --git a/apps/web/pages/event-types/[type]/index.tsx b/apps/web/pages/event-types/[type]/index.tsx index f44d98edfb..6eccf9b98f 100644 --- a/apps/web/pages/event-types/[type]/index.tsx +++ b/apps/web/pages/event-types/[type]/index.tsx @@ -283,7 +283,7 @@ const EventTypePage = (props: inferSSRProps) => { if (metadata?.multipleDuration.length < 1) { throw new Error(t("event_setup_multiple_duration_error")); } else { - if (input.length && !metadata?.multipleDuration?.includes(input.length)) { + if (!input.length && !metadata?.multipleDuration?.includes(input.length)) { throw new Error(t("event_setup_multiple_duration_default_error")); } } diff --git a/packages/lib/slots.ts b/packages/lib/slots.ts index 11f143fba2..66df31328e 100644 --- a/packages/lib/slots.ts +++ b/packages/lib/slots.ts @@ -13,6 +13,8 @@ export type GetSlots = { }; export type TimeFrame = { startTime: number; endTime: number }; +const minimumOfOne = (input: number) => (input < 1 ? 1 : input); + /** * TODO: What does this function do? * Why is it needed? @@ -26,16 +28,24 @@ const splitAvailableTime = ( let initialTime = startTimeMinutes; const finalizationTime = endTimeMinutes; const result = [] as TimeFrame[]; + + // Ensure that both the frequency and event length are at least 1 minute, if they + // would be zero, we would have an infinite loop in this while! + const frequencyMinimumOne = minimumOfOne(frequency); + const eventLengthMinimumOne = minimumOfOne(eventLength); + while (initialTime < finalizationTime) { - const periodTime = initialTime + frequency; - const slotEndTime = initialTime + eventLength; + const periodTime = initialTime + frequencyMinimumOne; + const slotEndTime = initialTime + eventLengthMinimumOne; /* check if the slot end time surpasses availability end time of the user 1 minute is added to round up the hour mark so that end of the slot is considered in the check instead of x9 eg: if finalization time is 11:59, slotEndTime is 12:00, we ideally want the slot to be available */ if (slotEndTime <= finalizationTime + 1) result.push({ startTime: initialTime, endTime: periodTime }); - initialTime += frequency; + // Ensure that both the frequency and event length are at least 1 minute, if they + // would be zero, we would have an infinite loop in this while! + initialTime += frequencyMinimumOne; } return result; };