cal.pub0.org/components/booking/BookingListItem.tsx

143 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-09-30 10:46:39 +00:00
import { BanIcon, CheckIcon, ClockIcon, XIcon } from "@heroicons/react/outline";
import { BookingStatus } from "@prisma/client";
import dayjs from "dayjs";
import { useMutation } from "react-query";
import { HttpError } from "@lib/core/http/error";
2021-10-15 10:53:42 +00:00
import { useLocale } from "@lib/hooks/useLocale";
2021-09-30 10:46:39 +00:00
import { inferQueryOutput, trpc } from "@lib/trpc";
import TableActions, { ActionType } from "@components/ui/TableActions";
2021-09-30 10:46:39 +00:00
type BookingItem = inferQueryOutput<"viewer.bookings">["bookings"][number];
2021-09-30 10:46:39 +00:00
function BookingListItem(booking: BookingItem) {
const { t, i18n } = useLocale();
2021-09-30 10:46:39 +00:00
const utils = trpc.useContext();
2021-09-30 10:46:39 +00:00
const mutation = useMutation(
async (confirm: boolean) => {
const res = await fetch("/api/book/confirm", {
method: "PATCH",
body: JSON.stringify({ id: booking.id, confirmed: confirm, language: i18n.language }),
2021-09-30 10:46:39 +00:00
headers: {
"Content-Type": "application/json",
},
});
if (!res.ok) {
throw new HttpError({ statusCode: res.status });
}
},
{
async onSettled() {
await utils.invalidateQueries(["viewer.bookings"]);
2021-09-30 10:46:39 +00:00
},
}
);
const isUpcoming = new Date(booking.endTime) >= new Date();
const isCancelled = booking.status === BookingStatus.CANCELLED;
const pendingActions: ActionType[] = [
2021-09-30 10:46:39 +00:00
{
id: "reject",
2021-10-15 10:53:42 +00:00
label: t("reject"),
2021-09-30 10:46:39 +00:00
onClick: () => mutation.mutate(false),
icon: BanIcon,
disabled: mutation.isLoading,
},
{
id: "confirm",
2021-10-15 10:53:42 +00:00
label: t("confirm"),
2021-09-30 10:46:39 +00:00
onClick: () => mutation.mutate(true),
icon: CheckIcon,
disabled: mutation.isLoading,
color: "primary",
},
];
const bookedActions: ActionType[] = [
2021-09-30 10:46:39 +00:00
{
id: "cancel",
2021-10-15 10:53:42 +00:00
label: t("cancel"),
2021-09-30 10:46:39 +00:00
href: `/cancel/${booking.uid}`,
icon: XIcon,
},
{
id: "reschedule",
2021-10-15 10:53:42 +00:00
label: t("reschedule"),
2021-09-30 10:46:39 +00:00
href: `/reschedule/${booking.uid}`,
icon: ClockIcon,
},
];
const startTime = dayjs(booking.startTime).format(isUpcoming ? "ddd, D MMM" : "D MMMM YYYY");
return (
<tr className="flex">
<td className="hidden py-4 pl-6 align-top sm:table-cell whitespace-nowrap">
2021-10-29 10:03:37 +00:00
<div className="text-sm leading-6 text-gray-900">{startTime}</div>
<div className="text-sm text-gray-500">
{dayjs(booking.startTime).format("HH:mm")} - {dayjs(booking.endTime).format("HH:mm")}
</div>
2021-09-30 10:46:39 +00:00
</td>
<td className={"pl-4 py-4 flex-1" + (booking.rejected ? " line-through" : "")}>
2021-09-30 10:46:39 +00:00
<div className="sm:hidden">
{!booking.confirmed && !booking.rejected && <Tag className="mb-2 mr-2">{t("unconfirmed")}</Tag>}
{!!booking?.eventType?.price && !booking.paid && <Tag className="mb-2 mr-2">Pending payment</Tag>}
2021-10-29 10:03:37 +00:00
<div className="text-sm font-medium text-gray-900">
2021-09-30 10:46:39 +00:00
{startTime}:{" "}
<small className="text-sm text-gray-500">
{dayjs(booking.startTime).format("HH:mm")} - {dayjs(booking.endTime).format("HH:mm")}
</small>
</div>
</div>
<div
title={booking.title}
className="text-sm font-medium leading-6 truncate text-neutral-900 max-w-56 md:max-w-max">
2021-09-30 10:46:39 +00:00
{booking.eventType?.team && <strong>{booking.eventType.team.name}: </strong>}
{booking.title}
{!!booking?.eventType?.price && !booking.paid && (
<Tag className="hidden ml-2 sm:inline-flex">Pending payment</Tag>
)}
{!booking.confirmed && !booking.rejected && (
<Tag className="hidden ml-2 sm:inline-flex">{t("unconfirmed")}</Tag>
)}
2021-09-30 10:46:39 +00:00
</div>
{booking.description && (
2021-10-29 10:03:37 +00:00
<div className="text-sm text-gray-500 truncate max-w-52 md:max-w-96" title={booking.description}>
2021-09-30 10:46:39 +00:00
&quot;{booking.description}&quot;
</div>
)}
{booking.attendees.length !== 0 && (
<div className="text-sm text-gray-900 hover:text-blue-500">
2021-09-30 10:46:39 +00:00
<a href={"mailto:" + booking.attendees[0].email}>{booking.attendees[0].email}</a>
</div>
)}
</td>
<td className="py-4 pr-4 text-sm font-medium text-right whitespace-nowrap">
2021-09-30 10:46:39 +00:00
{isUpcoming && !isCancelled ? (
<>
{!booking.confirmed && !booking.rejected && <TableActions actions={pendingActions} />}
{booking.confirmed && !booking.rejected && <TableActions actions={bookedActions} />}
2021-10-15 10:53:42 +00:00
{!booking.confirmed && booking.rejected && (
<div className="text-sm text-gray-500">{t("rejected")}</div>
)}
2021-09-30 10:46:39 +00:00
</>
) : null}
</td>
</tr>
);
}
const Tag = ({ children, className = "" }: React.PropsWithChildren<{ className?: string }>) => {
return (
<span
className={`inline-flex items-center px-1.5 py-0.5 rounded-sm text-xs font-medium bg-yellow-100 text-yellow-800 ${className}`}>
{children}
</span>
);
};
2021-09-30 10:46:39 +00:00
export default BookingListItem;