2023-08-22 12:18:36 +00:00
|
|
|
import type { EventType } from "@prisma/client";
|
2023-08-02 09:35:48 +00:00
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
2023-04-11 02:20:15 +00:00
|
|
|
|
2023-08-22 12:18:36 +00:00
|
|
|
import type { PaymentPageProps } from "@calcom/ee/payments/pages/payment";
|
|
|
|
import type { BookingResponse } from "@calcom/features/bookings/types";
|
|
|
|
|
2023-08-02 09:35:48 +00:00
|
|
|
function getNewSeachParams(args: {
|
2023-04-11 02:20:15 +00:00
|
|
|
query: Record<string, string | null | undefined | boolean>;
|
2023-08-02 09:35:48 +00:00
|
|
|
searchParams?: URLSearchParams;
|
|
|
|
}) {
|
|
|
|
const { query, searchParams } = args;
|
|
|
|
const newSearchParams = new URLSearchParams(searchParams);
|
|
|
|
Object.entries(query).forEach(([key, value]) => {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newSearchParams.append(key, String(value));
|
|
|
|
});
|
|
|
|
return newSearchParams;
|
|
|
|
}
|
2023-04-11 02:20:15 +00:00
|
|
|
|
2023-08-22 12:18:36 +00:00
|
|
|
type SuccessRedirectBookingType = Pick<
|
|
|
|
BookingResponse | PaymentPageProps["booking"],
|
|
|
|
"uid" | "title" | "description" | "startTime" | "endTime" | "location"
|
|
|
|
>;
|
|
|
|
|
|
|
|
export const getBookingRedirectExtraParams = (booking: SuccessRedirectBookingType) => {
|
|
|
|
type BookingResponseKey = keyof SuccessRedirectBookingType;
|
|
|
|
const redirectQueryParamKeys: BookingResponseKey[] = [
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"startTime",
|
|
|
|
"endTime",
|
|
|
|
"location",
|
|
|
|
];
|
|
|
|
|
|
|
|
return (Object.keys(booking) as BookingResponseKey[])
|
|
|
|
.filter((key) => redirectQueryParamKeys.includes(key))
|
|
|
|
.reduce((obj, key) => ({ ...obj, [key]: booking[key] }), {});
|
|
|
|
};
|
|
|
|
|
2023-08-02 09:35:48 +00:00
|
|
|
export const useBookingSuccessRedirect = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
const searchParams = useSearchParams();
|
|
|
|
const bookingSuccessRedirect = ({
|
|
|
|
successRedirectUrl,
|
2023-04-11 02:20:15 +00:00
|
|
|
query,
|
2023-08-22 12:18:36 +00:00
|
|
|
booking,
|
2023-08-02 09:35:48 +00:00
|
|
|
}: {
|
|
|
|
successRedirectUrl: EventType["successRedirectUrl"];
|
|
|
|
query: Record<string, string | null | undefined | boolean>;
|
2023-08-22 12:18:36 +00:00
|
|
|
booking: SuccessRedirectBookingType;
|
2023-08-02 09:35:48 +00:00
|
|
|
}) => {
|
|
|
|
if (successRedirectUrl) {
|
|
|
|
const url = new URL(successRedirectUrl);
|
|
|
|
// Using parent ensures, Embed iframe would redirect outside of the iframe.
|
2023-08-22 12:18:36 +00:00
|
|
|
const bookingExtraParams = getBookingRedirectExtraParams(booking);
|
|
|
|
const newSearchParams = getNewSeachParams({
|
|
|
|
query: {
|
|
|
|
...query,
|
|
|
|
...bookingExtraParams,
|
|
|
|
},
|
|
|
|
searchParams,
|
|
|
|
});
|
2023-08-02 09:35:48 +00:00
|
|
|
window.parent.location.href = `${url.toString()}?${newSearchParams.toString()}`;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newSearchParams = getNewSeachParams({ query });
|
2023-08-22 12:18:36 +00:00
|
|
|
return router.push(`/booking/${booking.uid}?${newSearchParams.toString()}`);
|
2023-08-02 09:35:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bookingSuccessRedirect;
|
2023-04-11 02:20:15 +00:00
|
|
|
};
|