2022-09-29 16:58:29 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query";
|
2023-05-09 16:53:23 +00:00
|
|
|
import { useState } from "react";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
2023-05-09 16:53:23 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { showToast, Switch } from "@calcom/ui";
|
2023-06-09 22:21:54 +00:00
|
|
|
import { ArrowLeft, RotateCw } from "@calcom/ui/components/icon";
|
2022-09-07 01:34:33 +00:00
|
|
|
|
2022-09-06 22:58:16 +00:00
|
|
|
interface ICalendarSwitchProps {
|
|
|
|
title: string;
|
|
|
|
externalId: string;
|
|
|
|
type: string;
|
|
|
|
isChecked: boolean;
|
|
|
|
name: string;
|
2022-09-07 01:34:33 +00:00
|
|
|
isLastItemInList?: boolean;
|
2023-05-09 16:53:23 +00:00
|
|
|
destination?: boolean;
|
2022-09-06 22:58:16 +00:00
|
|
|
}
|
|
|
|
const CalendarSwitch = (props: ICalendarSwitchProps) => {
|
2022-09-07 01:34:33 +00:00
|
|
|
const { title, externalId, type, isChecked, name, isLastItemInList = false } = props;
|
2023-05-09 16:53:23 +00:00
|
|
|
const [checkedInternal, setCheckedInternal] = useState(isChecked);
|
2022-09-06 22:58:16 +00:00
|
|
|
const utils = trpc.useContext();
|
2023-05-09 16:53:23 +00:00
|
|
|
const { t } = useLocale();
|
2022-09-06 22:58:16 +00:00
|
|
|
const mutation = useMutation<
|
|
|
|
unknown,
|
|
|
|
unknown,
|
|
|
|
{
|
|
|
|
isOn: boolean;
|
|
|
|
}
|
|
|
|
>(
|
2022-09-07 01:34:33 +00:00
|
|
|
async ({ isOn }: { isOn: boolean }) => {
|
2022-09-06 22:58:16 +00:00
|
|
|
const body = {
|
|
|
|
integration: type,
|
|
|
|
externalId: 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 {
|
2023-06-19 21:22:30 +00:00
|
|
|
const res = await fetch("/api/availability/calendar?" + new URLSearchParams(body), {
|
2022-09-06 22:58:16 +00:00
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error("Something went wrong");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
async onSettled() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.integrations.invalidate();
|
2023-05-09 16:53:23 +00:00
|
|
|
await utils.viewer.connectedCalendars.invalidate();
|
2022-09-06 22:58:16 +00:00
|
|
|
},
|
|
|
|
onError() {
|
2023-05-09 16:53:23 +00:00
|
|
|
setCheckedInternal(false);
|
2022-09-06 22:58:16 +00:00
|
|
|
showToast(`Something went wrong when toggling "${title}""`, "error");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return (
|
2023-05-15 13:56:26 +00:00
|
|
|
<div className={classNames("my-2 flex flex-row items-center")}>
|
2022-09-07 01:34:33 +00:00
|
|
|
<div className="flex pl-2">
|
2022-09-06 22:58:16 +00:00
|
|
|
<Switch
|
|
|
|
id={externalId}
|
2023-05-09 16:53:23 +00:00
|
|
|
checked={checkedInternal}
|
2022-09-06 22:58:16 +00:00
|
|
|
onCheckedChange={(isOn: boolean) => {
|
2023-05-09 16:53:23 +00:00
|
|
|
setCheckedInternal(isOn);
|
2022-09-06 22:58:16 +00:00
|
|
|
mutation.mutate({ isOn });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-09-07 01:34:33 +00:00
|
|
|
<label className="ml-3 text-sm font-medium leading-5" htmlFor={externalId}>
|
2022-09-06 22:58:16 +00:00
|
|
|
{name}
|
|
|
|
</label>
|
2023-05-09 16:53:23 +00:00
|
|
|
{!!props.destination && (
|
|
|
|
<span className="bg-subtle text-default ml-8 inline-flex items-center gap-1 rounded-md px-2 py-1 text-sm font-normal sm:ml-4">
|
|
|
|
<ArrowLeft className="h-4 w-4" />
|
|
|
|
{t("adding_events_to")}
|
|
|
|
</span>
|
|
|
|
)}
|
2023-06-09 22:21:54 +00:00
|
|
|
{mutation.isLoading && <RotateCw className="text-muted h-4 w-4 animate-spin ltr:ml-1 rtl:mr-1" />}
|
2022-09-06 22:58:16 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { CalendarSwitch };
|