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() as React.MutableRefObject; 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 (

{t("manage_your_webhook")}


{" "} {t("event_triggers")}{" "}

{t("booking_created")}

{ setBookingCreated(!bookingCreated); }} />

{t("booking_rescheduled")}

{ setBookingRescheduled(!bookingRescheduled); }} />

{t("booking_cancelled")}

{ setBookingCancelled(!bookingCancelled); }} />
{" "} {t("webhook_status")}{" "}

{t("webhook_enabled")}

{ setWebhookEnabled(!webhookEnabled); }} />
); }