import { CalendarIcon, XIcon } from "@heroicons/react/solid";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import { getSession } from "next-auth/client";
import { useRouter } from "next/router";
import { useState } from "react";
import { useLocale } from "@lib/hooks/useLocale";
import prisma from "@lib/prisma";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@lib/telemetry";
import { HeadSeo } from "@components/seo/head-seo";
import { Button } from "@components/ui/Button";
dayjs.extend(utc);
export default function Type(props) {
const { t } = useLocale();
// Get router variables
const router = useRouter();
const { uid } = router.query;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [is24h, setIs24h] = useState(false);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(props.booking ? null : t("booking_already_cancelled"));
const telemetry = useTelemetry();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const cancellationHandler = async (event) => {
setLoading(true);
const payload = {
uid: uid,
};
telemetry.withJitsu((jitsu) =>
jitsu.track(telemetryEventTypes.bookingCancelled, collectPageParameters())
);
const res = await fetch("/api/cancel", {
body: JSON.stringify(payload),
headers: {
"Content-Type": "application/json",
},
method: "DELETE",
});
if (res.status >= 200 && res.status < 300) {
await router.push(
`/cancel/success?name=${props.profile.name}&title=${props.booking.title}&eventPage=${
props.profile.slug
}&team=${props.booking.eventType.team ? 1 : 0}`
);
} else {
setLoading(false);
setError(`${t("error_with_status_code_occured", { status: res.status })} ${t("please_try_again")}`);
}
};
return (
{error && (
)}
{!error && (
<>
{props.cancellationAllowed
? t("really_cancel_booking")
: t("cannot_cancel_booking")}
{props.cancellationAllowed ? t("reschedule_instead") : t("event_is_in_the_past")}
{props.booking.title}
{dayjs(props.booking.startTime).format(
(is24h ? "H:mm" : "h:mma") + ", dddd DD MMMM YYYY"
)}
{props.cancellationAllowed && (
)}
>
)}
);
}
export async function getServerSideProps(context) {
const session = await getSession(context);
const booking = await prisma.booking.findUnique({
where: {
uid: context.query.uid,
},
select: {
id: true,
title: true,
description: true,
startTime: true,
endTime: true,
attendees: true,
user: {
select: {
id: true,
username: true,
name: true,
},
},
eventType: {
select: {
team: {
select: {
slug: true,
name: true,
},
},
},
},
},
});
if (!booking) {
// TODO: Booking is already cancelled
return {
props: { booking: null },
};
}
const bookingObj = Object.assign({}, booking, {
startTime: booking.startTime.toString(),
endTime: booking.endTime.toString(),
});
const profile = booking.eventType.team
? {
name: booking.eventType.team.name,
slug: booking.eventType.team.slug,
}
: booking.user;
return {
props: {
profile,
booking: bookingObj,
cancellationAllowed:
(!!session?.user && session.user.id == booking.user?.id) || booking.startTime >= new Date(),
},
};
}