import { CalendarIcon, XIcon } from "@heroicons/react/solid";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import { useRouter } from "next/router";
import { useState } from "react";
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) {
// 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 : "This booking was 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("An error with status code " + res.status + " occurred. Please try again later.");
}
};
return (
{error && (
)}
{!error && (
<>
Really cancel your booking?
Instead, you could also reschedule it.
{props.booking.title}
{dayjs(props.booking.startTime).format(
(is24h ? "H:mm" : "h:mma") + ", dddd DD MMMM YYYY"
)}
>
)}
);
}
export async function getServerSideProps(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: {
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,
},
};
}