2022-10-20 05:09:52 +00:00
|
|
|
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { GetStaticPaths, GetStaticProps } from "next";
|
|
|
|
import { useRouter } from "next/router";
|
2022-10-20 05:09:52 +00:00
|
|
|
import { Fragment } from "react";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { WipeMyCalActionButton } from "@calcom/app-store/wipemycalother/components";
|
2022-12-22 12:35:01 +00:00
|
|
|
import BookingLayout from "@calcom/features/bookings/layout/BookingLayout";
|
|
|
|
import { filterQuerySchema, useFilterQuery } from "@calcom/features/bookings/lib/useFilterQuery";
|
2022-08-24 20:18:42 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-12-22 12:35:01 +00:00
|
|
|
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
|
|
|
import { Alert, Button, EmptyScreen, Icon } from "@calcom/ui";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
import { useInViewObserver } from "@lib/hooks/useInViewObserver";
|
|
|
|
|
|
|
|
import BookingListItem from "@components/booking/BookingListItem";
|
2022-11-01 13:29:01 +00:00
|
|
|
import SkeletonLoader from "@components/booking/SkeletonLoader";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-12-07 20:53:44 +00:00
|
|
|
import { ssgInit } from "@server/lib/ssg";
|
|
|
|
|
2022-12-22 12:35:01 +00:00
|
|
|
type BookingListingStatus = z.infer<typeof filterQuerySchema>["status"];
|
2022-11-10 23:40:01 +00:00
|
|
|
type BookingOutput = RouterOutputs["viewer"]["bookings"]["get"]["bookings"][0];
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-12-02 10:08:12 +00:00
|
|
|
type RecurringInfo = {
|
|
|
|
recurringEventId: string | null;
|
|
|
|
count: number;
|
|
|
|
firstDate: Date | null;
|
|
|
|
bookings: { [key: string]: Date[] };
|
|
|
|
};
|
|
|
|
|
2022-09-22 07:48:27 +00:00
|
|
|
const validStatuses = ["upcoming", "recurring", "past", "cancelled", "unconfirmed"] as const;
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
const descriptionByStatus: Record<BookingListingStatus, string> = {
|
|
|
|
upcoming: "upcoming_bookings",
|
|
|
|
recurring: "recurring_bookings",
|
|
|
|
past: "past_bookings",
|
|
|
|
cancelled: "cancelled_bookings",
|
2022-09-22 07:48:27 +00:00
|
|
|
unconfirmed: "unconfirmed_bookings",
|
2022-08-24 20:18:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const querySchema = z.object({
|
|
|
|
status: z.enum(validStatuses),
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function Bookings() {
|
2022-12-22 12:35:01 +00:00
|
|
|
const { data: filterQuery } = useFilterQuery();
|
2022-08-24 20:18:42 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { status } = router.isReady ? querySchema.parse(router.query) : { status: "upcoming" as const };
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const query = trpc.viewer.bookings.get.useInfiniteQuery(
|
2022-12-22 12:35:01 +00:00
|
|
|
{
|
|
|
|
limit: 10,
|
|
|
|
filters: {
|
|
|
|
...filterQuery,
|
|
|
|
status: filterQuery.status ?? status,
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 23:40:01 +00:00
|
|
|
{
|
|
|
|
// first render has status `undefined`
|
|
|
|
enabled: router.isReady,
|
|
|
|
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
|
|
}
|
|
|
|
);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-09-12 10:15:30 +00:00
|
|
|
// Animate page (tab) tranistions to look smoothing
|
2022-10-20 05:09:52 +00:00
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
const buttonInView = useInViewObserver(() => {
|
|
|
|
if (!query.isFetching && query.hasNextPage && query.status === "success") {
|
|
|
|
query.fetchNextPage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const isEmpty = !query.data?.pages[0]?.bookings.length;
|
|
|
|
|
|
|
|
const shownBookings: Record<string, BookingOutput[]> = {};
|
|
|
|
const filterBookings = (booking: BookingOutput) => {
|
2022-10-06 19:23:22 +00:00
|
|
|
if (status === "recurring" || status == "unconfirmed" || status === "cancelled") {
|
2022-08-24 20:18:42 +00:00
|
|
|
if (!booking.recurringEventId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
shownBookings[booking.recurringEventId] !== undefined &&
|
|
|
|
shownBookings[booking.recurringEventId].length > 0
|
|
|
|
) {
|
|
|
|
shownBookings[booking.recurringEventId].push(booking);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
shownBookings[booking.recurringEventId] = [booking];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2022-09-12 10:15:30 +00:00
|
|
|
|
2022-12-02 10:08:12 +00:00
|
|
|
let recurringInfoToday: RecurringInfo | undefined;
|
|
|
|
|
|
|
|
const bookingsToday =
|
|
|
|
query.data?.pages.map((page) =>
|
|
|
|
page.bookings.filter((booking: BookingOutput) => {
|
|
|
|
recurringInfoToday = page.recurringInfo.find(
|
|
|
|
(info) => info.recurringEventId === booking.recurringEventId
|
|
|
|
);
|
|
|
|
return new Date(booking.startTime).toDateString() === new Date().toDateString();
|
|
|
|
})
|
|
|
|
)[0] || [];
|
|
|
|
|
2022-10-20 05:09:52 +00:00
|
|
|
const [animationParentRef] = useAutoAnimate<HTMLDivElement>();
|
2022-09-12 10:15:30 +00:00
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
return (
|
2022-09-05 19:06:34 +00:00
|
|
|
<BookingLayout heading={t("bookings")} subtitle={t("bookings_description")}>
|
2022-09-30 10:47:20 +00:00
|
|
|
<div className="flex w-full flex-col" ref={animationParentRef}>
|
2022-08-24 20:18:42 +00:00
|
|
|
{query.status === "error" && (
|
|
|
|
<Alert severity="error" title={t("something_went_wrong")} message={query.error.message} />
|
|
|
|
)}
|
2022-09-29 16:58:29 +00:00
|
|
|
{(query.status === "loading" || query.isPaused) && <SkeletonLoader />}
|
2022-08-24 20:18:42 +00:00
|
|
|
{query.status === "success" && !isEmpty && (
|
2022-12-02 10:08:12 +00:00
|
|
|
<>
|
|
|
|
{!!bookingsToday.length && status === "upcoming" && (
|
|
|
|
<div className="mb-6 pt-2 xl:pt-0">
|
|
|
|
<WipeMyCalActionButton bookingStatus={status} bookingsEmpty={isEmpty} />
|
|
|
|
<p className="mb-2 text-xs font-medium uppercase leading-4 text-gray-500">{t("today")}</p>
|
|
|
|
<div className="overflow-hidden rounded-md border border-gray-200">
|
|
|
|
<table className="w-full max-w-full table-fixed">
|
|
|
|
<tbody className="divide-y divide-gray-200 bg-white">
|
|
|
|
<Fragment>
|
|
|
|
{bookingsToday.map((booking: BookingOutput) => (
|
2022-11-04 16:43:02 +00:00
|
|
|
<BookingListItem
|
|
|
|
key={booking.id}
|
|
|
|
listingStatus={status}
|
2022-12-02 10:08:12 +00:00
|
|
|
recurringInfo={recurringInfoToday}
|
2022-11-04 16:43:02 +00:00
|
|
|
{...booking}
|
|
|
|
/>
|
2022-12-02 10:08:12 +00:00
|
|
|
))}
|
|
|
|
</Fragment>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="pt-2 xl:pt-0">
|
2022-12-26 10:41:28 +00:00
|
|
|
{/* <p className="mb-2 text-xs font-medium uppercase leading-4 text-gray-500">{t("all")}</p> */}
|
2022-12-02 10:08:12 +00:00
|
|
|
<div className="overflow-hidden rounded-md border border-gray-200">
|
|
|
|
<table className="w-full max-w-full table-fixed">
|
|
|
|
<tbody className="divide-y divide-gray-200 bg-white" data-testid="bookings">
|
|
|
|
{query.data.pages.map((page, index) => (
|
|
|
|
<Fragment key={index}>
|
|
|
|
{page.bookings.filter(filterBookings).map((booking: BookingOutput) => {
|
|
|
|
const recurringInfo = page.recurringInfo.find(
|
|
|
|
(info) => info.recurringEventId === booking.recurringEventId
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<BookingListItem
|
|
|
|
key={booking.id}
|
|
|
|
listingStatus={status}
|
|
|
|
recurringInfo={recurringInfo}
|
|
|
|
{...booking}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Fragment>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
<div className="p-4 text-center" ref={buttonInView.ref}>
|
|
|
|
<Button
|
|
|
|
color="minimal"
|
|
|
|
loading={query.isFetchingNextPage}
|
|
|
|
disabled={!query.hasNextPage}
|
|
|
|
onClick={() => query.fetchNextPage()}>
|
|
|
|
{query.hasNextPage ? t("load_more_results") : t("no_more_results")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-08-24 20:18:42 +00:00
|
|
|
</div>
|
2022-12-02 10:08:12 +00:00
|
|
|
</>
|
2022-08-24 20:18:42 +00:00
|
|
|
)}
|
|
|
|
{query.status === "success" && isEmpty && (
|
2022-12-22 12:35:01 +00:00
|
|
|
<div className="flex items-center justify-center pt-2 xl:pt-0">
|
2022-08-24 20:18:42 +00:00
|
|
|
<EmptyScreen
|
|
|
|
Icon={Icon.FiCalendar}
|
|
|
|
headline={t("no_status_bookings_yet", { status: t(status).toLowerCase() })}
|
|
|
|
description={t("no_status_bookings_yet_description", {
|
|
|
|
status: t(status).toLowerCase(),
|
|
|
|
description: t(descriptionByStatus[status]),
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</BookingLayout>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-07 20:53:44 +00:00
|
|
|
export const getStaticProps: GetStaticProps = async (ctx) => {
|
2022-08-24 20:18:42 +00:00
|
|
|
const params = querySchema.safeParse(ctx.params);
|
2022-12-07 20:53:44 +00:00
|
|
|
const ssg = await ssgInit(ctx);
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
if (!params.success) return { notFound: true };
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
status: params.data.status,
|
2022-12-07 20:53:44 +00:00
|
|
|
trpcState: ssg.dehydrate(),
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getStaticPaths: GetStaticPaths = () => {
|
|
|
|
return {
|
|
|
|
paths: validStatuses.map((status) => ({
|
|
|
|
params: { status },
|
|
|
|
locale: "en",
|
|
|
|
})),
|
|
|
|
fallback: "blocking",
|
|
|
|
};
|
|
|
|
};
|