2021-06-19 22:50:47 +00:00
|
|
|
import Link from "next/link";
|
2021-06-24 22:15:18 +00:00
|
|
|
import { useRouter } from "next/router";
|
2022-06-15 20:54:31 +00:00
|
|
|
import { FC, useEffect, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs, { Dayjs } from "@calcom/dayjs";
|
2022-06-15 20:54:31 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-10-25 00:14:06 +00:00
|
|
|
import { TimeFormat } from "@calcom/lib/timeFormat";
|
2022-03-27 19:48:13 +00:00
|
|
|
import { nameOfDay } from "@calcom/lib/weekday";
|
2022-07-22 17:27:06 +00:00
|
|
|
import type { Slot } from "@calcom/trpc/server/routers/viewer/slots";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { SkeletonContainer, SkeletonText, ToggleGroup } from "@calcom/ui";
|
2022-03-27 19:48:13 +00:00
|
|
|
|
2021-12-15 10:26:39 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2022-05-05 18:51:22 +00:00
|
|
|
import { timeZone } from "@lib/clock";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-23 17:18:29 +00:00
|
|
|
type AvailableTimesProps = {
|
2022-10-25 00:14:06 +00:00
|
|
|
timeFormat: TimeFormat;
|
|
|
|
onTimeFormatChange: (is24Hour: boolean) => void;
|
2021-09-23 17:18:29 +00:00
|
|
|
eventTypeId: number;
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringCount: number | undefined;
|
2022-04-06 17:20:30 +00:00
|
|
|
eventTypeSlug: string;
|
2022-10-10 13:24:06 +00:00
|
|
|
date?: Dayjs;
|
2022-05-24 13:19:12 +00:00
|
|
|
seatsPerTimeSlot?: number | null;
|
2022-06-15 20:54:31 +00:00
|
|
|
slots?: Slot[];
|
2022-06-27 21:01:46 +00:00
|
|
|
isLoading: boolean;
|
2022-09-05 21:10:58 +00:00
|
|
|
ethSignature?: string;
|
2021-09-23 17:18:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const AvailableTimes: FC<AvailableTimesProps> = ({
|
2022-06-15 20:54:31 +00:00
|
|
|
slots = [],
|
2022-06-27 21:01:46 +00:00
|
|
|
isLoading,
|
2021-07-08 21:14:29 +00:00
|
|
|
date,
|
|
|
|
eventTypeId,
|
2022-04-06 17:20:30 +00:00
|
|
|
eventTypeSlug,
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringCount,
|
2021-07-08 21:14:29 +00:00
|
|
|
timeFormat,
|
2022-10-25 00:14:06 +00:00
|
|
|
onTimeFormatChange,
|
2022-05-24 13:19:12 +00:00
|
|
|
seatsPerTimeSlot,
|
2022-09-05 21:10:58 +00:00
|
|
|
ethSignature,
|
2021-07-08 21:14:29 +00:00
|
|
|
}) => {
|
2021-12-20 11:55:49 +00:00
|
|
|
const { t, i18n } = useLocale();
|
2021-06-19 22:50:47 +00:00
|
|
|
const router = useRouter();
|
2021-06-27 22:30:11 +00:00
|
|
|
const { rescheduleUid } = router.query;
|
2021-07-08 21:14:29 +00:00
|
|
|
|
2021-12-15 10:26:39 +00:00
|
|
|
const [brand, setBrand] = useState("#292929");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setBrand(getComputedStyle(document.documentElement).getPropertyValue("--brand-color").trim());
|
|
|
|
}, []);
|
|
|
|
|
2022-10-10 13:24:06 +00:00
|
|
|
if (!date) return null;
|
|
|
|
|
2021-06-19 22:50:47 +00:00
|
|
|
return (
|
2022-10-10 13:24:06 +00:00
|
|
|
<div className="dark:bg-darkgray-100 mt-8 flex h-full w-full flex-col px-4 text-center sm:mt-0 sm:p-5 md:-mb-5 md:min-w-[200px] lg:min-w-[300px]">
|
2022-10-25 00:14:06 +00:00
|
|
|
<div className="mb-6 flex items-center text-left text-base">
|
|
|
|
<div className="mr-4">
|
|
|
|
<span className="text-bookingdarker dark:text-darkgray-800 font-semibold text-gray-900">
|
|
|
|
{nameOfDay(i18n.language, Number(date.format("d")), "short")}
|
|
|
|
</span>
|
|
|
|
<span className="text-bookinglight font-medium">
|
2022-10-28 19:13:15 +00:00
|
|
|
, {date.toDate().toLocaleString(i18n.language, { month: "long" })} {date.format(" D ")}
|
2022-10-25 00:14:06 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div className="ml-auto">
|
|
|
|
<ToggleGroup
|
|
|
|
onValueChange={(timeFormat) => onTimeFormatChange(timeFormat === "24")}
|
|
|
|
defaultValue={timeFormat === TimeFormat.TWELVE_HOUR ? "12" : "24"}
|
|
|
|
options={[
|
|
|
|
{ value: "12", label: t("12_hour_short") },
|
|
|
|
{ value: "24", label: t("24_hour_short") },
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-06-19 22:50:47 +00:00
|
|
|
</div>
|
2022-10-14 18:04:54 +00:00
|
|
|
<div className="-mb-5 grid flex-grow grid-cols-1 gap-x-2 overflow-y-auto sm:block md:h-[364px]">
|
2022-06-27 21:01:46 +00:00
|
|
|
{slots.length > 0 &&
|
2021-09-22 14:44:38 +00:00
|
|
|
slots.map((slot) => {
|
2021-10-12 13:11:33 +00:00
|
|
|
type BookingURL = {
|
|
|
|
pathname: string;
|
|
|
|
query: Record<string, string | number | string[] | undefined>;
|
|
|
|
};
|
|
|
|
const bookingUrl: BookingURL = {
|
2022-10-19 21:25:03 +00:00
|
|
|
pathname: router.pathname.endsWith("/embed") ? "../book" : "book",
|
2021-09-22 14:44:38 +00:00
|
|
|
query: {
|
|
|
|
...router.query,
|
2022-11-04 14:59:38 +00:00
|
|
|
date: dayjs.utc(slot.time).tz(timeZone()).format(),
|
2021-09-22 14:44:38 +00:00
|
|
|
type: eventTypeId,
|
2022-04-06 17:20:30 +00:00
|
|
|
slug: eventTypeSlug,
|
2022-05-05 21:16:25 +00:00
|
|
|
/** Treat as recurring only when a count exist and it's not a rescheduling workflow */
|
|
|
|
count: recurringCount && !rescheduleUid ? recurringCount : undefined,
|
2022-09-05 21:10:58 +00:00
|
|
|
ethSignature,
|
2021-09-22 14:44:38 +00:00
|
|
|
},
|
|
|
|
};
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
if (rescheduleUid) {
|
2021-10-12 13:11:33 +00:00
|
|
|
bookingUrl.query.rescheduleUid = rescheduleUid as string;
|
2021-09-22 14:44:38 +00:00
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2022-05-24 13:19:12 +00:00
|
|
|
// If event already has an attendee add booking id
|
|
|
|
if (slot.bookingUid) {
|
|
|
|
bookingUrl.query.bookingUid = slot.bookingUid;
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
return (
|
2022-06-15 20:54:31 +00:00
|
|
|
<div key={dayjs(slot.time).format()}>
|
2022-05-24 13:19:12 +00:00
|
|
|
{/* Current there is no way to disable Next.js Links */}
|
|
|
|
{seatsPerTimeSlot && slot.attendees && slot.attendees >= seatsPerTimeSlot ? (
|
|
|
|
<div
|
2021-12-15 10:26:39 +00:00
|
|
|
className={classNames(
|
2022-08-30 11:51:08 +00:00
|
|
|
"text-primary-500 dark:bg-darkgray-200 dark:text-darkgray-900 mb-2 block rounded-sm border bg-white py-2 font-medium opacity-25 dark:border-transparent ",
|
|
|
|
brand === "#fff" || brand === "#ffffff" ? "" : ""
|
2022-05-24 13:19:12 +00:00
|
|
|
)}>
|
2022-06-15 20:54:31 +00:00
|
|
|
{dayjs(slot.time).tz(timeZone()).format(timeFormat)}
|
2022-07-12 17:50:04 +00:00
|
|
|
{!!seatsPerTimeSlot && <p className="text-sm">{t("booking_full")}</p>}
|
2022-05-24 13:19:12 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2022-06-28 02:17:23 +00:00
|
|
|
<Link href={bookingUrl} prefetch={false}>
|
2022-05-24 13:19:12 +00:00
|
|
|
<a
|
|
|
|
className={classNames(
|
2022-09-02 21:16:36 +00:00
|
|
|
"text-primary-500 hover:border-gray-900 hover:bg-gray-50",
|
|
|
|
"dark:bg-darkgray-200 dark:hover:bg-darkgray-300 dark:hover:border-darkmodebrand mb-2 block rounded-md border bg-white py-2 text-sm font-medium dark:border-transparent dark:text-neutral-200",
|
2022-08-30 11:51:08 +00:00
|
|
|
brand === "#fff" || brand === "#ffffff" ? "" : ""
|
2022-05-24 13:19:12 +00:00
|
|
|
)}
|
|
|
|
data-testid="time">
|
2022-06-15 20:54:31 +00:00
|
|
|
{dayjs(slot.time).tz(timeZone()).format(timeFormat)}
|
2022-06-07 16:10:30 +00:00
|
|
|
{!!seatsPerTimeSlot && (
|
2022-05-24 13:19:12 +00:00
|
|
|
<p
|
|
|
|
className={`${
|
|
|
|
slot.attendees && slot.attendees / seatsPerTimeSlot >= 0.8
|
|
|
|
? "text-rose-600"
|
|
|
|
: slot.attendees && slot.attendees / seatsPerTimeSlot >= 0.33
|
|
|
|
? "text-yellow-500"
|
|
|
|
: "text-emerald-400"
|
|
|
|
} text-sm`}>
|
|
|
|
{slot.attendees ? seatsPerTimeSlot - slot.attendees : seatsPerTimeSlot} /{" "}
|
|
|
|
{seatsPerTimeSlot} {t("seats_available")}
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)}
|
2021-09-22 14:44:38 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
2022-06-27 21:01:46 +00:00
|
|
|
|
|
|
|
{!isLoading && !slots.length && (
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="-mt-4 flex h-full w-full flex-col content-center items-center justify-center">
|
2021-10-08 11:43:48 +00:00
|
|
|
<h1 className="my-6 text-xl text-black dark:text-white">{t("all_booked_today")}</h1>
|
2021-09-22 14:44:38 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-06-27 21:01:46 +00:00
|
|
|
|
|
|
|
{isLoading && !slots.length && (
|
|
|
|
<>
|
|
|
|
<SkeletonContainer className="mb-2">
|
2022-11-14 10:31:36 +00:00
|
|
|
<SkeletonText className="h-5 w-full" />
|
2022-06-27 21:01:46 +00:00
|
|
|
</SkeletonContainer>
|
|
|
|
<SkeletonContainer className="mb-2">
|
2022-11-14 10:31:36 +00:00
|
|
|
<SkeletonText className="h-5 w-full" />
|
2022-06-27 21:01:46 +00:00
|
|
|
</SkeletonContainer>
|
|
|
|
<SkeletonContainer className="mb-2">
|
2022-11-14 10:31:36 +00:00
|
|
|
<SkeletonText className="h-5 w-full" />
|
2022-06-27 21:01:46 +00:00
|
|
|
</SkeletonContainer>
|
|
|
|
</>
|
|
|
|
)}
|
2021-09-22 14:44:38 +00:00
|
|
|
</div>
|
2021-06-19 22:50:47 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-06-24 22:15:18 +00:00
|
|
|
};
|
2021-06-19 22:50:47 +00:00
|
|
|
|
2022-10-10 13:24:06 +00:00
|
|
|
AvailableTimes.displayName = "AvailableTimes";
|
|
|
|
|
2021-06-22 15:19:28 +00:00
|
|
|
export default AvailableTimes;
|