"use client"; import { useMutation } from "@tanstack/react-query"; import { useState } from "react"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import classNames from "@calcom/ui/classNames"; import { Switch } from "@calcom/ui/components/form"; import { ArrowLeftIcon, RotateCwIcon } from "@coss/ui/icons"; import { showToast } from "@calcom/ui/components/toast"; import type { ICalendarSwitchProps } from "@calcom/ui/components/calendar-switch"; type UserCalendarSwitchProps = Omit; type EventCalendarSwitchProps = ICalendarSwitchProps & { eventTypeId: number; }; const CalendarSwitch = (props: ICalendarSwitchProps) => { const { title, externalId, type, isChecked, name, credentialId, delegationCredentialId, eventTypeId, disabled, } = props; const [checkedInternal, setCheckedInternal] = useState(isChecked); const utils = trpc.useUtils(); const { t } = useLocale(); const mutation = useMutation({ mutationFn: async ({ isOn }: { isOn: boolean }) => { const body = { integration: type, externalId: externalId, ...(delegationCredentialId && { delegationCredentialId }), // new URLSearchParams does not accept numbers credentialId: String(credentialId), ...(eventTypeId ? { eventTypeId: String(eventTypeId) } : {}), }; 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?${new URLSearchParams(body)}`, { method: "DELETE", headers: { "Content-Type": "application/json", }, }); if (!res.ok) { throw new Error("Something went wrong"); } } }, async onSettled() { await utils.viewer.apps.integrations.invalidate(); await utils.viewer.calendars.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.isPending && ( )}
); }; export const UserCalendarSwitch = (props: UserCalendarSwitchProps) => { return ; }; export const EventCalendarSwitch = (props: EventCalendarSwitchProps) => { return ; }; export { CalendarSwitch };