2022-09-29 16:58:29 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query";
|
2022-08-26 00:11:41 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Badge, showToast, Switch } from "@calcom/ui";
|
|
|
|
import { FiArrowLeft } from "@calcom/ui/components/icon";
|
2022-08-26 00:11:41 +00:00
|
|
|
|
|
|
|
export function CalendarSwitch(props: {
|
|
|
|
type: string;
|
|
|
|
externalId: string;
|
|
|
|
title: string;
|
|
|
|
defaultSelected: boolean;
|
2022-09-19 02:01:34 +00:00
|
|
|
isSelected: boolean;
|
2022-08-26 00:11:41 +00:00
|
|
|
}) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
|
|
|
|
const mutation = useMutation<
|
|
|
|
unknown,
|
|
|
|
unknown,
|
|
|
|
{
|
|
|
|
isOn: boolean;
|
|
|
|
}
|
|
|
|
>(
|
|
|
|
async ({ isOn }) => {
|
|
|
|
const body = {
|
|
|
|
integration: props.type,
|
|
|
|
externalId: props.externalId,
|
|
|
|
};
|
|
|
|
if (isOn) {
|
|
|
|
const res = await fetch("/api/availability/calendar", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("Something went wrong");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const res = await fetch("/api/availability/calendar", {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("Something went wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
async onSettled() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.integrations.invalidate();
|
2022-08-26 00:11:41 +00:00
|
|
|
},
|
|
|
|
onError() {
|
|
|
|
showToast(`Something went wrong when toggling "${props.title}""`, "error");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return (
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="flex space-x-2 py-1 rtl:space-x-reverse">
|
2022-08-26 00:11:41 +00:00
|
|
|
<Switch
|
|
|
|
key={props.externalId}
|
|
|
|
name="enabled"
|
|
|
|
label={props.title}
|
2022-09-19 02:01:34 +00:00
|
|
|
defaultChecked={props.isSelected}
|
2022-08-26 00:11:41 +00:00
|
|
|
onCheckedChange={(isOn: boolean) => {
|
|
|
|
mutation.mutate({ isOn });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{props.defaultSelected && (
|
|
|
|
<Badge variant="gray">
|
2023-03-23 12:07:11 +00:00
|
|
|
<div className="flex">
|
|
|
|
<FiArrowLeft className="mr-1" /> {t("adding_events_to")}
|
|
|
|
</div>
|
2022-08-26 00:11:41 +00:00
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|