2022-05-18 21:05:49 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-03-15 20:07:35 +00:00
|
|
|
import { useCallback, useState } from "react";
|
2022-05-18 21:05:49 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry";
|
|
|
|
import type { RecurringEvent } from "@calcom/types/Calendar";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, TextArea } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { X } from "@calcom/ui/components/icon";
|
2022-05-18 21:05:49 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
booking: {
|
|
|
|
title?: string;
|
|
|
|
uid?: string;
|
2022-10-20 23:28:02 +00:00
|
|
|
id?: number;
|
2022-05-18 21:05:49 +00:00
|
|
|
};
|
|
|
|
profile: {
|
|
|
|
name: string | null;
|
|
|
|
slug: string | null;
|
|
|
|
};
|
2022-06-10 20:38:06 +00:00
|
|
|
recurringEvent: RecurringEvent | null;
|
2022-05-18 21:05:49 +00:00
|
|
|
team?: string | null;
|
|
|
|
setIsCancellationMode: (value: boolean) => void;
|
|
|
|
theme: string | null;
|
2022-11-16 19:48:17 +00:00
|
|
|
allRemainingBookings: boolean;
|
2023-03-14 04:19:05 +00:00
|
|
|
seatReferenceUid?: string;
|
2022-05-18 21:05:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CancelBooking(props: Props) {
|
|
|
|
const [cancellationReason, setCancellationReason] = useState<string>("");
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
2023-03-14 04:19:05 +00:00
|
|
|
const { booking, allRemainingBookings, seatReferenceUid } = props;
|
2022-05-18 21:05:49 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const telemetry = useTelemetry();
|
|
|
|
const [error, setError] = useState<string | null>(booking ? null : t("booking_already_cancelled"));
|
|
|
|
|
2023-03-15 20:07:35 +00:00
|
|
|
const cancelBookingRef = useCallback((node: HTMLTextAreaElement) => {
|
|
|
|
if (node !== null) {
|
|
|
|
node.scrollIntoView({ behavior: "smooth" });
|
|
|
|
node.focus();
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2022-07-26 08:27:57 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{error && (
|
2022-11-25 14:49:59 +00:00
|
|
|
<div className="mt-8">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-error mx-auto flex h-12 w-12 items-center justify-center rounded-full">
|
2023-04-12 15:26:31 +00:00
|
|
|
<X className="h-6 w-6 text-red-600" />
|
2022-07-26 08:27:57 +00:00
|
|
|
</div>
|
|
|
|
<div className="mt-3 text-center sm:mt-5">
|
2023-04-05 18:14:46 +00:00
|
|
|
<h3 className="text-emphasis text-lg font-medium leading-6" id="modal-title">
|
2022-07-26 08:27:57 +00:00
|
|
|
{error}
|
|
|
|
</h3>
|
2022-05-18 21:05:49 +00:00
|
|
|
</div>
|
2022-07-26 08:27:57 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!error && (
|
|
|
|
<div className="mt-5 sm:mt-6">
|
2023-04-05 18:14:46 +00:00
|
|
|
<label className="text-default font-medium">{t("cancellation_reason")}</label>
|
2022-11-16 19:48:17 +00:00
|
|
|
<TextArea
|
2023-06-10 03:47:50 +00:00
|
|
|
data-testid="cancel_reason"
|
2023-03-15 20:07:35 +00:00
|
|
|
ref={cancelBookingRef}
|
2022-07-26 08:27:57 +00:00
|
|
|
placeholder={t("cancellation_reason_placeholder")}
|
|
|
|
value={cancellationReason}
|
|
|
|
onChange={(e) => setCancellationReason(e.target.value)}
|
2023-04-05 18:14:46 +00:00
|
|
|
className="mt-2 mb-4 w-full "
|
2022-07-26 08:27:57 +00:00
|
|
|
rows={3}
|
|
|
|
/>
|
2022-11-16 19:48:17 +00:00
|
|
|
<div className="flex flex-col-reverse rtl:space-x-reverse ">
|
|
|
|
<div className="ml-auto flex w-full space-x-4 ">
|
|
|
|
<Button
|
|
|
|
className="ml-auto"
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => props.setIsCancellationMode(false)}>
|
2022-07-26 08:27:57 +00:00
|
|
|
{t("nevermind")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
2023-06-10 03:47:50 +00:00
|
|
|
data-testid="confirm_cancel"
|
2022-07-26 08:27:57 +00:00
|
|
|
onClick={async () => {
|
|
|
|
setLoading(true);
|
2022-05-18 21:05:49 +00:00
|
|
|
|
2022-07-26 08:27:57 +00:00
|
|
|
telemetry.event(telemetryEventTypes.bookingCancelled, collectPageParameters());
|
2022-05-18 21:05:49 +00:00
|
|
|
|
2022-07-26 08:27:57 +00:00
|
|
|
const res = await fetch("/api/cancel", {
|
2023-03-14 04:19:05 +00:00
|
|
|
body: JSON.stringify({
|
2023-03-20 18:30:00 +00:00
|
|
|
uid: booking?.uid,
|
2023-03-14 04:19:05 +00:00
|
|
|
cancellationReason: cancellationReason,
|
|
|
|
allRemainingBookings,
|
|
|
|
// @NOTE: very important this shouldn't cancel with number ID use uid instead
|
|
|
|
seatReferenceUid,
|
|
|
|
}),
|
2022-07-26 08:27:57 +00:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
2022-05-18 21:05:49 +00:00
|
|
|
|
2022-07-26 08:27:57 +00:00
|
|
|
if (res.status >= 200 && res.status < 300) {
|
2022-11-25 14:49:59 +00:00
|
|
|
await router.replace(router.asPath);
|
2022-07-26 08:27:57 +00:00
|
|
|
} else {
|
|
|
|
setLoading(false);
|
|
|
|
setError(
|
|
|
|
`${t("error_with_status_code_occured", { status: res.status })} ${t(
|
|
|
|
"please_try_again"
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
loading={loading}>
|
2022-11-16 19:48:17 +00:00
|
|
|
{props.allRemainingBookings ? t("cancel_all_remaining") : t("cancel_event")}
|
2022-07-26 08:27:57 +00:00
|
|
|
</Button>
|
2022-05-18 21:05:49 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-26 08:27:57 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2022-05-18 21:05:49 +00:00
|
|
|
}
|