refactor webhooks UI (#982)

This commit is contained in:
Alex Johansson authored and GitHub committed 2021-10-18 08:02:25 +01:00
1 parent 12f6065d84
commit d8dac426eb
13 files changed
+374 -616

No files matched your search

+5 -5
View File
@@ -25,17 +25,17 @@ export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps
);
type DialogHeaderProps = {
title: React.ReactElement | string;
subtitle: React.ReactElement | string;
title: React.ReactNode;
subtitle?: React.ReactNode;
};
export function DialogHeader({ title, subtitle }: DialogHeaderProps) {
export function DialogHeader(props: DialogHeaderProps) {
return (
<div className="mb-8">
<h3 className="font-cal text-gray-900 text-lg font-bold leading-6" id="modal-title">
{title}
{props.title}
</h3>
<div className="text-gray-400 text-sm">{subtitle}</div>
{props.subtitle && <div className="text-gray-400 text-sm">{props.subtitle}</div>}
</div>
);
}
+20 -1
View File
@@ -4,7 +4,8 @@ import { FormProvider, UseFormReturn } from "react-hook-form";
import classNames from "@lib/classNames";
export const Input = forwardRef<HTMLInputElement, JSX.IntrinsicElements["input"]>(function Input(props, ref) {
type InputProps = Omit<JSX.IntrinsicElements["input"], "name"> & { name: string };
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(props, ref) {
return (
<input
{...props}
@@ -61,3 +62,21 @@ export const Form = forwardRef<HTMLFormElement, { form: UseFormReturn<any> } & J
);
}
);
export function FieldsetLegend(props: JSX.IntrinsicElements["legend"]) {
return (
<legend {...props} className={classNames("text-sm font-medium text-gray-700", props.className)}>
{props.children}
</legend>
);
}
export function InputGroupBox(props: JSX.IntrinsicElements["div"]) {
return (
<div
{...props}
className={classNames("p-2 bg-white border border-gray-300 rounded-sm space-y-2", props.className)}>
{props.children}
</div>
);
}
+6 -5
View File
@@ -1,13 +1,14 @@
import { useId } from "@radix-ui/react-id";
import * as Label from "@radix-ui/react-label";
import * as PrimitiveSwitch from "@radix-ui/react-switch";
import { useState } from "react";
import React, { useState } from "react";
function classNames(...classes) {
return classes.filter(Boolean).join(" ");
}
import classNames from "@lib/classNames";
export default function Switch(props) {
type SwitchProps = React.ComponentProps<typeof PrimitiveSwitch.Root> & {
label: string;
};
export default function Switch(props: SwitchProps) {
const { label, onCheckedChange, ...primitiveProps } = props;
const [checked, setChecked] = useState(props.defaultChecked || false);
-179
View File
@@ -1,179 +0,0 @@
import { ArrowLeftIcon } from "@heroicons/react/solid";
import { useEffect, useRef, useState } from "react";
import { useLocale } from "@lib/hooks/useLocale";
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; onCloseEdit: () => void }) {
const { t } = useLocale();
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(t("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()}>
{t("back")}
</Button>
</div>
<div>
<div className="pb-5 pr-4 sm:pb-6">
<h3 className="text-lg font-bold leading-6 text-gray-900">{t("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">
{t("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">
{" "}
{t("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">{t("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">{t("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">{t("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">
{" "}
{t("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">{t("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}>
{t("save")}
</Button>
</div>
</div>
</form>
</div>
</div>
);
}
-23
View File
@@ -1,23 +0,0 @@
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>
);
}
-106
View File
@@ -1,106 +0,0 @@
import { TrashIcon, PencilAltIcon } from "@heroicons/react/outline";
import { useLocale } from "@lib/hooks/useLocale";
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: string;
webhook: Webhook;
onEditWebhook: () => void;
}) {
const { t } = useLocale();
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(t("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">
{t(`${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">
{t("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">
{t("enabled")}
</span>
)}
<Tooltip content={t("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={t("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={t("delete_webhook")}
confirmBtnText={t("confirm_delete_webhook")}
cancelBtnText={t("cancel")}
onConfirm={() => {
deleteWebhook(props.webhook.id);
}}>
{t("delete_webhook_confirmation_message")}
</ConfirmationDialogContent>
</Dialog>
</div>
</div>
</li>
);
}