2022-05-03 23:16:59 +00:00
|
|
|
import { Trans } from "next-i18next";
|
|
|
|
import Link from "next/link";
|
|
|
|
import { useState } from "react";
|
2022-05-11 04:58:10 +00:00
|
|
|
import { Toaster } from "react-hot-toast";
|
2022-05-03 23:16:59 +00:00
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, showToast, Tooltip } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Clipboard } from "@calcom/ui/components/icon";
|
2022-05-03 23:16:59 +00:00
|
|
|
|
2022-05-11 04:58:10 +00:00
|
|
|
export interface IZapierSetupProps {
|
2023-06-06 11:59:57 +00:00
|
|
|
inviteLink?: string;
|
2022-05-03 23:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ZAPIER = "zapier";
|
|
|
|
|
|
|
|
export default function ZapierSetup(props: IZapierSetupProps) {
|
|
|
|
const [newApiKey, setNewApiKey] = useState("");
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
2022-11-10 23:40:01 +00:00
|
|
|
const integrations = trpc.viewer.integrations.useQuery({ variant: "automation" });
|
|
|
|
const oldApiKey = trpc.viewer.apiKeys.findKeyOfType.useQuery({ appId: ZAPIER });
|
2022-05-11 04:58:10 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const deleteApiKey = trpc.viewer.apiKeys.delete.useMutation();
|
2022-06-01 17:24:41 +00:00
|
|
|
const zapierCredentials: { credentialIds: number[] } | undefined = integrations.data?.items.find(
|
2022-09-27 12:01:54 +00:00
|
|
|
(item: { type: string }) => item.type === "zapier_automation"
|
2022-05-03 23:16:59 +00:00
|
|
|
);
|
|
|
|
const [credentialId] = zapierCredentials?.credentialIds || [false];
|
|
|
|
const showContent = integrations.data && integrations.isSuccess && credentialId;
|
2022-05-18 15:35:54 +00:00
|
|
|
const isCalDev = process.env.NEXT_PUBLIC_WEBAPP_URL === "https://app.cal.dev";
|
2022-05-03 23:16:59 +00:00
|
|
|
|
|
|
|
async function createApiKey() {
|
|
|
|
const event = { note: "Zapier", expiresAt: null, appId: ZAPIER };
|
2022-11-10 23:40:01 +00:00
|
|
|
const apiKey = await utils.client.viewer.apiKeys.create.mutate(event);
|
2022-05-03 23:16:59 +00:00
|
|
|
if (oldApiKey.data) {
|
|
|
|
deleteApiKey.mutate({
|
|
|
|
id: oldApiKey.data.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setNewApiKey(apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (integrations.isLoading) {
|
2023-04-05 18:14:46 +00:00
|
|
|
return <div className="bg-emphasis absolute z-50 flex h-screen w-full items-center" />;
|
2022-05-03 23:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-emphasis flex h-screen">
|
2022-05-03 23:16:59 +00:00
|
|
|
{showContent ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-default m-auto max-w-[43em] overflow-auto rounded pb-10 md:p-10">
|
2022-08-31 07:43:40 +00:00
|
|
|
<div className="md:flex md:flex-row">
|
|
|
|
<div className="invisible md:visible">
|
|
|
|
<img className="h-11" src="/api/app-store/zapier/icon.svg" alt="Zapier Logo" />
|
2022-05-03 23:16:59 +00:00
|
|
|
</div>
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="ml-2 ltr:mr-2 rtl:ml-2 md:ml-5">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-default">{t("setting_up_zapier")}</div>
|
2022-05-03 23:16:59 +00:00
|
|
|
{!newApiKey ? (
|
|
|
|
<>
|
|
|
|
<div className="mt-1 text-xl">{t("generate_api_key")}:</div>
|
2023-06-22 22:25:37 +00:00
|
|
|
<Button color="primary" onClick={() => createApiKey()} className="mb-4 mt-4">
|
2022-05-03 23:16:59 +00:00
|
|
|
{t("generate_api_key")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="mt-1 text-xl">{t("your_unique_api_key")}</div>
|
2022-10-27 05:50:16 +00:00
|
|
|
<div className="my-2 mt-3 flex-wrap sm:flex sm:flex-nowrap">
|
2023-04-05 18:14:46 +00:00
|
|
|
<code className="bg-subtle h-full w-full whitespace-pre-wrap rounded-md py-[6px] pl-2 pr-2 sm:rounded-r-none sm:pr-5">
|
2022-10-27 05:50:16 +00:00
|
|
|
{newApiKey}
|
|
|
|
</code>
|
|
|
|
<Tooltip side="top" content={t("copy_to_clipboard")}>
|
2022-05-03 23:16:59 +00:00
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(newApiKey);
|
|
|
|
showToast(t("api_key_copied"), "success");
|
|
|
|
}}
|
|
|
|
type="button"
|
2022-10-27 05:50:16 +00:00
|
|
|
className="mt-4 text-base sm:mt-0 sm:rounded-l-none">
|
2023-04-12 15:26:31 +00:00
|
|
|
<Clipboard className="h-5 w-5 text-gray-100 ltr:mr-2 rtl:ml-2" />
|
2022-05-03 23:16:59 +00:00
|
|
|
{t("copy")}
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className="text-default mb-5 mt-2 text-sm font-semibold">{t("copy_safe_api_key")}</div>
|
2022-05-03 23:16:59 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2023-06-22 22:25:37 +00:00
|
|
|
<ol className="mb-5 ml-5 mt-5 list-decimal ltr:mr-5 rtl:ml-5">
|
2022-05-18 15:35:54 +00:00
|
|
|
{isCalDev && (
|
2022-05-11 04:58:10 +00:00
|
|
|
<li>
|
2022-05-18 15:35:54 +00:00
|
|
|
{t("go_to")}
|
2022-05-11 04:58:10 +00:00
|
|
|
<a href={props.inviteLink} className="text-orange-600 underline">
|
2022-05-18 15:35:54 +00:00
|
|
|
{t("zapier_invite_link")}
|
2022-05-11 04:58:10 +00:00
|
|
|
</a>
|
|
|
|
</li>
|
2022-06-01 17:24:41 +00:00
|
|
|
)}
|
2022-05-18 15:35:54 +00:00
|
|
|
<Trans i18nKey="zapier_setup_instructions">
|
2022-05-03 23:16:59 +00:00
|
|
|
<li>Log into your Zapier account and create a new Zap.</li>
|
|
|
|
<li>Select Cal.com as your Trigger app. Also choose a Trigger event.</li>
|
|
|
|
<li>Choose your account and then enter your Unique API Key.</li>
|
|
|
|
<li>Test your Trigger.</li>
|
|
|
|
<li>You're set!</li>
|
|
|
|
</Trans>
|
|
|
|
</ol>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href="/apps/installed/automation?hl=zapier" passHref={true} legacyBehavior>
|
2022-05-03 23:16:59 +00:00
|
|
|
<Button color="secondary">{t("done")}</Button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className="ml-5 mt-5">
|
2022-05-03 23:16:59 +00:00
|
|
|
<div>{t("install_zapier_app")}</div>
|
|
|
|
<div className="mt-3">
|
2023-01-06 12:13:56 +00:00
|
|
|
<Link href="/apps/zapier" passHref={true} legacyBehavior>
|
2022-05-03 23:16:59 +00:00
|
|
|
<Button>{t("go_to_app_store")}</Button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-11 04:58:10 +00:00
|
|
|
<Toaster position="bottom-right" />
|
2022-05-03 23:16:59 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|