2022-07-22 17:27:06 +00:00
|
|
|
import { ClockIcon } from "@heroicons/react/outline";
|
2022-09-29 16:58:29 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useState } from "react";
|
2022-04-15 02:24:27 +00:00
|
|
|
|
2022-07-12 14:32:27 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-04-15 02:24:27 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import logger from "@calcom/lib/logger";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, Dialog, DialogContent, DialogFooter, DialogHeader, showToast } from "@calcom/ui";
|
2022-04-15 02:24:27 +00:00
|
|
|
|
|
|
|
interface IConfirmDialogWipe {
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setIsOpenDialog: Dispatch<SetStateAction<boolean>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IWipeMyCalAction {
|
|
|
|
initialDate: string;
|
|
|
|
endDate: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wipeMyCalAction = async (props: IWipeMyCalAction) => {
|
|
|
|
const { initialDate, endDate } = props;
|
|
|
|
const body = {
|
|
|
|
initialDate,
|
|
|
|
endDate,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const endpoint = "/api/integrations/wipemycalother/wipe";
|
2022-06-10 19:16:10 +00:00
|
|
|
return fetch(`${process.env.NEXT_PUBLIC_WEBAPP_URL}` + endpoint, {
|
2022-04-15 02:24:27 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
showToast("Error ocurred while trying to cancel bookings", "error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ConfirmDialog = (props: IConfirmDialogWipe) => {
|
|
|
|
const { t } = useLocale();
|
2022-07-22 17:27:06 +00:00
|
|
|
const { isOpenDialog, setIsOpenDialog } = props;
|
2022-04-15 02:24:27 +00:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
const today = dayjs();
|
|
|
|
const initialDate = today.startOf("day");
|
|
|
|
const endDate = today.endOf("day");
|
|
|
|
const dateFormat = "ddd, MMM D, YYYY h:mm A";
|
2022-04-15 10:09:40 +00:00
|
|
|
|
2022-04-15 02:24:27 +00:00
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
const rescheduleApi = useMutation(
|
|
|
|
async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
|
|
|
const result = await wipeMyCalAction({
|
|
|
|
initialDate: initialDate.toISOString(),
|
|
|
|
endDate: endDate.toISOString(),
|
|
|
|
});
|
|
|
|
if (result) {
|
|
|
|
showToast(t("reschedule_request_sent"), "success");
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
showToast(t("unexpected_error_try_again"), "error");
|
2023-04-17 22:22:44 +00:00
|
|
|
// @TODO: notify
|
2022-04-15 02:24:27 +00:00
|
|
|
}
|
|
|
|
setIsLoading(false);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
async onSettled() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.bookings.invalidate();
|
2022-04-15 02:24:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
2022-06-10 19:16:10 +00:00
|
|
|
<DialogContent onOpenAutoFocus={(e) => e.preventDefault()}>
|
2022-04-15 08:54:09 +00:00
|
|
|
<div className="flex flex-row space-x-3">
|
2022-04-15 02:24:27 +00:00
|
|
|
<div className="flex h-10 w-10 flex-shrink-0 justify-center rounded-full bg-[#FAFAFA]">
|
2022-07-12 21:35:12 +00:00
|
|
|
<ClockIcon className="m-auto h-6 w-6" />
|
2022-04-15 02:24:27 +00:00
|
|
|
</div>
|
2022-04-15 08:54:09 +00:00
|
|
|
<div className="pt-1">
|
2022-07-12 17:50:04 +00:00
|
|
|
<DialogHeader title="Wipe My Calendar" />
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-subtle mt-2 text-sm">
|
2022-04-15 02:24:27 +00:00
|
|
|
This will cancel all upcoming meetings from: <br />{" "}
|
2023-04-05 18:14:46 +00:00
|
|
|
<strong className="text-emphasis">
|
2022-04-15 02:24:27 +00:00
|
|
|
{initialDate.format(dateFormat)} - {endDate.format(dateFormat)}
|
|
|
|
</strong>
|
|
|
|
</p>
|
2022-09-09 15:02:31 +00:00
|
|
|
<p className="mt-6 mb-2 text-sm">Are you sure? This can't be undone</p>
|
2022-04-15 02:24:27 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-15 08:54:09 +00:00
|
|
|
|
|
|
|
<DialogFooter>
|
2022-06-10 19:16:10 +00:00
|
|
|
<Button color="secondary" onClick={() => setIsOpenDialog(false)}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
|
2022-04-15 08:54:09 +00:00
|
|
|
<Button
|
2022-09-09 15:02:31 +00:00
|
|
|
color="primary"
|
2022-04-15 08:54:09 +00:00
|
|
|
data-testid="send_request"
|
|
|
|
disabled={isLoading}
|
|
|
|
onClick={async () => {
|
|
|
|
try {
|
|
|
|
rescheduleApi.mutate();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
logger.error(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
{t("confirm")}
|
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
2022-04-15 02:24:27 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|