2021-10-20 15:42:40 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useMutation } from "react-query";
|
|
|
|
|
2022-06-03 14:57:50 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-03-23 22:00:30 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
2022-03-16 23:36:43 +00:00
|
|
|
import { ButtonBaseProps } from "@calcom/ui/Button";
|
|
|
|
import { Dialog } from "@calcom/ui/Dialog";
|
|
|
|
|
2021-10-20 15:42:40 +00:00
|
|
|
import ConfirmationDialogContent from "@components/dialog/ConfirmationDialogContent";
|
|
|
|
|
|
|
|
export default function DisconnectIntegration(props: {
|
|
|
|
/** Integration credential id */
|
|
|
|
id: number;
|
|
|
|
render: (renderProps: ButtonBaseProps) => JSX.Element;
|
2021-10-30 15:54:21 +00:00
|
|
|
onOpenChange: (isOpen: boolean) => unknown | Promise<unknown>;
|
2021-10-20 15:42:40 +00:00
|
|
|
}) {
|
2022-06-03 14:57:50 +00:00
|
|
|
const { t } = useLocale();
|
2021-10-20 15:42:40 +00:00
|
|
|
const [modalOpen, setModalOpen] = useState(false);
|
|
|
|
const mutation = useMutation(
|
|
|
|
async () => {
|
|
|
|
const res = await fetch("/api/integrations", {
|
|
|
|
method: "DELETE",
|
|
|
|
body: JSON.stringify({ id: props.id }),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("Something went wrong");
|
|
|
|
}
|
2022-03-23 22:00:30 +00:00
|
|
|
return res.json();
|
2021-10-20 15:42:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
async onSettled() {
|
2021-10-20 15:54:52 +00:00
|
|
|
await props.onOpenChange(modalOpen);
|
2021-10-20 15:42:40 +00:00
|
|
|
},
|
2022-03-23 22:00:30 +00:00
|
|
|
onSuccess(data) {
|
|
|
|
showToast(data.message, "success");
|
2021-10-20 15:42:40 +00:00
|
|
|
setModalOpen(false);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Dialog open={modalOpen} onOpenChange={setModalOpen}>
|
|
|
|
<ConfirmationDialogContent
|
|
|
|
variety="danger"
|
2022-06-03 14:57:50 +00:00
|
|
|
title={t("remove_app")}
|
|
|
|
confirmBtnText={t("yes_remove_app")}
|
2021-10-20 15:42:40 +00:00
|
|
|
cancelBtnText="Cancel"
|
|
|
|
onConfirm={() => {
|
|
|
|
mutation.mutate();
|
|
|
|
}}>
|
2022-06-03 14:57:50 +00:00
|
|
|
{t("are_you_sure_you_want_to_remove_this_app")}
|
2021-10-20 15:42:40 +00:00
|
|
|
</ConfirmationDialogContent>
|
|
|
|
</Dialog>
|
|
|
|
{props.render({
|
|
|
|
onClick() {
|
|
|
|
setModalOpen(true);
|
|
|
|
},
|
|
|
|
disabled: modalOpen,
|
|
|
|
loading: mutation.isLoading,
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|