From ff4fb5e56fcb631c2bed3281811b1170e9321899 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 26 May 2023 10:45:15 +0800 Subject: [PATCH] refactor(console): replace confirm modal with useConfirmModal hook (#3906) --- .../src/pages/WebhookDetails/index.tsx | 76 ++++++++----------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/packages/console/src/pages/WebhookDetails/index.tsx b/packages/console/src/pages/WebhookDetails/index.tsx index a99b6d7e0..c7ae3c5ca 100644 --- a/packages/console/src/pages/WebhookDetails/index.tsx +++ b/packages/console/src/pages/WebhookDetails/index.tsx @@ -15,9 +15,7 @@ import WebhookDark from '@/assets/images/webhook-dark.svg'; import Webhook from '@/assets/images/webhook.svg'; import ActionMenu, { ActionMenuItem } from '@/components/ActionMenu'; import Card from '@/components/Card'; -import ConfirmModal from '@/components/ConfirmModal'; import CopyToClipboard from '@/components/CopyToClipboard'; -import DeleteConfirmModal from '@/components/DeleteConfirmModal'; import DetailsPage from '@/components/DetailsPage'; import DynamicT from '@/components/DynamicT'; import PageMeta from '@/components/PageMeta'; @@ -25,6 +23,7 @@ import TabNav, { TabNavItem } from '@/components/TabNav'; import Tag from '@/components/Tag'; import { WebhookDetailsTabs } from '@/consts'; import useApi, { type RequestError } from '@/hooks/use-api'; +import { useConfirmModal } from '@/hooks/use-confirm-modal'; import useTheme from '@/hooks/use-theme'; import { buildUrl } from '@/utils/url'; @@ -49,21 +48,31 @@ function WebhookDetails() { const theme = useTheme(); const WebhookIcon = theme === 'light' ? Webhook : WebhookDark; - const [isDeleteFormOpen, setIsDeleteFormOpen] = useState(false); + const { show, cancel } = useConfirmModal(); + const [isDeleting, setIsDeleting] = useState(false); - const [isDisableFormOpen, setIsDisableFormOpen] = useState(false); const [isUpdatingEnableState, setIsUpdatingEnableState] = useState(false); useEffect(() => { - setIsDeleteFormOpen(false); - setIsDisableFormOpen(false); - }, [data?.enabled, pathname]); + return () => { + cancel(); + }; + }, [cancel, pathname]); - const onDelete = async () => { + const handleDelete = async () => { if (!data || isDeleting) { return; } + const [result] = await show({ + ModalContent: () => , + confirmButtonText: 'general.delete', + }); + + if (!result) { + return; + } + setIsDeleting(true); try { @@ -75,11 +84,22 @@ function WebhookDetails() { } }; - const onToggleEnableState = async () => { + const handleToggleEnableState = async () => { if (!data || isUpdatingEnableState) { return; } + if (isEnabled) { + const [result] = await show({ + ModalContent: () => , + confirmButtonText: 'webhook_details.disable_webhook', + }); + + if (!result) { + return; + } + } + setIsUpdatingEnableState(true); try { @@ -87,7 +107,6 @@ function WebhookDetails() { .patch(`api/hooks/${data.id}`, { json: { enabled: !isEnabled } }) .json(); void mutate({ ...updatedHook, executionStats: data.executionStats }); - setIsDisableFormOpen(false); const { enabled } = updatedHook; toast.success( t(enabled ? 'webhook_details.webhook_reactivated' : 'webhook_details.webhook_disabled') @@ -150,13 +169,7 @@ function WebhookDetails() { : } iconClassName={styles.icon} - onClick={async () => { - if (isEnabled) { - setIsDisableFormOpen(true); - return; - } - await onToggleEnableState(); - }} + onClick={handleToggleEnableState} > - } - type="danger" - onClick={() => { - setIsDeleteFormOpen(true); - }} - > + } type="danger" onClick={handleDelete}> - { - setIsDeleteFormOpen(true); - }} - onConfirm={onDelete} - > -
{t('webhook_details.deletion_reminder')}
-
- { - setIsDisableFormOpen(false); - }} - onConfirm={onToggleEnableState} - > - -