cal.pub0.org/packages/app-store/sendgrid/pages/setup/index.tsx

150 lines
5.5 KiB
TypeScript
Raw Normal View History

Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2022-11-17 21:38:34 +00:00
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
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";
import { Button, Form, Icon, showToast, TextField } from "@calcom/ui";
Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
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 (
<div className="flex h-screen bg-gray-200">
<div className="m-auto rounded bg-white p-5 md:w-[520px] md:p-10">
<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>
<h1 className="text-gray-600">{t("provide_api_key")}</h1>
<div className="mt-1 text-sm">
{t("generate_api_key_description")}{" "}
<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"
placeholder="api_xyz..."
onChange={async (e) => {
onChange(e.target.value);
form.setValue("api_key", e.target.value);
await form.trigger("api_key");
}}
/>
)}
/>
</fieldset>
<div className="mt-5 justify-end space-x-2 sm:mt-4 sm:flex">
<Button type="button" color="secondary" onClick={() => router.back()}>
{t("cancel")}
</Button>
<Button
type="submit"
loading={testLoading}
disabled={testPassed === true}
StartIcon={testPassed !== undefined ? (testPassed ? Icon.FiCheck : Icon.FiX) : undefined}
className={
testPassed !== undefined
? testPassed
? " !bg-green-100 !text-green-700 hover:bg-green-100"
: "!border-red-700 bg-red-100 !text-red-700 hover:bg-red-100"
: "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>
);
}