Feature/cal 274 add webhooks (#628)

* added prisma models and migration, minor webhook init --WIP

* --WIP

* --WIP

* added radix-checkbox and other webhook additions --WIP

* added API connections and other modifications --WIP

* --WIP

* replaced checkbox with toggle --WIP

* updated to use Dialog instead of modal --WIP

* fixed API and other small fixes -WIP

* created a dummy hook for test --WIP

* replaced static hook with dynamic hooks

* yarn lock conflict quickfix

* added cancel event hook and other minor additions --WIP

* minor improvements --WIP

* added more add-webhook flow items--WIP

* updated migration to have alter table for eventType

* many ui/ux fixes, logic fixes and action fixes --WIP

* bugfix for incorrect webhook filtering

* some more fixes, edit webhook --WIP

* removed redundant checkbox

* more bugfixes and edit-webhook flow --WIP

* more build and lint fixes

* --WIP

* more fixes and added toast notif --WIP

* --updated iconButton

* clean-up

* fixed enabled check in edit webhook

* another fix

* fixed edit webhook bug

* added await to payload lambda

* wrapped payload call in promise

* fixed cancel/uid CTA alignment

* --requested changes --removed eventType relationship

* Adds missing migration

* Fixes missing daysjs plugin and type fixes

* Adds failsafe for webhooks

* Adds missing dayjs utc plugins

* Fixed schema and migrations

* Updates webhooks query

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Omar López <[email protected]>
This commit is contained in:
authored and GitHub committed 2021-10-04 17:40:52 -06:00
1 parent 785058558c
commit 4c07faefe7
19 files changed
+956 -62

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@ export default function SettingsShell({ children }: { children: React.ReactNode
href: "/settings/security",
icon: KeyIcon,
},
{ name: "Embed", href: "/settings/embed", icon: CodeIcon },
{ name: "Embed & Webhooks", href: "/settings/embed", icon: CodeIcon },
{
name: "Teams",
href: "/settings/teams",
+176
View File
@@ -0,0 +1,176 @@
import { ArrowLeftIcon } from "@heroicons/react/solid";
import { EventType } from "@prisma/client";
import { useEffect, useRef, useState } from "react";
import showToast from "@lib/notification";
import { Webhook } from "@lib/webhook";
import Button from "@components/ui/Button";
import Switch from "@components/ui/Switch";
export default function EditTeam(props: {
webhook: Webhook;
eventTypes: EventType[];
onCloseEdit: () => void;
}) {
const [bookingCreated, setBookingCreated] = useState(
props.webhook.eventTriggers.includes("booking_created")
);
const [bookingRescheduled, setBookingRescheduled] = useState(
props.webhook.eventTriggers.includes("booking_rescheduled")
);
const [bookingCancelled, setBookingCancelled] = useState(
props.webhook.eventTriggers.includes("booking_cancelled")
);
const [webhookEnabled, setWebhookEnabled] = useState(props.webhook.active);
const [webhookEventTrigger, setWebhookEventTriggers] = useState([
"BOOKING_CREATED",
"BOOKING_RESCHEDULED",
"BOOKING_CANCELLED",
]);
const [btnLoading, setBtnLoading] = useState(false);
const subUrlRef = useRef<HTMLInputElement>() as React.MutableRefObject<HTMLInputElement>;
useEffect(() => {
const arr = [];
bookingCreated && arr.push("BOOKING_CREATED");
bookingRescheduled && arr.push("BOOKING_RESCHEDULED");
bookingCancelled && arr.push("BOOKING_CANCELLED");
setWebhookEventTriggers(arr);
}, [bookingCreated, bookingRescheduled, bookingCancelled, webhookEnabled]);
const handleErrors = async (resp: Response) => {
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.message);
}
return resp.json();
};
const updateWebhookHandler = (event) => {
event.preventDefault();
setBtnLoading(true);
return fetch("/api/webhooks/" + props.webhook.id, {
method: "PATCH",
body: JSON.stringify({
subscriberUrl: subUrlRef.current.value,
eventTriggers: webhookEventTrigger,
enabled: webhookEnabled,
}),
headers: {
"Content-Type": "application/json",
},
})
.then(handleErrors)
.then(() => {
showToast("Webhook updated successfully!", "success");
setBtnLoading(false);
});
};
return (
<div className="divide-y divide-gray-200 lg:col-span-9">
<div className="py-6 lg:pb-8">
<div className="mb-4">
<Button
type="button"
color="secondary"
size="sm"
loading={btnLoading}
StartIcon={ArrowLeftIcon}
onClick={() => props.onCloseEdit()}>
Back
</Button>
</div>
<div>
<div className="pb-5 pr-4 sm:pb-6">
<h3 className="text-lg font-bold leading-6 text-gray-900">Manage your webhook</h3>
</div>
</div>
<hr className="mt-2" />
<form className="divide-y divide-gray-200 lg:col-span-9" onSubmit={updateWebhookHandler}>
<div className="my-4">
<div className="mb-4">
<label htmlFor="subUrl" className="block text-sm font-medium text-gray-700">
Subscriber Url
</label>
<input
ref={subUrlRef}
type="text"
name="subUrl"
id="subUrl"
defaultValue={props.webhook.subscriberUrl || ""}
placeholder="https://example.com/sub"
required
className="block w-full px-3 py-2 mt-1 border border-gray-300 rounded-sm shadow-sm focus:outline-none focus:ring-neutral-500 focus:border-neutral-500 sm:text-sm"
/>
<legend className="block pt-4 mb-2 text-sm font-medium text-gray-700"> Event Triggers </legend>
<div className="p-2 bg-white border border-gray-300 rounded-sm">
<div className="flex p-2">
<div className="w-10/12">
<h2 className="text-sm text-gray-800">Booking Created</h2>
</div>
<div className="flex items-center justify-end w-2/12 text-right">
<Switch
defaultChecked={bookingCreated}
onCheckedChange={() => {
setBookingCreated(!bookingCreated);
}}
/>
</div>
</div>
<div className="flex px-2 py-1">
<div className="w-10/12">
<h2 className="text-sm text-gray-800">Booking Rescheduled</h2>
</div>
<div className="flex items-center justify-end w-2/12 text-right">
<Switch
defaultChecked={bookingRescheduled}
onCheckedChange={() => {
setBookingRescheduled(!bookingRescheduled);
}}
/>
</div>
</div>
<div className="flex p-2">
<div className="w-10/12">
<h2 className="text-sm text-gray-800">Booking Cancelled</h2>
</div>
<div className="flex items-center justify-end w-2/12 text-right">
<Switch
defaultChecked={bookingCancelled}
onCheckedChange={() => {
setBookingCancelled(!bookingCancelled);
}}
/>
</div>
</div>
</div>
<legend className="block pt-4 mb-2 text-sm font-medium text-gray-700"> Webhook Status </legend>
<div className="p-2 bg-white border border-gray-300 rounded-sm">
<div className="flex p-2">
<div className="w-10/12">
<h2 className="text-sm text-gray-800">Webhook Enabled</h2>
</div>
<div className="flex items-center justify-end w-2/12 text-right">
<Switch
defaultChecked={webhookEnabled}
onCheckedChange={() => {
setWebhookEnabled(!webhookEnabled);
}}
/>
</div>
</div>
</div>
</div>
<div className="gap-2 mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
<Button type="submit" color="primary" className="ml-2" loading={btnLoading}>
Save
</Button>
</div>
</div>
</form>
</div>
</div>
);
}
+23
View File
@@ -0,0 +1,23 @@
import { Webhook } from "@lib/webhook";
import WebhookListItem from "./WebhookListItem";
export default function WebhookList(props: {
webhooks: Webhook[];
onChange: () => void;
onEditWebhook: (webhook: Webhook) => void;
}) {
return (
<div>
<ul className="px-4 mb-2 bg-white border divide-y divide-gray-200 rounded">
{props.webhooks.map((webhook: Webhook) => (
<WebhookListItem
onChange={props.onChange}
key={webhook.id}
webhook={webhook}
onEditWebhook={() => props.onEditWebhook(webhook)}></WebhookListItem>
))}
</ul>
</div>
);
}
+105
View File
@@ -0,0 +1,105 @@
import { TrashIcon, PencilAltIcon } from "@heroicons/react/outline";
import showToast from "@lib/notification";
import { Webhook } from "@lib/webhook";
import { Dialog, DialogTrigger } from "@components/Dialog";
import { Tooltip } from "@components/Tooltip";
import ConfirmationDialogContent from "@components/dialog/ConfirmationDialogContent";
import Button from "@components/ui/Button";
export default function WebhookListItem(props: {
onChange: () => void;
key: number;
webhook: Webhook;
onEditWebhook: () => void;
}) {
const handleErrors = async (resp: Response) => {
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.message);
}
return resp.json();
};
const deleteWebhook = (webhookId: string) => {
fetch("/api/webhooks/" + webhookId, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
})
.then(handleErrors)
.then(() => {
showToast("Webhook removed successfully!", "success");
props.onChange();
});
};
return (
<li className="divide-y">
<div className="flex justify-between my-4">
<div className="flex pr-2 border-r border-gray-100">
<span className="flex flex-col space-y-2 text-xs">
{props.webhook.eventTriggers.map((eventTrigger, ind) => (
<span key={ind} className="px-1 text-xs text-blue-700 rounded-md w-max bg-blue-50">
{eventTrigger}
</span>
))}
</span>
</div>
<div className="flex w-full">
<div className="self-center inline-block ml-3 space-y-1">
<span className="flex text-sm text-neutral-700">{props.webhook.subscriberUrl}</span>
</div>
</div>
<div className="flex">
{!props.webhook.active && (
<span className="self-center h-6 px-3 py-1 text-xs text-red-700 capitalize rounded-md bg-red-50">
Disabled
</span>
)}
{!!props.webhook.active && (
<span className="self-center h-6 px-3 py-1 text-xs text-green-700 capitalize rounded-md bg-green-50">
Enabled
</span>
)}
<Tooltip content="Edit Webhook">
<Button
onClick={() => props.onEditWebhook()}
color="minimal"
size="icon"
StartIcon={PencilAltIcon}
className="self-center w-full p-2 ml-4"></Button>
</Tooltip>
<Dialog>
<Tooltip content="Delete Webhook">
<DialogTrigger asChild>
<Button
onClick={(e) => {
e.stopPropagation();
}}
color="minimal"
size="icon"
StartIcon={TrashIcon}
className="self-center w-full p-2 ml-2"></Button>
</DialogTrigger>
</Tooltip>
<ConfirmationDialogContent
variety="danger"
title="Delete Webhook"
confirmBtnText="Yes, delete webhook"
cancelBtnText="Cancel"
onConfirm={() => {
deleteWebhook(props.webhook.id);
}}>
Are you sure you want to delete this webhook? You will no longer receive Cal.com meeting data at
a specified URL, in real-time, when an event is scheduled or canceled .
</ConfirmationDialogContent>
</Dialog>
</div>
</div>
</li>
);
}