feat: display long durations in hours on booking (#12631)

Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
Mike ZhouandUdit Takkar authored and GitHub committed 2023-12-02 18:27:11 +00:00
1 parent b2cdb08780
commit 0b9cf78e9b
26 files changed
+55 -2

No files matched your search

@@ -1,3 +1,4 @@
import type { TFunction } from "next-i18next";
import { useEffect } from "react";
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
@@ -7,6 +8,31 @@ import { Badge } from "@calcom/ui";
import type { PublicEvent } from "../../types";
/** Render X mins as X hours or X hours Y mins instead of in minutes once >= 60 minutes */
const getDurationFormatted = (mins: number, t: TFunction) => {
const hours = Math.floor(mins / 60);
mins %= 60;
// format minutes string
let minStr = "";
if (mins > 0) {
minStr =
mins === 1
? t("minute_one", { count: 1 })
: t("multiple_duration_timeUnit", { count: mins, unit: "minute" });
}
// format hours string
let hourStr = "";
if (hours > 0) {
hourStr =
hours === 1
? t("hour_one", { count: 1 })
: t("multiple_duration_timeUnit", { count: hours, unit: "hour" });
}
if (hourStr && minStr) return `${hourStr} ${minStr}`;
return hourStr || minStr;
};
export const EventDuration = ({ event }: { event: PublicEvent }) => {
const { t } = useLocale();
const [selectedDuration, setSelectedDuration, state] = useBookerStore((state) => [
@@ -25,7 +51,7 @@ export const EventDuration = ({ event }: { event: PublicEvent }) => {
}, [selectedDuration, setSelectedDuration, event.metadata?.multipleDuration, event.length, isDynamicEvent]);
if (!event?.metadata?.multipleDuration && !isDynamicEvent)
return <>{t("multiple_duration_mins", { count: event.length })}</>;
return <>{getDurationFormatted(event.length, t)}</>;
const durations = event?.metadata?.multipleDuration || [15, 30, 60];
@@ -39,7 +65,9 @@ export const EventDuration = ({ event }: { event: PublicEvent }) => {
className={classNames(selectedDuration === duration && "bg-brand-default text-brand")}
size="md"
key={duration}
onClick={() => setSelectedDuration(duration)}>{`${duration} ${t("minute_timeUnit")}`}</Badge>
onClick={() => setSelectedDuration(duration)}>
{getDurationFormatted(duration, t)}
</Badge>
))}
</div>
);