From 68f05ce2c77791b38cd828811bb13624dc5328a4 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Mon, 17 Oct 2022 18:28:57 +0100 Subject: [PATCH] Uses utcOffset to display bookings according to current time & timeZone (#5050) --- .../components/booking/BookingListItem.tsx | 23 ++-- apps/web/lib/parseDate.ts | 17 ++- apps/web/pages/success.tsx | 100 ++++++++++-------- 3 files changed, 83 insertions(+), 57 deletions(-) diff --git a/apps/web/components/booking/BookingListItem.tsx b/apps/web/components/booking/BookingListItem.tsx index 03923695ef..62974df16f 100644 --- a/apps/web/components/booking/BookingListItem.tsx +++ b/apps/web/components/booking/BookingListItem.tsx @@ -206,6 +206,9 @@ function BookingListItem(booking: BookingItemProps) { }, }); }; + + const utcOffset = dayjs().tz(user?.timeZone).utcOffset(); + return ( <>
{startTime}
- {dayjs(booking.startTime) - .tz(user?.timeZone) + {dayjs + .utc(booking.startTime) + .utcOffset(utcOffset) .format(user && user.timeFormat === 12 ? "h:mma" : "HH:mm")}{" "} -{" "} - {dayjs(booking.endTime) - .tz(user?.timeZone) + {dayjs + .utc(booking.endTime) + .utcOffset(utcOffset) .format(user && user.timeFormat === 12 ? "h:mma" : "HH:mm")}
@@ -299,12 +304,14 @@ function BookingListItem(booking: BookingItemProps) {
{startTime}
- {dayjs(booking.startTime) - .tz(user?.timeZone) + {dayjs + .utc(booking.startTime) + .utcOffset(utcOffset) .format(user && user.timeFormat === 12 ? "h:mma" : "HH:mm")}{" "} -{" "} - {dayjs(booking.endTime) - .tz(user?.timeZone) + {dayjs + .utc(booking.endTime) + .utcOffset(utcOffset) .format(user && user.timeFormat === 12 ? "h:mma" : "HH:mm")}
diff --git a/apps/web/lib/parseDate.ts b/apps/web/lib/parseDate.ts index 2f8d50a224..601d5e7ec5 100644 --- a/apps/web/lib/parseDate.ts +++ b/apps/web/lib/parseDate.ts @@ -39,10 +39,20 @@ export const parseRecurringDates = ( const rule = new RRule({ ...restRecurringEvent, count: recurringCount, - dtstart: dayjs(startDate).toDate(), + dtstart: new Date( + Date.UTC( + dayjs.utc(startDate).get("year"), + dayjs.utc(startDate).get("month"), + dayjs.utc(startDate).get("date"), + dayjs.utc(startDate).get("hour"), + dayjs.utc(startDate).get("minute") + ) + ), }); + + const utcOffset = dayjs(startDate).tz(timeZone).utcOffset(); const dateStrings = rule.all().map((r) => { - return processDate(dayjs(r).tz(timeZone), i18n); + return processDate(dayjs.utc(r).utcOffset(utcOffset), i18n); }); return [dateStrings, rule.all()]; }; @@ -66,8 +76,9 @@ export const extractRecurringDates = ( count: recurringInfo?._count.recurringEventId, dtstart: recurringInfo?._min.startTime, }).all(); + const utcOffset = dayjs(recurringInfo?._min.startTime).tz(timeZone).utcOffset(); const dateStrings = allDates.map((r) => { - return processDate(dayjs(r).tz(timeZone), i18n); + return processDate(dayjs.utc(r).utcOffset(utcOffset), i18n); }); return [dateStrings, allDates]; }; diff --git a/apps/web/pages/success.tsx b/apps/web/pages/success.tsx index 3d15fb2366..907bdc77ce 100644 --- a/apps/web/pages/success.tsx +++ b/apps/web/pages/success.tsx @@ -620,52 +620,60 @@ export function RecurringBookings({ ? recurringBookings.sort((a, b) => (dayjs(a).isAfter(dayjs(b)) ? 1 : -1)) : null; - return recurringBookingsSorted && listingStatus === "recurring" ? ( - <> - {eventType.recurringEvent?.count && ( - - {getEveryFreqFor({ - t, - recurringEvent: eventType.recurringEvent, - recurringCount: recurringBookings?.length ?? undefined, - })} - - )} - {eventType.recurringEvent?.count && - recurringBookingsSorted.slice(0, 4).map((dateStr, idx) => ( -
- {dayjs(dateStr).format("MMMM DD, YYYY")} -
- {dayjs(dateStr).format("LT")} - {dayjs(dateStr).add(eventType.length, "m").format("LT")}{" "} - - ({localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()}) - -
- ))} - {recurringBookingsSorted.length > 4 && ( - setMoreEventsVisible(!moreEventsVisible)}> - - {t("plus_more", { count: recurringBookingsSorted.length - 4 })} - - - {eventType.recurringEvent?.count && - recurringBookingsSorted.slice(4).map((dateStr, idx) => ( -
- {dayjs(dateStr).format("MMMM DD, YYYY")} -
- {dayjs(dateStr).format("LT")} - {dayjs(dateStr).add(eventType.length, "m").format("LT")}{" "} - - ({localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()}) - -
- ))} -
-
- )} - - ) : ( + if (recurringBookingsSorted && listingStatus === "recurring") { + // recurring bookings should only be adjusted to the start date. + const utcOffset = dayjs(recurringBookingsSorted[0]).utcOffset(); + return ( + <> + {eventType.recurringEvent?.count && ( + + {getEveryFreqFor({ + t, + recurringEvent: eventType.recurringEvent, + recurringCount: recurringBookings?.length ?? undefined, + })} + + )} + {eventType.recurringEvent?.count && + recurringBookingsSorted.slice(0, 4).map((dateStr, idx) => ( +
+ {dayjs(dateStr).utcOffset(utcOffset).format("MMMM DD, YYYY")} +
+ {dayjs(dateStr).utcOffset(utcOffset).format("LT")} -{" "} + {dayjs(dateStr).utcOffset(utcOffset).add(eventType.length, "m").format("LT")}{" "} + + ({localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()}) + +
+ ))} + {recurringBookingsSorted.length > 4 && ( + setMoreEventsVisible(!moreEventsVisible)}> + + {t("plus_more", { count: recurringBookingsSorted.length - 4 })} + + + {eventType.recurringEvent?.count && + recurringBookingsSorted.slice(4).map((dateStr, idx) => ( +
+ {dayjs(dateStr).utcOffset(utcOffset).format("MMMM DD, YYYY")} +
+ {dayjs(dateStr).utcOffset(utcOffset).format("LT")} -{" "} + {dayjs(dateStr).utcOffset(utcOffset).add(eventType.length, "m").format("LT")}{" "} + + ({localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()}) + +
+ ))} +
+
+ )} + + ); + } + + return ( <> {date.format("MMMM DD, YYYY")}