import { useMutation } from "@tanstack/react-query"; import { useState } from "react"; import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import { showToast, Switch } from "@calcom/ui"; import { ArrowLeft, RotateCw } from "@calcom/ui/components/icon"; interface ICalendarSwitchProps { title: string; externalId: string; type: string; isChecked: boolean; name: string; isLastItemInList?: boolean; destination?: boolean; credentialId: number; } const CalendarSwitch = (props: ICalendarSwitchProps) => { const { title, externalId, type, isChecked, name, isLastItemInList = false, credentialId } = props; const [checkedInternal, setCheckedInternal] = useState(isChecked); const utils = trpc.useContext(); const { t } = useLocale(); const mutation = useMutation< unknown, unknown, { isOn: boolean; } >( async ({ isOn }: { isOn: boolean }) => { 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, credentialId }), }); if (!res.ok) { throw new Error("Something went wrong"); } } else { const res = await fetch(`/api/availability/calendar?${new URLSearchParams(body)}`, { method: "DELETE", headers: { "Content-Type": "application/json", }, }); if (!res.ok) { throw new Error("Something went wrong"); } } }, { async onSettled() { await utils.viewer.integrations.invalidate(); await utils.viewer.connectedCalendars.invalidate(); }, onError() { setCheckedInternal(false); showToast(`Something went wrong when toggling "${title}"`, "error"); }, } ); return (
{ setCheckedInternal(isOn); await mutation.mutate({ isOn }); }} />
{!!props.destination && ( {t("adding_events_to")} )} {mutation.isLoading && }
); }; export { CalendarSwitch };