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-03-23 22:00:30 +00:00
|
|
|
import { InstallAppButton } from "@calcom/app-store/components";
|
2022-03-16 23:36:43 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
|
|
|
import Button from "@calcom/ui/Button";
|
2022-03-11 00:26:42 +00:00
|
|
|
import Switch from "@calcom/ui/Switch";
|
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
import { QueryCell } from "@lib/QueryCell";
|
2021-11-07 15:52:48 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
2021-10-30 15:54:21 +00:00
|
|
|
import { trpc } from "@lib/trpc";
|
|
|
|
|
2022-01-21 21:35:31 +00:00
|
|
|
import DestinationCalendarSelector from "@components/DestinationCalendarSelector";
|
2021-10-30 15:54:21 +00:00
|
|
|
import { List } from "@components/List";
|
|
|
|
import { ShellSubHeading } from "@components/Shell";
|
|
|
|
|
|
|
|
import DisconnectIntegration from "./DisconnectIntegration";
|
|
|
|
import IntegrationListItem from "./IntegrationListItem";
|
|
|
|
import SubHeadingTitleWithConnections from "./SubHeadingTitleWithConnections";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onChanged: () => unknown | Promise<unknown>;
|
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
|
|
|
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-03-23 22:00:30 +00:00
|
|
|
title={item.integration.title}
|
|
|
|
imageSrc={item.integration.imageSrc}
|
2021-12-09 15:51:37 +00:00
|
|
|
description={item.primary?.externalId || "No external Id"}
|
|
|
|
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
|
|
|
{t("disconnect")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
onOpenChange={props.onChanged}
|
2021-10-30 15:54:21 +00:00
|
|
|
/>
|
2021-12-09 15:51:37 +00:00
|
|
|
}>
|
2022-02-09 00:05:13 +00:00
|
|
|
<ul className="space-y-2 p-4">
|
2021-12-09 15:51:37 +00:00
|
|
|
{item.calendars.map((cal) => (
|
|
|
|
<CalendarSwitch
|
|
|
|
key={cal.externalId}
|
2022-03-23 22:00:30 +00:00
|
|
|
externalId={cal.externalId}
|
|
|
|
title={cal.name || "Nameless calendar"}
|
2021-12-09 15:51:37 +00:00
|
|
|
type={item.integration.type}
|
|
|
|
defaultSelected={cal.isSelected}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</IntegrationListItem>
|
|
|
|
) : (
|
|
|
|
<Alert
|
|
|
|
severity="warning"
|
2022-04-28 20:45:27 +00:00
|
|
|
title={t("calendar_error")}
|
2021-12-09 15:51:37 +00:00
|
|
|
message={item.error?.message}
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
function CalendarList(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.integrations"]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<QueryCell
|
|
|
|
query={query}
|
|
|
|
success={({ data }) => (
|
|
|
|
<List>
|
|
|
|
{data.calendar.items.map((item) => (
|
|
|
|
<IntegrationListItem
|
|
|
|
key={item.title}
|
2022-03-23 22:00:30 +00:00
|
|
|
title={item.title}
|
|
|
|
imageSrc={item.imageSrc}
|
|
|
|
description={item.description}
|
2021-10-30 15:54:21 +00:00
|
|
|
actions={
|
2022-03-23 22:00:30 +00:00
|
|
|
<InstallAppButton
|
2021-10-30 15:54:21 +00:00
|
|
|
type={item.type}
|
2022-03-23 22:00:30 +00:00
|
|
|
render={(buttonProps) => (
|
|
|
|
<Button color="secondary" {...buttonProps}>
|
2021-11-07 15:52:48 +00:00
|
|
|
{t("connect")}
|
2021-10-30 15:54:21 +00:00
|
|
|
</Button>
|
|
|
|
)}
|
2022-03-23 22:00:30 +00:00
|
|
|
onChanged={() => props.onChanged()}
|
2021-10-30 15:54:21 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2022-03-23 22:00:30 +00:00
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
export function CalendarListContainer(props: { heading?: false }) {
|
2021-11-07 15:52:48 +00:00
|
|
|
const { t } = useLocale();
|
2021-10-30 15:54:21 +00:00
|
|
|
const { heading = true } = props;
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const onChanged = () =>
|
|
|
|
Promise.allSettled([
|
|
|
|
utils.invalidateQueries(["viewer.integrations"]),
|
|
|
|
utils.invalidateQueries(["viewer.connectedCalendars"]),
|
|
|
|
]);
|
|
|
|
const query = trpc.useQuery(["viewer.connectedCalendars"]);
|
2022-01-21 21:35:31 +00:00
|
|
|
const mutation = trpc.useMutation("viewer.setDestinationCalendar");
|
|
|
|
|
2021-10-30 15:54:21 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{heading && (
|
|
|
|
<ShellSubHeading
|
2021-12-09 19:37:29 +00:00
|
|
|
className="mt-10 mb-0"
|
2021-12-09 15:51:37 +00:00
|
|
|
title={
|
|
|
|
<SubHeadingTitleWithConnections
|
|
|
|
title="Calendars"
|
|
|
|
numConnections={query.data?.connectedCalendars.length}
|
|
|
|
/>
|
|
|
|
}
|
2021-11-07 15:52:48 +00:00
|
|
|
subtitle={t("configure_how_your_event_types_interact")}
|
2021-12-09 19:37:29 +00:00
|
|
|
actions={
|
2022-02-09 22:32:31 +00:00
|
|
|
<div className="sm:min-w-80 block max-w-full">
|
2022-01-21 21:35:31 +00:00
|
|
|
<DestinationCalendarSelector
|
|
|
|
onChange={mutation.mutate}
|
|
|
|
isLoading={mutation.isLoading}
|
|
|
|
value={query.data?.destinationCalendar?.externalId}
|
|
|
|
/>
|
2021-12-09 19:37:29 +00:00
|
|
|
</div>
|
|
|
|
}
|
2021-10-30 15:54:21 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<ConnectedCalendarsList onChanged={onChanged} />
|
2021-12-09 15:51:37 +00:00
|
|
|
{!!query.data?.connectedCalendars.length && (
|
2021-10-30 15:54:21 +00:00
|
|
|
<ShellSubHeading
|
|
|
|
className="mt-6"
|
2021-11-07 15:52:48 +00:00
|
|
|
title={<SubHeadingTitleWithConnections title={t("connect_an_additional_calendar")} />}
|
2021-10-30 15:54:21 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<CalendarList onChanged={onChanged} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|