import { CalendarIcon, ClockIcon, CreditCardIcon, ExclamationIcon, LocationMarkerIcon, } from "@heroicons/react/solid"; import { EventTypeCustomInputType } from "@prisma/client"; import dayjs from "dayjs"; import Head from "next/head"; import { useRouter } from "next/router"; import { useEffect, useMemo, useState } from "react"; import { Controller, useForm, useWatch } from "react-hook-form"; import { FormattedNumber, IntlProvider } from "react-intl"; import { ReactMultiEmail } from "react-multi-email"; import { useMutation } from "react-query"; import { createPaymentLink } from "@ee/lib/stripe/client"; import { asStringOrNull } from "@lib/asStringOrNull"; import { timeZone } from "@lib/clock"; import { ensureArray } from "@lib/ensureArray"; import { useLocale } from "@lib/hooks/useLocale"; import useTheme from "@lib/hooks/useTheme"; import { LocationType } from "@lib/location"; import createBooking from "@lib/mutations/bookings/create-booking"; import { parseZone } from "@lib/parseZone"; import slugify from "@lib/slugify"; import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@lib/telemetry"; import CustomBranding from "@components/CustomBranding"; import { Form } from "@components/form/fields"; import AvatarGroup from "@components/ui/AvatarGroup"; import { Button } from "@components/ui/Button"; import PhoneInput from "@components/ui/form/PhoneInput"; import { BookPageProps } from "../../../pages/[user]/book"; import { TeamBookingPageProps } from "../../../pages/team/[slug]/book"; type BookingPageProps = BookPageProps | TeamBookingPageProps; const BookingPage = (props: BookingPageProps) => { const { t, i18n } = useLocale(); const router = useRouter(); /* * This was too optimistic * I started, then I remembered what a beast book/event.ts is * Gave up shortly after. One day. Maybe. * const mutation = trpc.useMutation("viewer.bookEvent", { onSuccess: ({ booking }) => { // go to success page. }, });*/ const mutation = useMutation(createBooking, { onSuccess: async ({ attendees, paymentUid, ...responseData }) => { if (paymentUid) { return await router.push( createPaymentLink({ paymentUid, date, name: attendees[0].name, absolute: false, }) ); } const location = (function humanReadableLocation(location) { if (!location) { return; } if (location.includes("integration")) { return t("web_conferencing_details_to_follow"); } return location; })(responseData.location); return router.push({ pathname: "/success", query: { date, type: props.eventType.id, user: props.profile.slug, reschedule: !!rescheduleUid, name: attendees[0].name, email: attendees[0].email, location, }, }); }, }); const rescheduleUid = router.query.rescheduleUid as string; const { isReady } = useTheme(props.profile.theme); const date = asStringOrNull(router.query.date); const timeFormat = asStringOrNull(router.query.clock) === "24h" ? "H:mm" : "h:mma"; const [guestToggle, setGuestToggle] = useState(false); // it would be nice if Prisma at some point in the future allowed for Json; as of now this is not the case. const locations: { type: LocationType }[] = useMemo( () => (props.eventType.locations as { type: LocationType }[]) || [], [props.eventType.locations] ); useEffect(() => { if (router.query.guest) { setGuestToggle(true); } }, [router.query.guest]); const telemetry = useTelemetry(); useEffect(() => { telemetry.withJitsu((jitsu) => jitsu.track(telemetryEventTypes.timeSelected, collectPageParameters())); }, [telemetry]); const locationInfo = (type: LocationType) => locations.find((location) => location.type === type); // TODO: Move to translations const locationLabels = { [LocationType.InPerson]: t("in_person_meeting"), [LocationType.Phone]: t("phone_call"), [LocationType.GoogleMeet]: "Google Meet", [LocationType.Zoom]: "Zoom Video", [LocationType.Daily]: "Daily.co Video", }; type BookingFormValues = { name: string; email: string; notes?: string; locationType?: LocationType; guests?: string[]; phone?: string; customInputs?: { [key: string]: string; }; }; const bookingForm = useForm({ defaultValues: { name: (router.query.name as string) || "", email: (router.query.email as string) || "", notes: (router.query.notes as string) || "", guests: ensureArray(router.query.guest), customInputs: props.eventType.customInputs.reduce( (customInputs, input) => ({ ...customInputs, [input.id]: router.query[slugify(input.label)], }), {} ), }, }); const selectedLocation = useWatch({ control: bookingForm.control, name: "locationType", defaultValue: ((): LocationType | undefined => { if (router.query.location) { return router.query.location as LocationType; } if (locations.length === 1) { return locations[0]?.type; } })(), }); const getLocationValue = (booking: Pick) => { const { locationType } = booking; switch (locationType) { case LocationType.Phone: { return booking.phone; } case LocationType.InPerson: { return locationInfo(locationType).address; } // Catches all other location types, such as Google Meet, Zoom etc. default: return selectedLocation; } }; const bookEvent = (booking: BookingFormValues) => { telemetry.withJitsu((jitsu) => jitsu.track(telemetryEventTypes.bookingConfirmed, collectPageParameters()) ); // "metadata" is a reserved key to allow for connecting external users without relying on the email address. // <...url>&metadata[user_id]=123 will be send as a custom input field as the hidden type. const metadata = Object.keys(router.query) .filter((key) => key.startsWith("metadata")) .reduce( (metadata, key) => ({ ...metadata, [key.substring("metadata[".length, key.length - 1)]: router.query[key], }), {} ); mutation.mutate({ ...booking, start: dayjs(date).format(), end: dayjs(date).add(props.eventType.length, "minute").format(), eventTypeId: props.eventType.id, timeZone: timeZone(), language: i18n.language, rescheduleUid, user: router.query.user, location: getLocationValue(booking), metadata, customInputs: Object.keys(booking.customInputs || {}).map((inputId) => ({ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion label: props.eventType.customInputs.find((input) => input.id === parseInt(inputId))!.label, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion value: booking.customInputs![inputId], })), }); }; return (
{rescheduleUid ? t("booking_reschedule_confirmation", { eventTypeTitle: props.eventType.title, profileName: props.profile.name, }) : t("booking_confirmation", { eventTypeTitle: props.eventType.title, profileName: props.profile.name, })}{" "} | Cal.com
{isReady && (
user.name !== props.profile.name) .map((user) => ({ image: user.avatar, title: user.name, })) )} />

{props.profile.name}

{props.eventType.title}

{props.eventType.length} {t("minutes")}

{props.eventType.price > 0 && (

)} {selectedLocation === LocationType.InPerson && (

{locationInfo(selectedLocation).address}

)}

{parseZone(date).format(timeFormat + ", dddd DD MMMM YYYY")}

{props.eventType.description}

{locations.length > 1 && (
{t("location")} {locations.map((location, i) => ( ))}
)} {selectedLocation === LocationType.Phone && (
)} {props.eventType.customInputs .sort((a, b) => a.id - b.id) .map((input) => (
{input.type !== EventTypeCustomInputType.BOOL && ( )} {input.type === EventTypeCustomInputType.TEXTLONG && (