[Feature]Booking Embed (#2227)

This commit is contained in:
Hariom Balhara authored and GitHub committed 2022-03-31 09:45:47 +01:00
1 parent 4e9c3be598
commit 4a58da62d6
39 files changed
+1740 -22

No files matched your search

+9 -2
View File
@@ -5,13 +5,15 @@ import dayjsBusinessTime from "dayjs-business-time";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import { memoize } from "lodash";
import { useEffect, useMemo, useRef, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { useEmbedStyles } from "@calcom/embed-core";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import classNames from "@lib/classNames";
import { timeZone } from "@lib/clock";
import { weekdayNames } from "@lib/core/i18n/weekday";
import { doWorkAsync } from "@lib/doWorkAsync";
import { useLocale } from "@lib/hooks/useLocale";
import getSlots from "@lib/slots";
import { WorkingHours } from "@lib/types/schedule";
@@ -85,6 +87,8 @@ function DatePicker({
}: DatePickerProps): JSX.Element {
const { i18n } = useLocale();
const [browsingDate, setBrowsingDate] = useState<Dayjs | null>(date);
const enabledDateButtonEmbedStyles = useEmbedStyles("enabledDateButton");
const disabledDateButtonEmbedStyles = useEmbedStyles("disabledDateButton");
const [month, setMonth] = useState<string>("");
const [year, setYear] = useState<string>("");
@@ -274,6 +278,9 @@ function DatePicker({
<button
onClick={() => onDatePicked(browsingDate.date(day.date))}
disabled={day.disabled}
style={
day.disabled ? { ...disabledDateButtonEmbedStyles } : { ...enabledDateButtonEmbedStyles }
}
className={classNames(
"absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm text-center",
"hover:border-brand hover:border dark:hover:border-white",
@@ -1,5 +1,4 @@
import React from "react";
import { Control } from "react-hook-form";
import BasePhoneInput, { Props } from "react-phone-number-input/react-hook-form";
import "react-phone-number-input/style.css";
+5
View File
@@ -1,4 +1,5 @@
import Head from "next/head";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { Maybe } from "@trpc/server";
@@ -27,6 +28,10 @@ function applyThemeAndAddListener(theme: string) {
// makes sure the ui doesn't flash
export default function useTheme(theme?: Maybe<string>) {
const [isReady, setIsReady] = useState(false);
const router = useRouter();
// Embed UI configuration takes more precedence over App Configuration
theme = (router.query.theme as string | null) || theme;
useEffect(() => {
// TODO: isReady doesn't seem required now. This is also impacting PSI Score for pages which are using isReady.
+1
View File
@@ -8,6 +8,7 @@ const withTM = require("next-transpile-modules")([
"@calcom/prisma",
"@calcom/stripe",
"@calcom/ui",
"@calcom/embed-core",
]);
const { i18n } = require("./next-i18next.config");
+11 -4
View File
@@ -8,7 +8,9 @@ import React, { useState } from "react";
import { Toaster } from "react-hot-toast";
import { JSONObject } from "superjson/dist/types";
import { useLocale } from "@lib/hooks/useLocale";
import { sdkActionManager, useEmbedStyles } from "@calcom/embed-core";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import useTheme from "@lib/hooks/useTheme";
import prisma from "@lib/prisma";
import { inferSSRProps } from "@lib/types/inferSSRProps";
@@ -30,6 +32,7 @@ export default function User(props: inferSSRProps<typeof getServerSideProps>) {
const { user, eventTypes } = props;
const { t } = useLocale();
const router = useRouter();
const eventTypeListItemEmbedStyles = useEmbedStyles("eventTypeListItem");
const query = { ...router.query };
delete query.user; // So it doesn't display in the Link (and make tests fail)
@@ -48,9 +51,9 @@ export default function User(props: inferSSRProps<typeof getServerSideProps>) {
<div className="h-screen dark:bg-neutral-900">
<main className="mx-auto max-w-3xl px-4 py-24">
<div className="mb-8 text-center">
<AvatarSSR user={user} className="mx-auto mb-4 h-24 w-24" alt={nameOrUsername}></AvatarSSR>
<AvatarSSR user={user} className="mx-auto mb-4 h-24 w-24" alt={nameOrUsername} />
<h1 className="font-cal mb-1 text-3xl text-neutral-900 dark:text-white">
{nameOrUsername}
<span>{nameOrUsername}</span>
{user.verified && (
<BadgeCheckIcon className="mx-1 -mt-1 inline h-6 w-6 text-blue-500 dark:text-white" />
)}
@@ -71,7 +74,7 @@ export default function User(props: inferSSRProps<typeof getServerSideProps>) {
eventTypes.map((type) => (
<div
key={type.id}
style={{ display: "flex" }}
style={{ display: "flex", ...eventTypeListItemEmbedStyles }}
className="hover:border-brand group relative rounded-sm border border-neutral-200 bg-white hover:bg-gray-50 dark:border-neutral-700 dark:bg-gray-800 dark:hover:border-neutral-600">
<ArrowRightIcon className="absolute right-3 top-3 h-4 w-4 text-black opacity-0 transition-opacity group-hover:opacity-100 dark:text-white" />
{/* Don't prefetch till the time we drop the amount of javascript in [user][type] page which is impacting score for [user] page */}
@@ -91,6 +94,10 @@ export default function User(props: inferSSRProps<typeof getServerSideProps>) {
"You must verify a wallet with a token belonging to the specified smart contract first",
"error"
);
} else {
sdkActionManager?.fire("eventTypeSelected", {
eventType: type,
});
}
}}
className="block w-full px-6 py-4"
+2
View File
@@ -3,6 +3,8 @@ import Head from "next/head";
// import { ReactQueryDevtools } from "react-query/devtools";
import superjson from "superjson";
import "@calcom/embed-core/src/embed-iframe";
import AppProviders, { AppProps } from "@lib/app-providers";
import { seoConfig } from "@lib/config/next-seo.config";
+6 -2
View File
@@ -5,10 +5,12 @@ type Props = Record<string, unknown> & DocumentProps;
class MyDocument extends Document<Props> {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
const isEmbed = ctx.req?.url?.includes("embed");
return { ...initialProps, isEmbed };
}
render() {
const props = this.props;
const { locale } = this.props.__NEXT_DATA__;
const dir = locale === "ar" || locale === "he" ? "rtl" : "ltr";
@@ -23,7 +25,9 @@ class MyDocument extends Document<Props> {
<meta name="msapplication-TileColor" content="#ff0000" />
<meta name="theme-color" content="#ffffff" />
</Head>
<body className="bg-gray-100 dark:bg-neutral-900">
{/* Keep the embed hidden till parent initializes and gives it the appropriate styles */}
<body className="bg-gray-100 dark:bg-neutral-900" style={props.isEmbed ? { display: "none" } : {}}>
<Main />
<NextScript />
</body>
+27 -8
View File
@@ -10,12 +10,13 @@ import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { sdkActionManager } from "@calcom/embed-core";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import Button from "@calcom/ui/Button";
import { EmailInput } from "@calcom/ui/form/fields";
import { asStringOrThrow, asStringOrNull } from "@lib/asStringOrNull";
import { getEventName } from "@lib/event";
import { useLocale } from "@lib/hooks/useLocale";
import useTheme from "@lib/hooks/useTheme";
import { isBrandingHidden } from "@lib/isBrandingHidden";
import prisma from "@lib/prisma";
@@ -40,11 +41,7 @@ export default function Success(props: inferSSRProps<typeof getServerSideProps>)
const [date, setDate] = useState(dayjs.utc(asStringOrThrow(router.query.date)));
const { isReady, Theme } = useTheme(props.profile.theme);
useEffect(() => {
setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()));
setIs24h(!!localStorage.getItem("timeOption.is24hClock"));
}, []);
const { eventType } = props;
const attendeeName = typeof name === "string" ? name : "Nameless";
@@ -57,6 +54,26 @@ export default function Success(props: inferSSRProps<typeof getServerSideProps>)
};
const eventName = getEventName(eventNameObject);
const needsConfirmation = eventType.requiresConfirmation && reschedule != "true";
useEffect(() => {
const users = eventType.users;
// TODO: We should probably make it consistent with Webhook payload. Some data is not available here, as and when requirement comes we can add
sdkActionManager!.fire("bookingSuccessful", {
eventType,
date: date.toString(),
duration: eventType.length,
organizer: {
name: users[0].name || "Nameless",
email: users[0].email || "Email-less",
timeZone: users[0].timeZone,
},
confirmed: !needsConfirmation,
// TODO: Add payment details
});
setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess()));
setIs24h(!!localStorage.getItem("timeOption.is24hClock"));
}, [eventType, needsConfirmation]);
function eventLink(): string {
const optional: { location?: string } = {};
@@ -87,8 +104,6 @@ export default function Success(props: inferSSRProps<typeof getServerSideProps>)
return encodeURIComponent(event.value ? event.value : false);
}
const needsConfirmation = props.eventType.requiresConfirmation && reschedule != "true";
return (
(isReady && (
<div className="h-screen bg-neutral-100 dark:bg-neutral-900" data-testid="success-page">
@@ -322,6 +337,8 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
theme: true,
brandColor: true,
darkBrandColor: true,
email: true,
timeZone: true,
},
},
team: {
@@ -351,6 +368,8 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
theme: true,
brandColor: true,
darkBrandColor: true,
email: true,
timeZone: true,
},
});
if (user) {