import { CheckIcon } from "@heroicons/react/outline"; import { ClockIcon } from "@heroicons/react/solid"; import dayjs from "dayjs"; import timezone from "dayjs/plugin/timezone"; import toArray from "dayjs/plugin/toArray"; import utc from "dayjs/plugin/utc"; import { createEvent } from "ics"; import { GetServerSidePropsContext } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; 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"; import { inferSSRProps } from "@lib/types/inferSSRProps"; import CustomBranding from "@components/CustomBranding"; import { EmailInput } from "@components/form/fields"; import { HeadSeo } from "@components/seo/head-seo"; import Button from "@components/ui/Button"; import { ssrInit } from "@server/lib/ssr"; dayjs.extend(utc); dayjs.extend(toArray); dayjs.extend(timezone); export default function Success(props: inferSSRProps) { const { t } = useLocale(); const router = useRouter(); const { location, name, reschedule } = router.query; const [is24h, setIs24h] = useState(false); const [date, setDate] = useState(dayjs.utc(asStringOrThrow(router.query.date))); const { isReady } = useTheme(props.profile.theme); useEffect(() => { setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess())); setIs24h(!!localStorage.getItem("timeOption.is24hClock")); }, []); const attendeeName = typeof name === "string" ? name : "Nameless"; const eventNameObject = { attendeeName, eventType: props.eventType.title, eventName: props.eventType.eventName, host: props.profile.name || "Nameless", t, }; const eventName = getEventName(eventNameObject); function eventLink(): string { const optional: { location?: string } = {}; if (location) { optional["location"] = Array.isArray(location) ? location[0] : location; } const event = createEvent({ start: [ date.toDate().getUTCFullYear(), (date.toDate().getUTCMonth() as number) + 1, date.toDate().getUTCDate(), date.toDate().getUTCHours(), date.toDate().getUTCMinutes(), ], startInputType: "utc", title: eventName, description: props.eventType.description ? props.eventType.description : undefined, /** formatted to required type of description ^ */ duration: { minutes: props.eventType.length }, ...optional, }); if (event.error) { throw event.error; } return encodeURIComponent(event.value ? event.value : false); } const needsConfirmation = props.eventType.requiresConfirmation && reschedule != "true"; return ( (isReady && (
)) || null ); } export async function getServerSideProps(context: GetServerSidePropsContext) { const ssr = await ssrInit(context); const typeId = parseInt(asStringOrNull(context.query.type) ?? ""); if (isNaN(typeId)) { return { notFound: true, }; } const eventType = await prisma.eventType.findUnique({ where: { id: typeId, }, select: { id: true, title: true, description: true, length: true, eventName: true, requiresConfirmation: true, userId: true, users: { select: { name: true, hideBranding: true, plan: true, theme: true, brandColor: true, }, }, team: { select: { name: true, hideBranding: true, }, }, }, }); if (!eventType) { return { notFound: true, }; } if (!eventType.users.length && eventType.userId) { // TODO we should add `user User` relation on `EventType` so this extra query isn't needed const user = await prisma.user.findUnique({ where: { id: eventType.userId, }, select: { name: true, hideBranding: true, plan: true, theme: true, brandColor: true, }, }); if (user) { eventType.users.push(user); } } if (!eventType.users.length) { return { notFound: true, }; } const profile = { name: eventType.team?.name || eventType.users[0]?.name || null, theme: (!eventType.team?.name && eventType.users[0]?.theme) || null, brandColor: eventType.team ? null : eventType.users[0].brandColor, }; return { props: { hideBranding: eventType.team ? eventType.team.hideBranding : isBrandingHidden(eventType.users[0]), profile, eventType, trpcState: ssr.dehydrate(), }, }; }