import { Button, IconButton, Input } from "@mui/joy"; import React, { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { webhookServiceClient } from "@/grpcweb"; import useLoading from "@/hooks/useLoading"; import { useTranslate } from "@/utils/i18n"; import { generateDialog } from "./Dialog"; import Icon from "./Icon"; interface Props extends DialogProps { webhookId?: number; onConfirm: () => void; } interface State { name: string; url: string; } const CreateWebhookDialog: React.FC = (props: Props) => { const { webhookId, destroy, onConfirm } = props; const t = useTranslate(); const [state, setState] = useState({ name: "", url: "", }); const requestState = useLoading(false); const isCreating = webhookId === undefined; useEffect(() => { if (webhookId) { webhookServiceClient .getWebhook({ id: webhookId, }) .then(({ webhook }) => { if (!webhook) { return; } setState({ name: webhook.name, url: webhook.url, }); }); } }, []); const setPartialState = (partialState: Partial) => { setState({ ...state, ...partialState, }); }; const handleTitleInputChange = (e: React.ChangeEvent) => { setPartialState({ name: e.target.value, }); }; const handleUrlInputChange = (e: React.ChangeEvent) => { setPartialState({ url: e.target.value, }); }; const handleSaveBtnClick = async () => { if (!state.name || !state.url) { toast.error("Please fill all required fields"); return; } try { if (isCreating) { await webhookServiceClient.createWebhook({ name: state.name, url: state.url, }); } else { await webhookServiceClient.updateWebhook({ webhook: { id: webhookId, name: state.name, url: state.url, }, updateMask: ["name", "url"], }); } onConfirm(); destroy(); } catch (error: any) { console.error(error); toast.error(error.details); } }; return ( <>

{isCreating ? "Create webhook" : "Edit webhook"}

destroy()}>
Title *
Payload URL *
); }; function showCreateWebhookDialog(onConfirm: () => void) { generateDialog( { className: "create-webhook-dialog", dialogName: "create-webhook-dialog", }, CreateWebhookDialog, { onConfirm, }, ); } export default showCreateWebhookDialog;