Files
cal.diy/packages/features/calendars/CalendarSwitch.tsx
T
Omar LópezandUdit Takkar d294a74aad feat: Populate gCal calendar cache via webhooks (#11928)
* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* Update CalendarService.ts

* Type fixes

* WIP

* fix: improve cache hits

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Update CalendarService.ts

* Feedback

* Update CalendarService.ts

* Update CalendarService.ts

* Update _router.ts

* feedback

* WIP

* WIP

* Update schema.prisma

* feedback

* typefixes

* Update Calendar.d.ts

* fix: watches when adding a calendar

* Discard changes to packages/app-store/googlecalendar/api/add.ts

* WIP

Signed-off-by: Omar López <[email protected]>

* WIP

Signed-off-by: Omar López <[email protected]>

* WP

Signed-off-by: Omar López <[email protected]>

* Update calendar.ts

* Update calendar.ts

* Update callback.ts

* Update callback.ts

* Conflicts

* WIP

* WIP

* Update CalendarService.ts

* Cleanup

* Discard changes to packages/features/settings/layouts/SettingsLayout.tsx

* Update calendar-cache.repository.ts

* WIP

* Update getSelectedCalendarsToWatch.sql

* WIP

* Update CalendarService.ts

* Cleanup

* Discard changes to packages/app-store/googlecalendar/lib/CalendarService.ts

* Create CalendarService.wip.ts

* WIP

* Update CalendarService.ts

* Update getSelectedCalendarsToWatch.sql

* Delete CalendarService.wip.ts

* test updates

* cleanup

Signed-off-by: Omar López <[email protected]>

* Update CalendarService.test.ts

* Update CalendarService.ts

* type fixes

* Update OAuthManager.ts

* Update CalendarService.ts

* Almost there

* Update CalendarService.test.ts

* Update calendar.ts

* Update callback.ts

* Update toggleFeatureFlag.handler.ts

* fix: feedback

* Update getSelectedCalendarsToWatch.sql

* Fix unit tests

---------

Signed-off-by: Omar López <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>
2024-11-15 14:59:37 -03:00

99 lines
2.9 KiB
TypeScript

"use client";
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 { Icon, showToast, Switch } from "@calcom/ui";
export type 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.useUtils();
const { t } = useLocale();
const mutation = useMutation({
mutationFn: async ({ isOn }: { isOn: boolean }) => {
const body = {
integration: type,
externalId: externalId,
// new URLSearchParams does not accept numbers
credentialId: String(credentialId),
};
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.integrations.invalidate();
await utils.viewer.connectedCalendars.invalidate();
},
onError() {
setCheckedInternal(false);
showToast(`Something went wrong when toggling "${title}"`, "error");
},
});
return (
<div className={classNames("my-2 flex flex-row items-center")}>
<div className="flex pl-2">
<Switch
id={externalId}
checked={checkedInternal}
disabled={mutation.isPending}
onCheckedChange={async (isOn: boolean) => {
setCheckedInternal(isOn);
await mutation.mutate({ isOn });
}}
/>
</div>
<label className="ml-3 break-all text-sm font-medium leading-5" htmlFor={externalId}>
{name}
</label>
{!!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">
<Icon name="arrow-left" className="h-4 w-4" />
{t("adding_events_to")}
</span>
)}
{mutation.isPending && (
<Icon name="rotate-cw" className="text-muted h-4 w-4 animate-spin ltr:ml-1 rtl:mr-1" />
)}
</div>
);
};
export { CalendarSwitch };