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";
|
2022-10-27 05:50:16 +00:00
|
|
|
import { ClipboardCopyIcon } from "@calcom/ui/Icon";
|
2022-11-04 15:40:46 +00:00
|
|
|
import { Button } from "@calcom/ui/components";
|
|
|
|
import { showToast, Tooltip } from "@calcom/ui/v2";
|
2022-05-03 23:16:59 +00:00
|
|
|
|
2022-05-11 04:58:10 +00:00
|
|
|
export interface IZapierSetupProps {
|
|
|
|
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) {
|
2022-10-27 05:50:16 +00:00
|
|
|
return <div className="absolute z-50 flex h-screen w-full items-center bg-gray-200" />;
|
2022-05-03 23:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex h-screen bg-gray-200">
|
|
|
|
{showContent ? (
|
2022-08-31 07:43:40 +00:00
|
|
|
<div className="m-auto max-w-[43em] overflow-auto rounded bg-white pb-10 md:p-10">
|
|
|
|
<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>
|
2022-08-31 07:43:40 +00:00
|
|
|
<div className="ml-2 mr-2 md:ml-5">
|
|
|
|
<div className="text-gray-600">{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>
|
|
|
|
<Button onClick={() => createApiKey()} className="mt-4 mb-4">
|
|
|
|
{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">
|
|
|
|
<code className="h-full w-full whitespace-pre-wrap rounded-md bg-gray-100 py-2 pl-2 pr-2 sm:rounded-r-none sm:pr-5">
|
|
|
|
{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">
|
2022-06-24 13:37:56 +00:00
|
|
|
<ClipboardCopyIcon className="mr-2 h-5 w-5 text-neutral-100" />
|
2022-05-03 23:16:59 +00:00
|
|
|
{t("copy")}
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 mb-5 text-sm font-semibold text-gray-600">
|
|
|
|
{t("copy_safe_api_key")}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-05-11 04:58:10 +00:00
|
|
|
<ol className="mt-5 mb-5 ml-5 mr-5 list-decimal">
|
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>
|
2022-11-17 21:38:34 +00:00
|
|
|
<Link href="/apps/installed/automation?hl=zapier" passHref={true}>
|
2022-05-03 23:16:59 +00:00
|
|
|
<Button color="secondary">{t("done")}</Button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="mt-5 ml-5">
|
|
|
|
<div>{t("install_zapier_app")}</div>
|
|
|
|
<div className="mt-3">
|
2022-07-12 17:50:04 +00:00
|
|
|
<Link href="/apps/zapier" passHref={true}>
|
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>
|
|
|
|
);
|
|
|
|
}
|