Files
calendar/packages/lib/bookingSuccessRedirect.ts
T
b7851e6e53 refactor: next/router hooks with next/navigation hooks (#9105)
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Bailey Pumfleet <bailey@pumfleet.co.uk>
Co-authored-by: Peer Richelsen <peer@cal.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
2023-08-02 11:35:48 +02:00

44 lines
1.4 KiB
TypeScript

import type { Booking, EventType } from "@prisma/client";
import { useRouter, useSearchParams } from "next/navigation";
function getNewSeachParams(args: {
query: Record<string, string | null | undefined | boolean>;
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;
}
export const useBookingSuccessRedirect = () => {
const router = useRouter();
const searchParams = useSearchParams();
const bookingSuccessRedirect = ({
successRedirectUrl,
bookingUid,
query,
}: {
successRedirectUrl: EventType["successRedirectUrl"];
bookingUid: Booking["uid"] | undefined;
query: Record<string, string | null | undefined | boolean>;
}) => {
if (successRedirectUrl) {
const url = new URL(successRedirectUrl);
// Using parent ensures, Embed iframe would redirect outside of the iframe.
const newSearchParams = getNewSeachParams({ query, searchParams });
window.parent.location.href = `${url.toString()}?${newSearchParams.toString()}`;
return;
}
const newSearchParams = getNewSeachParams({ query });
return router.push(`/booking/${bookingUid}?${newSearchParams.toString()}`);
};
return bookingSuccessRedirect;
};