import prisma from "@lib/prisma"; import { useSession } from "next-auth/client"; import Shell from "@components/Shell"; import { useRouter } from "next/router"; import dayjs from "dayjs"; import { Fragment } from "react"; // TODO: replace headlessui with radix-ui import { Menu, Transition } from "@headlessui/react"; import { DotsHorizontalIcon } from "@heroicons/react/solid"; import classNames from "@lib/classNames"; import { ClockIcon, CalendarIcon, XIcon, CheckIcon, BanIcon } from "@heroicons/react/outline"; import Loader from "@components/Loader"; import { Button } from "@components/ui/Button"; import { getSession } from "@lib/auth"; import { BookingStatus, User } from "@prisma/client"; import EmptyScreen from "@components/EmptyScreen"; export default function Bookings({ bookings }) { // eslint-disable-next-line @typescript-eslint/no-unused-vars const [session, loading] = useSession(); const isEmpty = Object.keys(bookings).length === 0; const router = useRouter(); if (loading) { return ; } async function confirmBookingHandler(booking, confirm: boolean) { const res = await fetch("/api/book/confirm", { method: "PATCH", body: JSON.stringify({ id: booking.id, confirmed: confirm }), headers: { "Content-Type": "application/json", }, }); if (res.ok) { await router.replace(router.asPath); } } return (
{isEmpty ? ( ) : (
{bookings .filter((booking) => booking.status !== BookingStatus.CANCELLED) .map((booking) => ( ))}
{!booking.confirmed && !booking.rejected && ( Unconfirmed )}
{booking.eventType?.team && {booking.eventType.team.name}: } {booking.title}
{dayjs(booking.startTime).format("D MMMM YYYY")}:{" "} {dayjs(booking.startTime).format("HH:mm")} -{" "} {dayjs(booking.endTime).format("HH:mm")}
{booking.attendees.length !== 0 && ( )}
{dayjs(booking.startTime).format("D MMMM YYYY")}
{dayjs(booking.startTime).format("HH:mm")} -{" "} {dayjs(booking.endTime).format("HH:mm")}
{!booking.confirmed && !booking.rejected && ( <>
{({ open }) => ( <>
Open options
{({ active }) => ( confirmBookingHandler(booking, true)} className={classNames( active ? "bg-neutral-100 text-neutral-900" : "text-neutral-700", "group flex items-center px-4 py-2 text-sm font-medium" )}> )} {({ active }) => ( confirmBookingHandler(booking, false)} className={classNames( active ? "bg-neutral-100 text-neutral-900" : "text-neutral-700", "group flex items-center px-4 py-2 text-sm w-full font-medium" )}> )}
)}
)} {booking.confirmed && !booking.rejected && ( <>
{({ open }) => ( <>
Open options
{({ active }) => ( )} {({ active }) => ( )}
)}
)} {!booking.confirmed && booking.rejected && (
Rejected
)}
)}
); } export async function getServerSideProps(context) { const session = await getSession(context); if (!session) { return { redirect: { permanent: false, destination: "/auth/login" } }; } const user: User = await prisma.user.findUnique({ where: { id: session.user.id, }, select: { email: true, }, }); const b = await prisma.booking.findMany({ where: { OR: [ { userId: session.user.id, }, { attendees: { some: { email: user.email, }, }, }, ], }, select: { uid: true, title: true, description: true, attendees: true, confirmed: true, rejected: true, id: true, startTime: true, endTime: true, eventType: { select: { team: { select: { name: true, }, }, }, }, status: true, }, orderBy: { startTime: "asc", }, }); const bookings = b.reverse().map((booking) => { return { ...booking, startTime: booking.startTime.toISOString(), endTime: booking.endTime.toISOString() }; }); return { props: { bookings } }; }