2021-09-22 19:52:38 +00:00
|
|
|
import { ExclamationIcon } from "@heroicons/react/solid";
|
|
|
|
import { SchedulingType } from "@prisma/client";
|
2021-09-23 17:18:29 +00:00
|
|
|
import { Dayjs } from "dayjs";
|
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";
|
2021-12-15 10:26:39 +00:00
|
|
|
import React, { FC, useEffect, useState } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-12-15 10:26:39 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-10-08 11:43:48 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { useSlots } from "@lib/hooks/useSlots";
|
|
|
|
|
2021-08-04 19:39:59 +00:00
|
|
|
import Loader from "@components/Loader";
|
2021-06-19 22:50:47 +00:00
|
|
|
|
2021-09-23 17:18:29 +00:00
|
|
|
type AvailableTimesProps = {
|
|
|
|
timeFormat: string;
|
|
|
|
minimumBookingNotice: number;
|
|
|
|
eventTypeId: number;
|
|
|
|
eventLength: number;
|
|
|
|
date: Dayjs;
|
|
|
|
users: {
|
|
|
|
username: string | null;
|
|
|
|
}[];
|
|
|
|
schedulingType: SchedulingType | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AvailableTimes: FC<AvailableTimesProps> = ({
|
2021-07-08 21:14:29 +00:00
|
|
|
date,
|
|
|
|
eventLength,
|
|
|
|
eventTypeId,
|
2021-07-22 22:52:27 +00:00
|
|
|
minimumBookingNotice,
|
2021-07-08 21:14:29 +00:00
|
|
|
timeFormat,
|
2021-09-14 08:45:28 +00:00
|
|
|
users,
|
|
|
|
schedulingType,
|
2021-07-08 21:14:29 +00:00
|
|
|
}) => {
|
2021-10-12 13:11:33 +00:00
|
|
|
const { t } = 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-09-14 08:45:28 +00:00
|
|
|
const { slots, loading, error } = useSlots({
|
2021-07-08 21:14:29 +00:00
|
|
|
date,
|
|
|
|
eventLength,
|
2021-09-14 08:45:28 +00:00
|
|
|
schedulingType,
|
|
|
|
users,
|
2021-07-22 22:52:27 +00:00
|
|
|
minimumBookingNotice,
|
2021-09-23 17:18:29 +00:00
|
|
|
eventTypeId,
|
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 (
|
2021-12-06 11:37:15 +00:00
|
|
|
<div className="flex flex-col mt-8 text-center sm:pl-4 sm:mt-0 sm:w-1/3 md:-mb-5">
|
2021-11-18 01:03:19 +00:00
|
|
|
<div className="mb-4 text-lg font-light text-left text-gray-600">
|
|
|
|
<span className="w-1/2 text-gray-600 dark:text-white">
|
2021-10-08 11:43:48 +00:00
|
|
|
<strong>{t(date.format("dddd").toLowerCase())}</strong>
|
|
|
|
<span className="text-gray-500">
|
|
|
|
{date.format(", DD ")}
|
|
|
|
{t(date.format("MMMM").toLowerCase())}
|
|
|
|
</span>
|
2021-08-12 17:10:57 +00:00
|
|
|
</span>
|
2021-06-19 22:50:47 +00:00
|
|
|
</div>
|
2021-12-06 11:37:15 +00:00
|
|
|
<div className="flex-grow md:h-[364px] overflow-y-auto">
|
2021-09-22 14:44:38 +00:00
|
|
|
{!loading &&
|
|
|
|
slots?.length > 0 &&
|
|
|
|
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,
|
|
|
|
date: slot.time.format(),
|
|
|
|
type: eventTypeId,
|
|
|
|
},
|
|
|
|
};
|
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
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
return (
|
|
|
|
<div key={slot.time.format()}>
|
|
|
|
<Link href={bookingUrl}>
|
2021-10-18 21:07:06 +00:00
|
|
|
<a
|
2021-12-15 10:26:39 +00:00
|
|
|
className={classNames(
|
|
|
|
"block py-4 mb-2 font-medium bg-white border rounded-sm dark:bg-gray-600 text-primary-500 dark:text-neutral-200 dark:border-transparent hover:text-white hover:bg-brand hover:text-brandcontrast dark:hover:border-black dark:hover:bg-brand dark:hover:text-brandcontrast",
|
|
|
|
brand === "#fff" || brand === "#ffffff" ? "border-brandcontrast" : "border-brand"
|
|
|
|
)}
|
2021-10-18 21:07:06 +00:00
|
|
|
data-testid="time">
|
2021-09-22 14:44:38 +00:00
|
|
|
{slot.time.format(timeFormat)}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{!loading && !error && !slots.length && (
|
2021-11-18 01:03:19 +00:00
|
|
|
<div className="flex flex-col items-center content-center justify-center w-full h-full -mt-4">
|
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>
|
|
|
|
)}
|
2021-06-27 22:30:11 +00:00
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
{loading && <Loader />}
|
2021-06-27 22:30:11 +00:00
|
|
|
|
2021-09-22 14:44:38 +00:00
|
|
|
{error && (
|
2021-11-18 01:03:19 +00:00
|
|
|
<div className="p-4 border-l-4 border-yellow-400 bg-yellow-50">
|
2021-09-22 14:44:38 +00:00
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-shrink-0">
|
2021-11-18 01:03:19 +00:00
|
|
|
<ExclamationIcon className="w-5 h-5 text-yellow-400" aria-hidden="true" />
|
2021-09-22 14:44:38 +00:00
|
|
|
</div>
|
|
|
|
<div className="ml-3">
|
2021-10-08 11:43:48 +00:00
|
|
|
<p className="text-sm text-yellow-700">{t("slots_load_fail")}</p>
|
2021-09-22 14:44:38 +00:00
|
|
|
</div>
|
2021-06-27 22:30:11 +00:00
|
|
|
</div>
|
2021-06-27 22:02:27 +00:00
|
|
|
</div>
|
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;
|