From 5ebb0d939a15ffc92cac54017e02c2964e4cb0c7 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 2 Jun 2022 11:04:17 +0800 Subject: [PATCH] Add edit webhook modal --- packages/web/pages/settings/webhooks.tsx | 75 ++++++++++++++++++------ 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/packages/web/pages/settings/webhooks.tsx b/packages/web/pages/settings/webhooks.tsx index f7093cfc7..495aa42f5 100644 --- a/packages/web/pages/settings/webhooks.tsx +++ b/packages/web/pages/settings/webhooks.tsx @@ -6,21 +6,43 @@ import { useGetWebhooksQuery, WebhookEvent, } from '../../lib/networking/queries/useGetWebhooksQuery' -import { useState } from 'react' +import { useEffect, useState } from 'react' import { showErrorToast, showSuccessToast } from '../../lib/toastHelpers' import { ConfirmationModal } from '../../components/patterns/ConfirmationModal' import { deleteWebhookMutation } from '../../lib/networking/mutations/deleteWebhookMutation' import { FormInputProps, FormModal } from '../../components/patterns/FormModal' import { setWebhookMutation } from '../../lib/networking/mutations/setWebhookMutation' +import { useGetWebhookQuery } from '../../lib/networking/queries/useGetWebhookQuery' export default function Webhooks(): JSX.Element { const { webhooks, revalidate } = useGetWebhooksQuery() - const [deleteId, setDeleteId] = useState(null) + const [onDeleteId, setOnDeleteId] = useState(null) const [addModelOpen, setAddModelOpen] = useState(false) - const [editModelOpen, setEditModelOpen] = useState(false) - const [id, setId] = useState('') + const [onEditId, setOnEditId] = useState('') const [url, setUrl] = useState('') const [eventTypes, setEventTypes] = useState([]) + const [formInputs, setFormInputs] = useState([ + { + label: 'URL', + onChange: setUrl, + name: 'url', + placeholder: 'https://example.com/webhook', + }, + ]) + const { webhook } = useGetWebhookQuery(onEditId) + + useEffect(() => { + if (webhook) { + setFormInputs([ + { + label: 'URL', + onChange: setUrl, + name: 'url', + value: webhook.url, + }, + ]) + } + }, [webhook]) applyStoredTheme(false) @@ -48,6 +70,20 @@ export default function Webhooks(): JSX.Element { revalidate() } + async function onUpdate(): Promise { + const result = await setWebhookMutation(onEditId, url, [ + 'PAGE_CREATED', + 'HIGHLIGHT_CREATED', + ]) + if (result) { + showSuccessToast('Updated', { position: 'bottom-right' }) + } else { + showErrorToast('Failed to update', { position: 'bottom-right' }) + } + setUrl('') + revalidate() + } + const headers = ['URL', 'Event Types', 'Enabled'] const rows = new Map() webhooks.forEach((webhook) => @@ -57,14 +93,6 @@ export default function Webhooks(): JSX.Element { webhook.enabled ? 'Yes' : 'No', ]) ) - const addFormInputs: FormInputProps[] = [ - { - label: 'URL', - onChange: setUrl, - name: 'url', - placeholder: 'https://example.com/webhook', - }, - ] return ( @@ -79,29 +107,40 @@ export default function Webhooks(): JSX.Element { title={'Add webhook'} onSubmit={onAdd} onOpenChange={setAddModelOpen} - inputs={addFormInputs} + inputs={formInputs} acceptButtonLabel={'Add'} /> )} - {deleteId && ( + {onEditId && ( + setOnEditId('')} + inputs={formInputs} + acceptButtonLabel={'Update'} + /> + )} + + {onDeleteId && ( { - await onDelete(deleteId) - setDeleteId(null) + await onDelete(onDeleteId) + setOnDeleteId(null) }} - onOpenChange={() => setDeleteId(null)} + onOpenChange={() => setOnDeleteId(null)} /> )} setAddModelOpen(true)} + onUpdate={setOnEditId} /> )