From 1758288948587a231c2b9beda4c4fd7590c6e88b Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Tue, 4 Apr 2023 14:22:27 +0530 Subject: [PATCH] Improvement: Do immediate redirect for "Redirect on Booking" (#7947) Co-authored-by: Alex van Andel --- .../components/eventtype/EventAdvancedTab.tsx | 31 ++--- apps/web/pages/booking/[uid].tsx | 131 +----------------- apps/web/public/static/locales/en/common.json | 1 + 3 files changed, 20 insertions(+), 143 deletions(-) diff --git a/apps/web/components/eventtype/EventAdvancedTab.tsx b/apps/web/components/eventtype/EventAdvancedTab.tsx index 06d3a5bd45..03a09ee2ab 100644 --- a/apps/web/components/eventtype/EventAdvancedTab.tsx +++ b/apps/web/components/eventtype/EventAdvancedTab.tsx @@ -10,11 +10,12 @@ import type { EventNameObjectType } from "@calcom/core/event"; import { getEventName } from "@calcom/core/event"; import DestinationCalendarSelector from "@calcom/features/calendars/DestinationCalendarSelector"; import { FormBuilder } from "@calcom/features/form-builder/FormBuilder"; -import { APP_NAME, CAL_URL, IS_SELF_HOSTED } from "@calcom/lib/constants"; +import { classNames } from "@calcom/lib"; +import { APP_NAME, CAL_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { Prisma } from "@calcom/prisma/client"; import { trpc } from "@calcom/trpc/react"; -import { Badge, Button, Checkbox, Label, SettingsToggle, showToast, TextField, Tooltip } from "@calcom/ui"; +import { Button, Checkbox, Label, SettingsToggle, showToast, TextField, Tooltip } from "@calcom/ui"; import { FiEdit, FiCopy } from "@calcom/ui/components/icon"; import RequiresConfirmationController from "./RequiresConfirmationController"; @@ -183,9 +184,10 @@ export const EventAdvancedTab = ({ eventType, team }: Pick - {/* Textfield has some margin by default we remove that so we can keep consitant aligment */} -
+ {/* Textfield has some margin by default we remove that so we can keep consistent alignment */} +
-
- - {/*TODO: Extract it out into a component when used more than once*/} - {!IS_SELF_HOSTED && ( - - - Platform Only - - - )} +
+ {t("redirect_url_warning")}
diff --git a/apps/web/pages/booking/[uid].tsx b/apps/web/pages/booking/[uid].tsx index 0abbcc68f6..e23402516d 100644 --- a/apps/web/pages/booking/[uid].tsx +++ b/apps/web/pages/booking/[uid].tsx @@ -6,7 +6,7 @@ import type { GetServerSidePropsContext } from "next"; import { useSession } from "next-auth/react"; import Link from "next/link"; import { useRouter } from "next/router"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useState } from "react"; import { RRule } from "rrule"; import { z } from "zod"; @@ -59,114 +59,6 @@ import EventReservationSchema from "@components/schemas/EventReservationSchema"; import { ssrInit } from "@server/lib/ssr"; -function redirectToExternalUrl(url: string) { - window.parent.location.href = url; -} - -/** - * Redirects to external URL with query params from current URL. - * Query Params and Hash Fragment if present in external URL are kept intact. - */ -function RedirectionToast({ url }: { url: string }) { - const [timeRemaining, setTimeRemaining] = useState(10); - const [isToastVisible, setIsToastVisible] = useState(true); - - const { t } = useLocale(); - const timerRef = useRef(null); - const router = useRouter(); - const { cancel: isCancellationMode } = querySchema.parse(router.query); - const urlWithSuccessParamsRef = useRef(); - if (isCancellationMode && timerRef.current) { - setIsToastVisible(false); - } - - useEffect(() => { - if (!isToastVisible && timerRef.current) { - window.clearInterval(timerRef.current); - } - }, [isToastVisible]); - - useEffect(() => { - const parsedExternalUrl = new URL(url); - - const parsedSuccessUrl = new URL(document.URL); - - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - /* @ts-ignore */ //https://stackoverflow.com/questions/49218765/typescript-and-iterator-type-iterableiteratort-is-not-an-array-type - for (const [name, value] of parsedExternalUrl.searchParams.entries()) { - parsedSuccessUrl.searchParams.set(name, value); - } - - const urlWithSuccessParams = - parsedExternalUrl.origin + - parsedExternalUrl.pathname + - "?" + - parsedSuccessUrl.searchParams.toString() + - parsedExternalUrl.hash; - urlWithSuccessParamsRef.current = urlWithSuccessParams; - - timerRef.current = window.setInterval(() => { - if (timeRemaining > 0) { - setTimeRemaining((timeRemaining) => { - return timeRemaining - 1; - }); - } else { - redirectToExternalUrl(urlWithSuccessParams); - window.clearInterval(timerRef.current as number); - } - }, 1000); - return () => { - window.clearInterval(timerRef.current as number); - }; - }, [timeRemaining, url]); - - if (!isToastVisible) { - return null; - } - - return ( - <> -
-
-
-
-
-

- Redirecting to {url} ... - - {t("you_are_being_redirected", { url, seconds: timeRemaining })} - -

-
-
- -
-
- -
-
-
-
-
- - ); -} - type SuccessProps = inferSSRProps; const stringToBoolean = z @@ -176,13 +68,14 @@ const stringToBoolean = z const querySchema = z.object({ uid: z.string(), - allRemainingBookings: stringToBoolean, + email: z.string().optional(), + eventTypeSlug: z.string().optional(), cancel: stringToBoolean, + allRemainingBookings: stringToBoolean, changes: stringToBoolean, reschedule: stringToBoolean, isSuccessBookingPage: stringToBoolean, formerTime: z.string().optional(), - email: z.string().optional(), seatReferenceUid: z.string().optional(), }); @@ -404,9 +297,6 @@ export default function Success(props: SuccessProps) {
- {isSuccessBookingPage && !isCancellationMode && eventType.successRedirectUrl ? ( - - ) : null}{" "}
{ }; }; -const schema = z.object({ - uid: z.string(), - email: z.string().optional(), - eventTypeSlug: z.string().optional(), - cancel: stringToBoolean, -}); - const handleSeatsEventTypeOnBooking = ( eventType: { seatsPerTimeSlot?: number | null; @@ -1017,9 +900,9 @@ const handleSeatsEventTypeOnBooking = ( export async function getServerSideProps(context: GetServerSidePropsContext) { const ssr = await ssrInit(context); - const parsedQuery = schema.safeParse(context.query); + const parsedQuery = querySchema.safeParse(context.query); if (!parsedQuery.success) return { notFound: true }; - const { uid, email, eventTypeSlug, cancel } = parsedQuery.data; + const { uid, email, eventTypeSlug, cancel, isSuccessBookingPage } = parsedQuery.data; const bookingInfoRaw = await prisma.booking.findFirst({ where: { @@ -1116,7 +999,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { customInputs: customInputSchema.array().parse(eventTypeRaw.customInputs), }; - if (eventType.metadata?.disableSuccessPage && eventType.successRedirectUrl && !cancel) { + if (eventType.successRedirectUrl && isSuccessBookingPage && !cancel) { return { redirect: { destination: eventType.successRedirectUrl, diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 07ca7483c5..78f18c0c3d 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -1689,6 +1689,7 @@ "events_rescheduled": "Events Rescheduled", "from_last_period": "from last period", "from_to_date_period": "From: {{startDate}} To: {{endDate}}", + "redirect_url_warning": "Adding a redirect will disable the success page. Make sure to mention \"Booking Confirmed\" on your custom success page.", "analytics_for_organisation": "Insights for {{organisationName}}", "subtitle_analytics": "Learn more about your team's activity", "event_trends": "Event Trends",