2022-03-23 22:00:30 +00:00
|
|
|
import { Fragment } from "react";
|
2021-10-30 15:54:21 +00:00
|
|
|
import { useMutation } from "react-query";
|
|
|
|
|
2022-06-12 15:19:35 +00:00
|
|
|
import { InstallAppButton } from "@calcom/app-store/components";
|
2022-06-01 17:24:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-03-16 23:36:43 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { inferQueryOutput, trpc } from "@calcom/trpc/react";
|
2022-03-16 23:36:43 +00:00
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
|
|
|
import Button from "@calcom/ui/Button";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { List } from "@calcom/ui/List";
|
|
|
|
import { ShellSubHeading } from "@calcom/ui/Shell";
|
2022-03-11 00:26:42 +00:00
|
|
|
import Switch from "@calcom/ui/Switch";
|
2022-07-28 19:58:26 +00:00
|
|
|
import SkeletonLoader from "@calcom/ui/apps/SkeletonLoader";
|
2022-03-11 00:26:42 +00:00
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
import { QueryCell } from "@lib/QueryCell";
|
|
|
|
|
2022-06-01 17:24:41 +00:00
|
|
|
import AdditionalCalendarSelector from "@components/AdditionalCalendarSelector";
|
2022-01-21 21:35:31 +00:00
|
|
|
import DestinationCalendarSelector from "@components/DestinationCalendarSelector";
|
2021-10-30 15:54:21 +00:00
|
|
|
|
|
|
|
import DisconnectIntegration from "./DisconnectIntegration";
|
|
|
|
import IntegrationListItem from "./IntegrationListItem";
|
|
|
|
import SubHeadingTitleWithConnections from "./SubHeadingTitleWithConnections";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onChanged: () => unknown | Promise<unknown>;
|
2022-06-12 15:19:35 +00:00
|
|
|
fromOnboarding?: boolean;
|
2021-10-30 15:54:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function CalendarSwitch(props: {
|
|
|
|
type: string;
|
|
|
|
externalId: string;
|
|
|
|
title: string;
|
|
|
|
defaultSelected: boolean;
|
|
|
|
}) {
|
|
|
|
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() {
|
|
|
|
await utils.invalidateQueries(["viewer.integrations"]);
|
|
|
|
},
|
|
|
|
onError() {
|
|
|
|
showToast(`Something went wrong when toggling "${props.title}""`, "error");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<div className="py-1">
|
|
|
|
<Switch
|
|
|
|
key={props.externalId}
|
|
|
|
name="enabled"
|
|
|
|
label={props.title}
|
|
|
|
defaultChecked={props.defaultSelected}
|
|
|
|
onCheckedChange={(isOn: boolean) => {
|
|
|
|
mutation.mutate({ isOn });
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-12 15:19:35 +00:00
|
|
|
function CalendarList(props: Props) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const query = trpc.useQuery(["viewer.integrations", { variant: "calendar", onlyInstalled: false }]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<QueryCell
|
|
|
|
query={query}
|
|
|
|
success={({ data }) => (
|
|
|
|
<List>
|
|
|
|
{data.items.map((item) => (
|
|
|
|
<IntegrationListItem
|
2022-08-13 11:04:57 +00:00
|
|
|
name={item.name}
|
2022-07-23 00:59:30 +00:00
|
|
|
slug={item.slug}
|
2022-06-12 15:19:35 +00:00
|
|
|
key={item.title}
|
|
|
|
title={item.title}
|
2022-08-13 11:04:57 +00:00
|
|
|
logo={item.logo}
|
2022-06-12 15:19:35 +00:00
|
|
|
description={item.description}
|
|
|
|
actions={
|
|
|
|
<InstallAppButton
|
|
|
|
type={item.type}
|
|
|
|
render={(buttonProps) => (
|
|
|
|
<Button color="secondary" {...buttonProps}>
|
|
|
|
{t("connect")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
onChanged={() => props.onChanged()}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
function ConnectedCalendarsList(props: Props) {
|
2021-11-07 15:52:48 +00:00
|
|
|
const { t } = useLocale();
|
2021-10-30 15:54:21 +00:00
|
|
|
const query = trpc.useQuery(["viewer.connectedCalendars"], { suspense: true });
|
2022-06-12 15:19:35 +00:00
|
|
|
const { fromOnboarding } = props;
|
2021-10-30 15:54:21 +00:00
|
|
|
return (
|
|
|
|
<QueryCell
|
|
|
|
query={query}
|
|
|
|
empty={() => null}
|
2021-12-09 15:51:37 +00:00
|
|
|
success={({ data }) => {
|
|
|
|
if (!data.connectedCalendars.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<List>
|
|
|
|
{data.connectedCalendars.map((item) => (
|
|
|
|
<Fragment key={item.credentialId}>
|
|
|
|
{item.calendars ? (
|
|
|
|
<IntegrationListItem
|
2022-07-23 00:59:30 +00:00
|
|
|
slug={item.integration.slug}
|
2022-03-23 22:00:30 +00:00
|
|
|
title={item.integration.title}
|
2022-08-13 11:04:57 +00:00
|
|
|
logo={item.integration.logo}
|
2021-12-09 15:51:37 +00:00
|
|
|
description={item.primary?.externalId || "No external Id"}
|
|
|
|
actions={
|
|
|
|
<DisconnectIntegration
|
|
|
|
id={item.credentialId}
|
2022-07-15 20:28:24 +00:00
|
|
|
externalId={item.primary?.externalId}
|
2021-12-09 15:51:37 +00:00
|
|
|
render={(btnProps) => (
|
2021-12-17 16:58:23 +00:00
|
|
|
<Button {...btnProps} color="warn" data-testid="integration-connection-button">
|
2021-12-09 15:51:37 +00:00
|
|
|
{t("disconnect")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
onOpenChange={props.onChanged}
|
2021-10-30 15:54:21 +00:00
|
|
|
/>
|
2021-12-09 15:51:37 +00:00
|
|
|
}>
|
2022-06-12 15:19:35 +00:00
|
|
|
{!fromOnboarding && (
|
|
|
|
<>
|
2022-06-15 16:35:49 +00:00
|
|
|
<p className="px-4 pt-4 text-sm text-neutral-500">{t("toggle_calendars_conflict")}</p>
|
2022-06-12 15:19:35 +00:00
|
|
|
<ul className="space-y-2 p-4">
|
|
|
|
{item.calendars.map((cal) => (
|
|
|
|
<CalendarSwitch
|
|
|
|
key={cal.externalId}
|
|
|
|
externalId={cal.externalId}
|
|
|
|
title={cal.name || "Nameless calendar"}
|
|
|
|
type={item.integration.type}
|
|
|
|
defaultSelected={cal.isSelected}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
)}
|
2021-12-09 15:51:37 +00:00
|
|
|
</IntegrationListItem>
|
|
|
|
) : (
|
|
|
|
<Alert
|
|
|
|
severity="warning"
|
2022-09-21 13:24:02 +00:00
|
|
|
title={t("something_went_wrong")}
|
|
|
|
message={t("calendar_error")}
|
2021-12-09 15:51:37 +00:00
|
|
|
actions={
|
|
|
|
<DisconnectIntegration
|
|
|
|
id={item.credentialId}
|
|
|
|
render={(btnProps) => (
|
2021-12-17 16:58:23 +00:00
|
|
|
<Button {...btnProps} color="warn" data-testid="integration-connection-button">
|
2021-12-09 15:51:37 +00:00
|
|
|
Disconnect
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
onOpenChange={() => props.onChanged()}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Fragment>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:33:07 +00:00
|
|
|
export function CalendarListContainer(props: {
|
|
|
|
heading?: boolean;
|
|
|
|
items?: inferQueryOutput<"viewer.integrations">["items"];
|
|
|
|
fromOnboarding?: boolean;
|
|
|
|
}) {
|
2021-11-07 15:52:48 +00:00
|
|
|
const { t } = useLocale();
|
2022-06-12 15:19:35 +00:00
|
|
|
const { heading = true, fromOnboarding } = props;
|
2021-10-30 15:54:21 +00:00
|
|
|
const utils = trpc.useContext();
|
|
|
|
const onChanged = () =>
|
|
|
|
Promise.allSettled([
|
2022-06-20 17:33:07 +00:00
|
|
|
utils.invalidateQueries(["viewer.integrations", { variant: "calendar", onlyInstalled: true }], {
|
|
|
|
exact: true,
|
|
|
|
}),
|
2021-10-30 15:54:21 +00:00
|
|
|
utils.invalidateQueries(["viewer.connectedCalendars"]),
|
|
|
|
]);
|
|
|
|
const query = trpc.useQuery(["viewer.connectedCalendars"]);
|
2022-06-01 17:24:41 +00:00
|
|
|
const installedCalendars = trpc.useQuery([
|
|
|
|
"viewer.integrations",
|
|
|
|
{ variant: "calendar", onlyInstalled: true },
|
|
|
|
]);
|
2022-01-21 21:35:31 +00:00
|
|
|
const mutation = trpc.useMutation("viewer.setDestinationCalendar");
|
2021-10-30 15:54:21 +00:00
|
|
|
return (
|
2022-06-01 17:24:41 +00:00
|
|
|
<QueryCell
|
|
|
|
query={query}
|
|
|
|
customLoader={<SkeletonLoader className="mt-10" />}
|
|
|
|
success={({ data }) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{(!!data.connectedCalendars.length || !!installedCalendars.data?.items.length) && (
|
|
|
|
<>
|
|
|
|
{heading && (
|
|
|
|
<ShellSubHeading
|
|
|
|
className="mt-10 mb-0"
|
|
|
|
title={
|
|
|
|
<SubHeadingTitleWithConnections
|
|
|
|
title="Calendars"
|
|
|
|
numConnections={data.connectedCalendars.length}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
subtitle={t("configure_how_your_event_types_interact")}
|
|
|
|
actions={
|
|
|
|
<div className="flex flex-col xl:flex-row xl:space-x-5">
|
|
|
|
<div className="sm:min-w-80 block max-w-full">
|
|
|
|
<DestinationCalendarSelector
|
|
|
|
onChange={mutation.mutate}
|
|
|
|
isLoading={mutation.isLoading}
|
|
|
|
value={data.destinationCalendar?.externalId}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-06-12 15:19:35 +00:00
|
|
|
|
2022-06-01 17:24:41 +00:00
|
|
|
{!!data.connectedCalendars.length && (
|
|
|
|
<div className="sm:min-w-80 inline max-w-full">
|
|
|
|
<AdditionalCalendarSelector isLoading={mutation.isLoading} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2022-06-15 16:35:49 +00:00
|
|
|
<ConnectedCalendarsList onChanged={onChanged} fromOnboarding={fromOnboarding} />
|
2022-06-12 15:19:35 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{fromOnboarding && (
|
|
|
|
<>
|
|
|
|
{!!query.data?.connectedCalendars.length && (
|
|
|
|
<ShellSubHeading
|
|
|
|
className="mt-4"
|
|
|
|
title={<SubHeadingTitleWithConnections title={t("connect_additional_calendar")} />}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<CalendarList onChanged={onChanged} />
|
2022-06-01 17:24:41 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
2021-10-30 15:54:21 +00:00
|
|
|
);
|
|
|
|
}
|