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-09-23 17:18:29 +00:00
|
|
|
import React, { FC } from "react";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
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 = {
|
|
|
|
workingHours: {
|
|
|
|
days: number[];
|
|
|
|
startTime: number;
|
|
|
|
endTime: number;
|
|
|
|
}[];
|
|
|
|
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
|
|
|
workingHours,
|
|
|
|
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,
|
2021-07-08 21:14:29 +00:00
|
|
|
workingHours,
|
2021-09-14 08:45:28 +00:00
|
|
|
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-06-19 22:50:47 +00:00
|
|
|
return (
|
2021-09-23 20:48:27 +00:00
|
|
|
<div className="sm:pl-4 mt-8 sm:mt-0 text-center sm:w-1/3 md:-mb-5">
|
2021-08-12 17:10:57 +00:00
|
|
|
<div className="text-gray-600 font-light text-lg mb-4 text-left">
|
|
|
|
<span className="w-1/2 dark:text-white text-gray-600">
|
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-09-22 14:44:38 +00:00
|
|
|
<div className="md:max-h-[364px] overflow-y-auto">
|
|
|
|
{!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}>
|
|
|
|
<a className="block font-medium mb-2 bg-white dark:bg-gray-600 text-primary-500 dark:text-neutral-200 border border-primary-500 dark:border-transparent rounded-sm hover:text-white hover:bg-primary-500 dark:hover:border-black py-4 dark:hover:bg-black">
|
|
|
|
{slot.time.format(timeFormat)}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
{!loading && !error && !slots.length && (
|
|
|
|
<div className="w-full h-full flex flex-col justify-center content-center items-center -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 && (
|
|
|
|
<div className="bg-yellow-50 border-l-4 border-yellow-400 p-4">
|
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<ExclamationIcon className="h-5 w-5 text-yellow-400" aria-hidden="true" />
|
|
|
|
</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;
|