2021-09-22 19:52:38 +00:00
|
|
|
import { SchedulingType } from "@prisma/client";
|
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-03-27 19:48:13 +00:00
|
|
|
import { nameOfDay } from "@calcom/lib/weekday";
|
2022-06-27 21:01:46 +00:00
|
|
|
import { SkeletonContainer, SkeletonText } 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
|
|
|
|
2022-06-15 20:54:31 +00:00
|
|
|
import type { Slot } from "@server/routers/viewer/slots";
|
2021-06-19 22:50:47 +00:00
|
|
|
|
2021-09-23 17:18:29 +00:00
|
|
|
type AvailableTimesProps = {
|
|
|
|
timeFormat: string;
|
|
|
|
eventTypeId: number;
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringCount: number | undefined;
|
2022-04-06 17:20:30 +00:00
|
|
|
eventTypeSlug: string;
|
2021-09-23 17:18:29 +00:00
|
|
|
date: Dayjs;
|
|
|
|
users: {
|
|
|
|
username: string | null;
|
|
|
|
}[];
|
|
|
|
schedulingType: SchedulingType | null;
|
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;
|
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,
|
2021-09-14 08:45:28 +00:00
|
|
|
schedulingType,
|
2022-05-24 13:19:12 +00:00
|
|
|
seatsPerTimeSlot,
|
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());
|
|
|
|
}, []);
|
|
|
|
|
2021-06-19 22:50:47 +00:00
|
|
|
return (
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="mt-8 flex flex-col text-center sm:mt-0 sm:w-1/3 sm:pl-4 md:-mb-5">
|
|
|
|
<div className="mb-4 text-left text-lg font-light text-gray-600">
|
2022-04-08 05:33:24 +00:00
|
|
|
<span className="text-bookingdarker w-1/2 dark:text-white">
|
2022-03-27 19:48:13 +00:00
|
|
|
<strong>{nameOfDay(i18n.language, Number(date.format("d")))}</strong>
|
2022-04-08 05:33:24 +00:00
|
|
|
<span className="text-bookinglight">
|
2021-12-20 11:55:49 +00:00
|
|
|
{date.format(", D ")}
|
|
|
|
{date.toDate().toLocaleString(i18n.language, { month: "long" })}
|
2021-10-08 11:43:48 +00:00
|
|
|
</span>
|
2021-08-12 17:10:57 +00:00
|
|
|
</span>
|
2021-06-19 22:50:47 +00:00
|
|
|
</div>
|
2022-06-23 18:36:23 +00:00
|
|
|
<div className="grid flex-grow grid-cols-2 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 = {
|
2021-09-22 14:44:38 +00:00
|
|
|
pathname: "book",
|
|
|
|
query: {
|
|
|
|
...router.query,
|
2022-06-15 20:54:31 +00:00
|
|
|
date: dayjs(slot.time).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,
|
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
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
if (schedulingType === SchedulingType.ROUND_ROBIN) {
|
|
|
|
bookingUrl.query.user = slot.users;
|
|
|
|
}
|
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-05-24 13:19:12 +00:00
|
|
|
"text-primary-500 mb-2 block rounded-sm border bg-white py-4 font-medium opacity-25 dark:border-transparent dark:bg-gray-600 dark:text-neutral-200 ",
|
2021-12-15 10:26:39 +00:00
|
|
|
brand === "#fff" || brand === "#ffffff" ? "border-brandcontrast" : "border-brand"
|
2022-05-24 13:19:12 +00:00
|
|
|
)}>
|
2022-06-15 20:54:31 +00:00
|
|
|
{dayjs(slot.time).tz(timeZone()).format(timeFormat)}
|
2022-06-07 16:10:30 +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(
|
|
|
|
"text-primary-500 hover:bg-brand hover:text-brandcontrast dark:hover:bg-darkmodebrand dark:hover:text-darkmodebrandcontrast mb-2 block rounded-sm border bg-white py-4 font-medium hover:text-white dark:border-transparent dark:bg-gray-600 dark:text-neutral-200 dark:hover:border-black",
|
|
|
|
brand === "#fff" || brand === "#ffffff" ? "border-brandcontrast" : "border-brand"
|
|
|
|
)}
|
|
|
|
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">
|
|
|
|
<SkeletonText width="full" height="20" />
|
|
|
|
</SkeletonContainer>
|
|
|
|
<SkeletonContainer className="mb-2">
|
|
|
|
<SkeletonText width="full" height="20" />
|
|
|
|
</SkeletonContainer>
|
|
|
|
<SkeletonContainer className="mb-2">
|
|
|
|
<SkeletonText width="full" height="20" />
|
|
|
|
</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
|
|
|
|
2021-06-22 15:19:28 +00:00
|
|
|
export default AvailableTimes;
|