2022-07-27 00:45:10 +00:00
|
|
|
import { GetStaticPaths, GetStaticProps } from "next";
|
2021-09-29 21:33:18 +00:00
|
|
|
import { useRouter } from "next/router";
|
2021-10-28 15:02:22 +00:00
|
|
|
import { Fragment } from "react";
|
2022-07-25 20:22:56 +00:00
|
|
|
import { z } from "zod";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-04-15 02:24:27 +00:00
|
|
|
import { WipeMyCalActionButton } from "@calcom/app-store/wipemycalother/components";
|
2022-04-14 21:25:24 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { inferQueryInput, inferQueryOutput, trpc } from "@calcom/trpc/react";
|
2022-03-16 23:36:43 +00:00
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
|
|
|
import Button from "@calcom/ui/Button";
|
2022-06-01 17:24:41 +00:00
|
|
|
import EmptyScreen from "@calcom/ui/EmptyScreen";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-07-28 19:58:26 +00:00
|
|
|
import Shell from "@calcom/ui/Shell";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2021-10-28 15:02:22 +00:00
|
|
|
import { useInViewObserver } from "@lib/hooks/useInViewObserver";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
|
|
|
import BookingsShell from "@components/BookingsShell";
|
2021-09-30 10:46:39 +00:00
|
|
|
import BookingListItem from "@components/booking/BookingListItem";
|
2022-04-25 17:01:51 +00:00
|
|
|
import SkeletonLoader from "@components/booking/SkeletonLoader";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2021-09-30 10:46:39 +00:00
|
|
|
type BookingListingStatus = inferQueryInput<"viewer.bookings">["status"];
|
2022-05-05 21:16:25 +00:00
|
|
|
type BookingOutput = inferQueryOutput<"viewer.bookings">["bookings"][0];
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-07-27 00:45:10 +00:00
|
|
|
const validStatuses = ["upcoming", "recurring", "past", "cancelled"] as const;
|
|
|
|
|
2022-07-25 20:22:56 +00:00
|
|
|
const descriptionByStatus: Record<BookingListingStatus, string> = {
|
|
|
|
upcoming: "upcoming_bookings",
|
|
|
|
recurring: "recurring_bookings",
|
|
|
|
past: "past_bookings",
|
|
|
|
cancelled: "cancelled_bookings",
|
|
|
|
};
|
|
|
|
|
|
|
|
const querySchema = z.object({
|
2022-07-27 00:45:10 +00:00
|
|
|
status: z.enum(validStatuses),
|
2022-07-25 20:22:56 +00:00
|
|
|
});
|
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
export default function Bookings() {
|
2021-10-28 15:02:22 +00:00
|
|
|
const router = useRouter();
|
2022-07-25 20:22:56 +00:00
|
|
|
const { status } = router.isReady ? querySchema.parse(router.query) : { status: "upcoming" as const };
|
2021-10-13 10:49:15 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
|
2021-10-28 15:02:22 +00:00
|
|
|
const query = trpc.useInfiniteQuery(["viewer.bookings", { status, limit: 10 }], {
|
2021-10-14 10:57:49 +00:00
|
|
|
// first render has status `undefined`
|
2022-07-25 20:22:56 +00:00
|
|
|
enabled: router.isReady,
|
2021-10-28 15:02:22 +00:00
|
|
|
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
|
|
});
|
|
|
|
|
|
|
|
const buttonInView = useInViewObserver(() => {
|
|
|
|
if (!query.isFetching && query.hasNextPage && query.status === "success") {
|
|
|
|
query.fetchNextPage();
|
|
|
|
}
|
2021-10-14 10:57:49 +00:00
|
|
|
});
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2021-10-28 16:12:30 +00:00
|
|
|
const isEmpty = !query.data?.pages[0]?.bookings.length;
|
|
|
|
|
2022-07-19 19:06:23 +00:00
|
|
|
// Get all recurring events of the series with the same recurringEventId
|
|
|
|
const defineRecurrentBookings = (
|
|
|
|
booking: BookingOutput,
|
|
|
|
groupedBookings: Record<string, BookingOutput[]>
|
|
|
|
) => {
|
|
|
|
let recurringBookings = undefined;
|
2022-05-05 21:16:25 +00:00
|
|
|
if (booking.recurringEventId !== null) {
|
2022-07-19 19:06:23 +00:00
|
|
|
recurringBookings = groupedBookings[booking.recurringEventId];
|
2022-05-05 21:16:25 +00:00
|
|
|
}
|
2022-07-19 19:06:23 +00:00
|
|
|
return { recurringBookings };
|
2022-05-05 21:16:25 +00:00
|
|
|
};
|
2022-07-19 19:06:23 +00:00
|
|
|
const shownBookings: Record<string, BookingOutput[]> = {};
|
2022-06-10 20:38:06 +00:00
|
|
|
const filterBookings = (booking: BookingOutput) => {
|
|
|
|
if (status === "recurring" || status === "cancelled") {
|
|
|
|
if (!booking.recurringEventId) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-19 19:06:23 +00:00
|
|
|
if (
|
|
|
|
shownBookings[booking.recurringEventId] !== undefined &&
|
|
|
|
shownBookings[booking.recurringEventId].length > 0
|
|
|
|
) {
|
|
|
|
shownBookings[booking.recurringEventId].push(booking);
|
2022-06-10 20:38:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-19 19:06:23 +00:00
|
|
|
shownBookings[booking.recurringEventId] = [booking];
|
2022-06-10 20:38:06 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2021-09-29 21:33:18 +00:00
|
|
|
return (
|
2022-09-05 19:06:34 +00:00
|
|
|
<Shell heading={t("bookings")} subtitle={t("bookings_description")}>
|
2022-07-22 17:27:06 +00:00
|
|
|
<WipeMyCalActionButton bookingStatus={status} bookingsEmpty={isEmpty} />
|
2021-09-29 21:33:18 +00:00
|
|
|
<BookingsShell>
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="-mx-4 flex flex-col sm:mx-auto">
|
2021-09-29 21:33:18 +00:00
|
|
|
<div className="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
2021-10-28 15:02:22 +00:00
|
|
|
<div className="inline-block min-w-full py-2 align-middle sm:px-6 lg:px-8">
|
|
|
|
{query.status === "error" && (
|
|
|
|
<Alert severity="error" title={t("something_went_wrong")} message={query.error.message} />
|
|
|
|
)}
|
2022-04-25 17:01:51 +00:00
|
|
|
{(query.status === "loading" || query.status === "idle") && <SkeletonLoader />}
|
2021-10-28 16:12:30 +00:00
|
|
|
{query.status === "success" && !isEmpty && (
|
2021-10-28 15:02:22 +00:00
|
|
|
<>
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="mt-6 overflow-hidden rounded-sm border border-b border-gray-200">
|
2021-09-29 21:33:18 +00:00
|
|
|
<table className="min-w-full divide-y divide-gray-200">
|
2022-02-09 00:05:13 +00:00
|
|
|
<tbody className="divide-y divide-gray-200 bg-white" data-testid="bookings">
|
2021-10-28 15:02:22 +00:00
|
|
|
{query.data.pages.map((page, index) => (
|
|
|
|
<Fragment key={index}>
|
2022-07-19 19:06:23 +00:00
|
|
|
{page.bookings.filter(filterBookings).map((booking: BookingOutput) => (
|
2022-05-05 21:16:25 +00:00
|
|
|
<BookingListItem
|
|
|
|
key={booking.id}
|
|
|
|
listingStatus={status}
|
2022-07-19 19:06:23 +00:00
|
|
|
{...defineRecurrentBookings(booking, shownBookings)}
|
2022-05-05 21:16:25 +00:00
|
|
|
{...booking}
|
|
|
|
/>
|
2021-10-28 15:02:22 +00:00
|
|
|
))}
|
|
|
|
</Fragment>
|
2021-09-29 21:33:18 +00:00
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="p-4 text-center" ref={buttonInView.ref}>
|
2021-10-28 15:02:22 +00:00
|
|
|
<Button
|
2022-04-29 17:17:34 +00:00
|
|
|
color="minimal"
|
2021-10-28 15:02:22 +00:00
|
|
|
loading={query.isFetchingNextPage}
|
|
|
|
disabled={!query.hasNextPage}
|
|
|
|
onClick={() => query.fetchNextPage()}>
|
|
|
|
{query.hasNextPage ? t("load_more_results") : t("no_more_results")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2021-10-28 16:12:30 +00:00
|
|
|
{query.status === "success" && isEmpty && (
|
2021-10-28 15:02:22 +00:00
|
|
|
<EmptyScreen
|
2022-08-03 16:01:29 +00:00
|
|
|
Icon={Icon.FiCalendar}
|
2022-06-10 09:36:07 +00:00
|
|
|
headline={t("no_status_bookings_yet", { status: t(status).toLowerCase() })}
|
2021-10-28 15:02:22 +00:00
|
|
|
description={t("no_status_bookings_yet_description", {
|
2022-06-10 09:36:07 +00:00
|
|
|
status: t(status).toLowerCase(),
|
2022-07-25 20:22:56 +00:00
|
|
|
description: t(descriptionByStatus[status]),
|
2021-10-28 15:02:22 +00:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
2021-09-29 21:33:18 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</BookingsShell>
|
|
|
|
</Shell>
|
|
|
|
);
|
|
|
|
}
|
2022-07-27 00:45:10 +00:00
|
|
|
|
|
|
|
export const getStaticProps: GetStaticProps = (ctx) => {
|
|
|
|
const params = querySchema.safeParse(ctx.params);
|
|
|
|
|
|
|
|
if (!params.success) return { notFound: true };
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
status: params.data.status,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getStaticPaths: GetStaticPaths = () => {
|
|
|
|
return {
|
|
|
|
paths: validStatuses.map((status) => ({
|
|
|
|
params: { status },
|
|
|
|
locale: "en",
|
|
|
|
})),
|
|
|
|
fallback: "blocking",
|
|
|
|
};
|
|
|
|
};
|