import { useCallback, useState } from 'react'; import { Stack, Box, Typography, Button, useMediaQuery, Theme } from '@mui/material'; import OpenInNewIcon from '@mui/icons-material/OpenInNew'; import ChevronLeftIcon from '@mui/icons-material/ChevronLeft'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; import { CLIENT_PATHS } from '../../../config'; import RecordedDemoIllustration from '../../illustrations/RecordedDemoIllustration'; import { useUserSlots, convertDayToUTCString, useDealSlots, useTeamSlots, } from '../../../api/bookings'; import { Slot, TeamSlot } from '../../../api/bookings/types'; import { bookMeetingIntoExistingDeal, bookMeetingIntoLatestNonStartedDeal, bookMeetingIntoProduct, getBookingWidgetSource, } from '../../../api/deals/actions'; import { PublicRouting, RouteResult, RoutingOutcomeType } from '../../../api/routing/types'; import { useInjectLeadContext } from '../../../api/deals/hooks'; import BaseCalendarForm, { StyledDateCalendar } from './CalendarForm'; import { useWidgetStateContext } from '../../../state'; import { FormValues } from '../types'; import { BookingParams } from '../../../api/deals/types'; import { sendPixelEvent } from '../../../pixel'; dayjs.extend(utc); dayjs.extend(timezone); type CalendarFormProps = { onSubmit: (booking: BookingParams) => Promise; loading: boolean; }; function UserCalendarForm({ userId, onSubmit, loading }: { userId: string } & CalendarFormProps) { const [date, setDate] = useState(null); const resp = useUserSlots(userId, convertDayToUTCString(date), { keepPreviousData: true, }); const onConfirm = useCallback( async (slot: Slot) => { await onSubmit({ type: 'user', userId, startTime: slot.startTime, endTime: slot.endTime, }); }, [userId, onSubmit] ); return ( ); } function DealCalendarForm({ dealId, onSubmit, loading, }: { dealId: string; loading: boolean; onSubmit: (slot: { startTime: Date; endTime: Date }) => Promise; }) { const [date, setDate] = useState(null); const resp = useDealSlots(dealId, convertDayToUTCString(date), { keepPreviousData: true, }); const onConfirm = useCallback( async (slot: Slot) => { await onSubmit({ startTime: slot.startTime, endTime: slot.endTime, }); }, [onSubmit] ); return ( ); } function TeamCalendarForm({ teamId, onSubmit, loading }: { teamId: string } & CalendarFormProps) { const [date, setDate] = useState(null); const resp = useTeamSlots(teamId, convertDayToUTCString(date), { keepPreviousData: true, }); const onConfirm = useCallback( async (slot: TeamSlot) => { await onSubmit({ type: 'team', teamId, ...slot, }); }, [teamId, onSubmit] ); return ( ); } function BaseLink({ title, description, buttonText, href, }: { title: string; description: string; buttonText: string; href: string; }) { return ( {title} {description} ); } function RecordedDemoLink({ productId }: { productId: string }) { return ( ); } function ExistingDealLink({ dealId, ...rest }: { dealId: string; title: string; description: string; buttonText: string; }) { return ; } function DisabledState({ routing }: { routing: PublicRouting }) { return ( Select a Date {}} disablePast disabled /> ({ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', padding: 2, bgcolor: 'background.paper', borderRadius: 1, boxShadow: theme.shadows[4], })} > Enter your information to reveal booking availability ); } interface Props { routing: PublicRouting; routeResult: RouteResult | null; formValues: FormValues | null; disabled: boolean; productId?: string; } function RoutingResultFormInner({ routing, productId, routeResult, formValues, disabled }: Props) { const isMobile = useMediaQuery((theme) => theme.breakpoints.down('md')); const [, , setConfirm] = useWidgetStateContext(); const { data } = useInjectLeadContext(); const [loading, setLoading] = useState(false); const onSubmit = useCallback( async (booking: BookingParams) => { setLoading(true); const source = getBookingWidgetSource(routing.id); try { if (!formValues || !routeResult) return; const { email, name, phone, url, altchaPayload } = formValues; if (productId) { const meeting = await bookMeetingIntoProduct( { email, productId, booking, name, phone, ...(url && { url }), }, altchaPayload, source ); sendPixelEvent({ data: { name, email, productId, }, facebookPixel: routing.account.facebookPixel, googlePixel: routing.account.googlePixel, }); setConfirm({ meeting, formValues: { email, name, phone, url } }); return; } const { productId: routedProductId } = routeResult; const meeting = await bookMeetingIntoLatestNonStartedDeal( { email, productId: routedProductId, booking, name, phone, ...(url && { url }), }, altchaPayload, source ); sendPixelEvent({ data: { name, email, productId: routedProductId, }, facebookPixel: routing.account.facebookPixel, googlePixel: routing.account.googlePixel, }); setConfirm({ meeting, formValues: { email, name, phone, url } }); } catch (e) { console.error('Error booking meeting', e); } finally { setLoading(false); } }, [ formValues, routeResult, productId, setConfirm, routing.account.facebookPixel, routing.account.googlePixel, routing.id, ] ); const onDealSubmit = useCallback( async (slot: { startTime: Date; endTime: Date }) => { if (!formValues || !data?.deal) return; const source = getBookingWidgetSource(routing.id); const { email, name, phone, url, altchaPayload } = formValues; setLoading(true); try { const meeting = await bookMeetingIntoExistingDeal( { dealId: data?.deal?.uuid, event: slot, email, name, phone, ...(url && { url }), }, altchaPayload, source ); setConfirm({ meeting, formValues: { email, name, phone, url } }); } catch (e) { console.error('Error booking meeting', e); } }, [formValues, data?.deal, setConfirm, routing.id] ); if (disabled) { if (isMobile) return null; return ; } if (data?.deal && data.deal.started) { if (!data.deal.primaryUserId) return ( ); // If deal is started but no user assigned, show link to deal; // Show calendar with slots for user of existing deal; return ; } // If user is routed to a recorded demo and deal was created through real-time injection, send them to the salesroom for the deal created if (routeResult?.outcome === RoutingOutcomeType.RECORDING && data?.deal) return ( ); switch (routeResult?.outcome) { case undefined: case null: return ; case RoutingOutcomeType.RECORDING: return ; case RoutingOutcomeType.TEAM: return ; case RoutingOutcomeType.USER: return ; default: return null; } } export default function RoutingResultForm({ onGoBack, ...rest }: Props & { onGoBack: () => void; }) { const isMobile = useMediaQuery((theme) => theme.breakpoints.down('md')); return ( {isMobile && ( )} ); }