2022-11-17 21:38:34 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { useRouter } from "next/router";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-11-17 21:38:34 +00:00
|
|
|
import { Toaster } from "react-hot-toast";
|
|
|
|
import z from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, Form, showToast, TextField } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Check, X } from "@calcom/ui/components/icon";
|
2022-11-17 21:38:34 +00:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
api_key: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export default function SendgridSetup() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const [testPassed, setTestPassed] = useState<boolean | undefined>(undefined);
|
|
|
|
const [testLoading, setTestLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const form = useForm<{
|
|
|
|
api_key: string;
|
|
|
|
}>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
if (testPassed === false) {
|
|
|
|
setTestPassed(undefined);
|
|
|
|
}
|
|
|
|
}, 3000);
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
}, [testPassed]);
|
|
|
|
|
|
|
|
return (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-emphasis flex h-screen">
|
|
|
|
<div className="bg-default m-auto rounded p-5 md:w-[520px] md:p-10">
|
2022-11-17 21:38:34 +00:00
|
|
|
<div className="flex flex-col space-y-5 md:flex-row md:space-y-0 md:space-x-5">
|
|
|
|
<div>
|
|
|
|
<img src="/api/app-store/sendgrid/logo.png" alt="Sendgrid" className="h-12 w-12 max-w-2xl" />
|
|
|
|
</div>
|
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<h1 className="text-default">{t("provide_api_key")}</h1>
|
2022-11-17 21:38:34 +00:00
|
|
|
|
|
|
|
<div className="mt-1 text-sm">
|
2023-03-20 11:18:46 +00:00
|
|
|
{t("generate_api_key_description", { appName: "Sendgrid" })}{" "}
|
2022-11-17 21:38:34 +00:00
|
|
|
<a
|
|
|
|
className="text-indigo-400"
|
|
|
|
href="https://app.sendgrid.com/settings/api_keys"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer">
|
|
|
|
Sendgrid
|
|
|
|
</a>
|
|
|
|
. {t("it_stored_encrypted")}
|
|
|
|
</div>
|
|
|
|
<div className="my-2 mt-3">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
const res = await fetch("/api/integrations/sendgrid/add", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify(values),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const json = await res.json();
|
|
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
router.push(json.url);
|
|
|
|
} else {
|
|
|
|
showToast(json.message, "error");
|
|
|
|
}
|
|
|
|
}}>
|
|
|
|
<fieldset className="space-y-2" disabled={form.formState.isSubmitting}>
|
|
|
|
<Controller
|
|
|
|
name="api_key"
|
|
|
|
control={form.control}
|
|
|
|
render={({ field: { onBlur, onChange } }) => (
|
|
|
|
<TextField
|
|
|
|
className="my-0"
|
|
|
|
onBlur={onBlur}
|
|
|
|
disabled={testPassed === true}
|
|
|
|
name="api_key"
|
2023-05-11 23:04:10 +00:00
|
|
|
placeholder="SG.xxxxxx..."
|
2022-11-17 21:38:34 +00:00
|
|
|
onChange={async (e) => {
|
|
|
|
onChange(e.target.value);
|
|
|
|
form.setValue("api_key", e.target.value);
|
|
|
|
await form.trigger("api_key");
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</fieldset>
|
2023-01-04 07:38:45 +00:00
|
|
|
<div className="mt-5 justify-end space-x-2 rtl:space-x-reverse sm:mt-4 sm:flex">
|
2022-11-17 21:38:34 +00:00
|
|
|
<Button type="button" color="secondary" onClick={() => router.back()}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
loading={testLoading}
|
|
|
|
disabled={testPassed === true}
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={testPassed !== undefined ? (testPassed ? Check : X) : undefined}
|
2022-11-17 21:38:34 +00:00
|
|
|
className={
|
|
|
|
testPassed !== undefined
|
|
|
|
? testPassed
|
2023-04-05 18:14:46 +00:00
|
|
|
? " !bg-success hover:bg-success !text-green-700"
|
|
|
|
: "bg-error hover:bg-error !border-red-700 !text-red-700"
|
2022-11-17 21:38:34 +00:00
|
|
|
: "secondary"
|
|
|
|
}
|
|
|
|
color={testPassed === true ? "minimal" : "secondary"}
|
|
|
|
onClick={async () => {
|
|
|
|
const check = await form.trigger("api_key");
|
|
|
|
if (!check) return;
|
|
|
|
const api_key = form.getValues("api_key");
|
|
|
|
setTestLoading(true);
|
|
|
|
const res = await fetch("/api/integrations/sendgrid/check", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ api_key }),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
setTestPassed(true);
|
|
|
|
} else {
|
|
|
|
setTestPassed(false);
|
|
|
|
}
|
|
|
|
setTestLoading(false);
|
|
|
|
}}>
|
|
|
|
{t(
|
|
|
|
testPassed !== undefined ? (testPassed ? "test_passed" : "test_failed") : "test_api_key"
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Toaster position="bottom-right" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|