2022-09-12 19:07:52 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import z from "zod";
|
|
|
|
|
2022-11-30 21:52:56 +00:00
|
|
|
import { APP_NAME } from "@calcom/lib/constants";
|
2022-09-12 19:07:52 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { getSettingsLayout as getLayout, Meta, showToast, SkeletonContainer } from "@calcom/ui";
|
2022-09-12 19:07:52 +00:00
|
|
|
|
|
|
|
import WebhookForm, { WebhookFormSubmitData } from "../components/WebhookForm";
|
|
|
|
|
|
|
|
const querySchema = z.object({ id: z.string() });
|
|
|
|
|
|
|
|
const EditWebhook = () => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const utils = trpc.useContext();
|
|
|
|
const router = useRouter();
|
|
|
|
|
2022-09-22 17:23:43 +00:00
|
|
|
function Component({ webhookId }: { webhookId: string }) {
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: installedApps, isLoading } = trpc.viewer.integrations.useQuery(
|
|
|
|
{ variant: "other", onlyInstalled: true },
|
2022-09-22 17:23:43 +00:00
|
|
|
{
|
|
|
|
suspense: true,
|
|
|
|
enabled: router.isReady,
|
|
|
|
}
|
|
|
|
);
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: webhook } = trpc.viewer.webhook.get.useQuery(
|
|
|
|
{ webhookId },
|
|
|
|
{
|
|
|
|
suspense: true,
|
|
|
|
enabled: router.isReady,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const { data: webhooks } = trpc.viewer.webhook.list.useQuery(undefined, {
|
2022-09-22 17:23:43 +00:00
|
|
|
suspense: true,
|
|
|
|
enabled: router.isReady,
|
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
const editWebhookMutation = trpc.viewer.webhook.edit.useMutation({
|
2022-09-22 17:23:43 +00:00
|
|
|
async onSuccess() {
|
2022-11-10 23:40:01 +00:00
|
|
|
await utils.viewer.webhook.list.invalidate();
|
2022-09-22 17:23:43 +00:00
|
|
|
showToast(t("webhook_updated_successfully"), "success");
|
|
|
|
router.back();
|
|
|
|
},
|
|
|
|
onError(error) {
|
|
|
|
showToast(`${error.message}`, "error");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const subscriberUrlReserved = (subscriberUrl: string, id: string): boolean => {
|
|
|
|
return !!webhooks?.find((webhook) => webhook.subscriberUrl === subscriberUrl && webhook.id !== id);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isLoading || !webhook) return <SkeletonContainer />;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Meta
|
|
|
|
title="Edit Webhook"
|
2022-11-30 21:52:56 +00:00
|
|
|
description={t("add_webhook_description", { appName: APP_NAME })}
|
2022-09-22 17:23:43 +00:00
|
|
|
backButton
|
|
|
|
/>
|
|
|
|
<WebhookForm
|
|
|
|
webhook={webhook}
|
|
|
|
onSubmit={(values: WebhookFormSubmitData) => {
|
|
|
|
if (subscriberUrlReserved(values.subscriberUrl, webhook.id)) {
|
|
|
|
showToast(t("webhook_subscriber_url_reserved"), "error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (values.changeSecret) {
|
|
|
|
values.secret = values.newSecret.length ? values.newSecret : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!values.payloadTemplate) {
|
|
|
|
values.payloadTemplate = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
editWebhookMutation.mutate({
|
|
|
|
id: webhook.id,
|
|
|
|
subscriberUrl: values.subscriberUrl,
|
|
|
|
eventTriggers: values.eventTriggers,
|
|
|
|
active: values.active,
|
|
|
|
payloadTemplate: values.payloadTemplate,
|
|
|
|
secret: values.secret,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
apps={installedApps?.items.map((app) => app.slug)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!router.isReady) return null;
|
|
|
|
|
|
|
|
const parsed = querySchema.safeParse(router.query);
|
|
|
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
throw new Error("Invalid query");
|
|
|
|
}
|
|
|
|
|
|
|
|
// tRPC useQuery needs webhookId to be available and it becomes available only after router.isReady is true
|
|
|
|
// It causes this requirement of a new component.
|
|
|
|
// I think we should do SSR for this page
|
|
|
|
return <Component webhookId={parsed.data.id} />;
|
2022-09-12 19:07:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
EditWebhook.getLayout = getLayout;
|
|
|
|
|
|
|
|
export default EditWebhook;
|