Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

Fix bug when end time calculation is after midnight (#347)

Luke Vella071eeb23397c
diff --git a/src/components/forms/poll-options-form/month-calendar/month-calendar.tsx b/src/components/forms/poll-options-form/month-calendar/month-calendar.tsx
index ed9a213e7..bda8d3c9c 100644
--- a/src/components/forms/poll-options-form/month-calendar/month-calendar.tsx
+++ b/src/components/forms/poll-options-form/month-calendar/month-calendar.tsx
@@ -263,22 +263,29 @@ const MonthCalendar: React.VoidFunctionComponent<DateTimePickerProps> = ({
<TimePicker
value={startDate}
onChange={(newStart) => {
- const newEnd = dayjs(newStart)
- .add(duration, "minutes")
- .toDate();
+ let newEnd = dayjs(newStart).add(
+ duration,
+ "minutes",
+ );
+
+ if (!newEnd.isSame(newStart, "day")) {
+ newEnd = newEnd
+ .set("hour", 23)
+ .set("minute", 45);
+ }
// replace enter with updated start time
onChange([
...options.slice(0, index),
{
...option,
start: formatDateWithoutTz(newStart),
- end: formatDateWithoutTz(newEnd),
+ end: formatDateWithoutTz(newEnd.toDate()),
},
...options.slice(index + 1),
]);
onNavigate(newStart);
onChangeDuration(
- dayjs(newEnd).diff(newStart, "minutes"),
+ newEnd.diff(newStart, "minutes"),
);
}}
/>
diff --git a/src/components/forms/poll-options-form/month-calendar/time-picker.tsx b/src/components/forms/poll-options-form/month-calendar/time-picker.tsx
index 3f24cfdee..ad21e19d3 100644
--- a/src/components/forms/poll-options-form/month-calendar/time-picker.tsx
+++ b/src/components/forms/poll-options-form/month-calendar/time-picker.tsx
@@ -46,28 +46,29 @@ const TimePicker: React.VoidFunctionComponent<TimePickerProps> = ({
],
});
- const startFromDate = startFrom
- ? dayjs(startFrom)
- : dayjs(value).startOf("day");
+ const renderOptions = () => {
+ const startFromDate = startFrom
+ ? dayjs(startFrom)
+ : dayjs(value).startOf("day");
- const options: React.ReactNode[] = [];
- for (let i = 0; i < 96; i++) {
- const optionValue = startFromDate.add(i * 15, "minutes");
- if (!optionValue.isSame(value, "day")) {
- // we only support event that start and end on the same day for now
- // because react-big-calendar does not support events that span days
- break;
+ const options: React.ReactNode[] = [];
+ const startMinute =
+ startFromDate.get("hour") * 60 + startFromDate.get("minute");
+ const intervals = Math.floor((1440 - startMinute) / 15);
+ for (let i = 0; i < intervals; i++) {
+ const optionValue = startFromDate.add(i * 15, "minutes");
+ options.push(
+ <Listbox.Option
+ key={i}
+ className={styleMenuItem}
+ value={optionValue.format("YYYY-MM-DDTHH:mm:ss")}
+ >
+ {optionValue.format("LT")}
+ </Listbox.Option>,
+ );
}
- options.push(
- <Listbox.Option
- key={i}
- className={styleMenuItem}
- value={optionValue.format("YYYY-MM-DDTHH:mm:ss")}
- >
- {optionValue.format("LT")}
- </Listbox.Option>,
- );
- }
+ return options;
+ };
return (
<Listbox
@@ -98,7 +99,7 @@ const TimePicker: React.VoidFunctionComponent<TimePickerProps> = ({
className="z-50 max-h-52 overflow-auto rounded-md bg-white py-1 shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none"
onMouseDown={stopPropagation}
>
- {options}
+ {renderOptions()}
</Listbox.Options>
) : null}
</FloatingPortal>