Skip to content

Commit

Permalink
useConfirmationDialog hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ivyjeong13 committed Jan 6, 2025
1 parent 8199094 commit a84b43d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
65 changes: 38 additions & 27 deletions ui/admin/app/components/workflow-triggers/DeleteWorkflowTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,66 +1,77 @@
import { useState } from "react";
import { toast } from "sonner";
import { mutate } from "swr";

import { CronJobApiService } from "~/lib/service/api/cronjobApiService";
import { WebhookApiService } from "~/lib/service/api/webhookApiService";

import { TypographyP } from "~/components/Typography";
import { ConfirmationDialog } from "~/components/composed/ConfirmationDialog";
import { DropdownMenuItem } from "~/components/ui/dropdown-menu";
import { useConfirmationDialog } from "~/hooks/component-helpers/useConfirmationDialog";
import { useAsync } from "~/hooks/useAsync";

export function DeleteWorkflowTrigger({
id,
name,
type,
children,
}: {
id: string;
name?: string;
type: "webhook" | "schedule";
children?: React.ReactNode;
}) {
const [open, setOpen] = useState(false);

const deleteWebhook = useAsync(WebhookApiService.deleteWebhook, {
onSuccess: () => {
mutate(WebhookApiService.getWebhooks.key());
toast.success("Webhook workflow trigger has been deleted.");
setOpen(false);
},
});

const deleteCronjob = useAsync(CronJobApiService.deleteCronJob, {
onSuccess: () => {
mutate(CronJobApiService.getCronJobs.key());
toast.success("Schedule workflow trigger has been deleted.");
setOpen(false);
},
});

const handleConfirmDelete = () => {
const handleConfirmDelete = async () => {
if (type === "webhook") {
deleteWebhook.execute(id);
await deleteWebhook.executeAsync(id);
} else {
deleteCronjob.execute(id);
await deleteCronjob.executeAsync(id);
}
};

const loading = deleteWebhook.isLoading || deleteCronjob.isLoading;
const { interceptAsync, dialogProps } = useConfirmationDialog();

return (
<ConfirmationDialog
open={open}
onOpenChange={setOpen}
title="Delete Trigger?"
description="This action cannot be undone."
onConfirm={handleConfirmDelete}
closeOnConfirm={false}
confirmProps={{
loading: loading,
disabled: loading,
variant: "destructive",
children: "Delete",
}}
>
{children}
</ConfirmationDialog>
<>
<DropdownMenuItem
variant="destructive"
onClick={(e) => {
e.preventDefault();
interceptAsync(handleConfirmDelete);
}}
>
Delete
</DropdownMenuItem>

<ConfirmationDialog
{...dialogProps}
title="Delete Workflow Trigger?"
description={
<div className="flex flex-col">
<TypographyP>
Are you sure you want to delete workflow trigger:{" "}
<b>{name || id}</b>?
</TypographyP>
<TypographyP>The action cannot be undone.</TypographyP>
</div>
}
confirmProps={{
children: "Delete",
variant: "destructive",
}}
/>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ export function WorkflowTriggerActions({ item }: { item: WorkflowTrigger }) {
<DropdownMenuItem>Edit</DropdownMenuItem>
</Link>

<DeleteWorkflowTrigger id={item.id} type={item.type}>
<DropdownMenuItem
className="text-destructive"
onSelect={(e) => e.preventDefault()}
>
Delete
</DropdownMenuItem>
</DeleteWorkflowTrigger>
<DeleteWorkflowTrigger
id={item.id}
name={item.name}
type={item.type}
/>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
Expand Down

0 comments on commit a84b43d

Please sign in to comment.