mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
refactor(console): replace confirm modal with useConfirmModal hook (#3906)
This commit is contained in:
parent
9550552438
commit
ff4fb5e56f
1 changed files with 31 additions and 45 deletions
|
@ -15,9 +15,7 @@ import WebhookDark from '@/assets/images/webhook-dark.svg';
|
||||||
import Webhook from '@/assets/images/webhook.svg';
|
import Webhook from '@/assets/images/webhook.svg';
|
||||||
import ActionMenu, { ActionMenuItem } from '@/components/ActionMenu';
|
import ActionMenu, { ActionMenuItem } from '@/components/ActionMenu';
|
||||||
import Card from '@/components/Card';
|
import Card from '@/components/Card';
|
||||||
import ConfirmModal from '@/components/ConfirmModal';
|
|
||||||
import CopyToClipboard from '@/components/CopyToClipboard';
|
import CopyToClipboard from '@/components/CopyToClipboard';
|
||||||
import DeleteConfirmModal from '@/components/DeleteConfirmModal';
|
|
||||||
import DetailsPage from '@/components/DetailsPage';
|
import DetailsPage from '@/components/DetailsPage';
|
||||||
import DynamicT from '@/components/DynamicT';
|
import DynamicT from '@/components/DynamicT';
|
||||||
import PageMeta from '@/components/PageMeta';
|
import PageMeta from '@/components/PageMeta';
|
||||||
|
@ -25,6 +23,7 @@ import TabNav, { TabNavItem } from '@/components/TabNav';
|
||||||
import Tag from '@/components/Tag';
|
import Tag from '@/components/Tag';
|
||||||
import { WebhookDetailsTabs } from '@/consts';
|
import { WebhookDetailsTabs } from '@/consts';
|
||||||
import useApi, { type RequestError } from '@/hooks/use-api';
|
import useApi, { type RequestError } from '@/hooks/use-api';
|
||||||
|
import { useConfirmModal } from '@/hooks/use-confirm-modal';
|
||||||
import useTheme from '@/hooks/use-theme';
|
import useTheme from '@/hooks/use-theme';
|
||||||
import { buildUrl } from '@/utils/url';
|
import { buildUrl } from '@/utils/url';
|
||||||
|
|
||||||
|
@ -49,21 +48,31 @@ function WebhookDetails() {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const WebhookIcon = theme === 'light' ? Webhook : WebhookDark;
|
const WebhookIcon = theme === 'light' ? Webhook : WebhookDark;
|
||||||
|
|
||||||
const [isDeleteFormOpen, setIsDeleteFormOpen] = useState(false);
|
const { show, cancel } = useConfirmModal();
|
||||||
|
|
||||||
const [isDeleting, setIsDeleting] = useState(false);
|
const [isDeleting, setIsDeleting] = useState(false);
|
||||||
const [isDisableFormOpen, setIsDisableFormOpen] = useState(false);
|
|
||||||
const [isUpdatingEnableState, setIsUpdatingEnableState] = useState(false);
|
const [isUpdatingEnableState, setIsUpdatingEnableState] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsDeleteFormOpen(false);
|
return () => {
|
||||||
setIsDisableFormOpen(false);
|
cancel();
|
||||||
}, [data?.enabled, pathname]);
|
};
|
||||||
|
}, [cancel, pathname]);
|
||||||
|
|
||||||
const onDelete = async () => {
|
const handleDelete = async () => {
|
||||||
if (!data || isDeleting) {
|
if (!data || isDeleting) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [result] = await show({
|
||||||
|
ModalContent: () => <DynamicT forKey="webhook_details.deletion_reminder" />,
|
||||||
|
confirmButtonText: 'general.delete',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setIsDeleting(true);
|
setIsDeleting(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -75,11 +84,22 @@ function WebhookDetails() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const onToggleEnableState = async () => {
|
const handleToggleEnableState = async () => {
|
||||||
if (!data || isUpdatingEnableState) {
|
if (!data || isUpdatingEnableState) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isEnabled) {
|
||||||
|
const [result] = await show({
|
||||||
|
ModalContent: () => <DynamicT forKey="webhook_details.disable_reminder" />,
|
||||||
|
confirmButtonText: 'webhook_details.disable_webhook',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setIsUpdatingEnableState(true);
|
setIsUpdatingEnableState(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -87,7 +107,6 @@ function WebhookDetails() {
|
||||||
.patch(`api/hooks/${data.id}`, { json: { enabled: !isEnabled } })
|
.patch(`api/hooks/${data.id}`, { json: { enabled: !isEnabled } })
|
||||||
.json<Hook>();
|
.json<Hook>();
|
||||||
void mutate({ ...updatedHook, executionStats: data.executionStats });
|
void mutate({ ...updatedHook, executionStats: data.executionStats });
|
||||||
setIsDisableFormOpen(false);
|
|
||||||
const { enabled } = updatedHook;
|
const { enabled } = updatedHook;
|
||||||
toast.success(
|
toast.success(
|
||||||
t(enabled ? 'webhook_details.webhook_reactivated' : 'webhook_details.webhook_disabled')
|
t(enabled ? 'webhook_details.webhook_reactivated' : 'webhook_details.webhook_disabled')
|
||||||
|
@ -150,13 +169,7 @@ function WebhookDetails() {
|
||||||
<ActionMenuItem
|
<ActionMenuItem
|
||||||
icon={isEnabled ? <Forbidden /> : <Shield />}
|
icon={isEnabled ? <Forbidden /> : <Shield />}
|
||||||
iconClassName={styles.icon}
|
iconClassName={styles.icon}
|
||||||
onClick={async () => {
|
onClick={handleToggleEnableState}
|
||||||
if (isEnabled) {
|
|
||||||
setIsDisableFormOpen(true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await onToggleEnableState();
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<DynamicT
|
<DynamicT
|
||||||
forKey={
|
forKey={
|
||||||
|
@ -166,37 +179,10 @@ function WebhookDetails() {
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</ActionMenuItem>
|
</ActionMenuItem>
|
||||||
<ActionMenuItem
|
<ActionMenuItem icon={<Delete />} type="danger" onClick={handleDelete}>
|
||||||
icon={<Delete />}
|
|
||||||
type="danger"
|
|
||||||
onClick={() => {
|
|
||||||
setIsDeleteFormOpen(true);
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<DynamicT forKey="webhook_details.delete_webhook" />
|
<DynamicT forKey="webhook_details.delete_webhook" />
|
||||||
</ActionMenuItem>
|
</ActionMenuItem>
|
||||||
</ActionMenu>
|
</ActionMenu>
|
||||||
<DeleteConfirmModal
|
|
||||||
isOpen={isDeleteFormOpen}
|
|
||||||
isLoading={isDeleting}
|
|
||||||
onCancel={() => {
|
|
||||||
setIsDeleteFormOpen(true);
|
|
||||||
}}
|
|
||||||
onConfirm={onDelete}
|
|
||||||
>
|
|
||||||
<div>{t('webhook_details.deletion_reminder')}</div>
|
|
||||||
</DeleteConfirmModal>
|
|
||||||
<ConfirmModal
|
|
||||||
isOpen={isDisableFormOpen}
|
|
||||||
isLoading={isUpdatingEnableState}
|
|
||||||
confirmButtonText="webhook_details.disable_webhook"
|
|
||||||
onCancel={async () => {
|
|
||||||
setIsDisableFormOpen(false);
|
|
||||||
}}
|
|
||||||
onConfirm={onToggleEnableState}
|
|
||||||
>
|
|
||||||
<DynamicT forKey="webhook_details.disable_reminder" />
|
|
||||||
</ConfirmModal>
|
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
<TabNav>
|
<TabNav>
|
||||||
|
|
Loading…
Add table
Reference in a new issue