-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] feat: Add AutomationsDeleteDialog (#16595)
- Loading branch information
1 parent
85f6e26
commit 45fc50f
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...rc/components/automations/automations-delete-dialog/automations-delete-dialog.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createFakeAutomation } from "@/mocks"; | ||
import { reactQueryDecorator } from "@/storybook/utils"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { fn } from "@storybook/test"; | ||
import { AutomationsDeleteDialog } from "./automations-delete-dialog"; | ||
|
||
const meta = { | ||
title: "Components/Automations/AutomationsDeleteDialog", | ||
component: AutomationsDeleteDialog, | ||
decorators: [reactQueryDecorator], | ||
args: { | ||
automation: createFakeAutomation(), | ||
onOpenChange: fn(), | ||
onDelete: fn(), | ||
}, | ||
} satisfies Meta<typeof AutomationsDeleteDialog>; | ||
|
||
export default meta; | ||
|
||
export const story: StoryObj = { name: "AutomationsDeleteDialog" }; |
39 changes: 39 additions & 0 deletions
39
...2/src/components/automations/automations-delete-dialog/automations-delete-dialog.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Toaster } from "@/components/ui/toaster"; | ||
import { createFakeAutomation } from "@/mocks"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { createWrapper } from "@tests/utils"; | ||
import { expect, test, vi } from "vitest"; | ||
|
||
import { AutomationsDeleteDialog } from "./automations-delete-dialog"; | ||
|
||
test("AutomationsDeleteDialog can successfully call delete", async () => { | ||
const MOCK_DATA = createFakeAutomation(); | ||
const user = userEvent.setup(); | ||
|
||
// ------------ Setup | ||
const mockOnDeleteFn = vi.fn(); | ||
render( | ||
<> | ||
<Toaster /> | ||
<AutomationsDeleteDialog | ||
automation={MOCK_DATA} | ||
onDelete={mockOnDeleteFn} | ||
onOpenChange={vi.fn()} | ||
/> | ||
</>, | ||
{ wrapper: createWrapper() }, | ||
); | ||
|
||
// ------------ Act | ||
expect(screen.getByRole("heading", { name: /delete automation/i })); | ||
await user.click( | ||
screen.getByRole("button", { | ||
name: /delete/i, | ||
}), | ||
); | ||
|
||
// ------------ Assert | ||
expect(screen.getByText(/automation deleted/i)).toBeVisible(); | ||
expect(mockOnDeleteFn).toHaveBeenCalledOnce(); | ||
}); |
66 changes: 66 additions & 0 deletions
66
ui-v2/src/components/automations/automations-delete-dialog/automations-delete-dialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Automation, useDeleteAutomation } from "@/api/automations"; | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog"; | ||
import { useToast } from "@/hooks/use-toast"; | ||
|
||
type AutomationsDeleteDialogProps = { | ||
automation: Automation; | ||
onOpenChange: (open: boolean) => void; | ||
onDelete: () => void; | ||
}; | ||
|
||
export const AutomationsDeleteDialog = ({ | ||
automation, | ||
onOpenChange, | ||
onDelete, | ||
}: AutomationsDeleteDialogProps) => { | ||
const { toast } = useToast(); | ||
const { deleteAutomation, isPending } = useDeleteAutomation(); | ||
|
||
const handleOnClick = (id: string) => { | ||
deleteAutomation(id, { | ||
onSuccess: () => { | ||
toast({ description: "Automation deleted" }); | ||
}, | ||
onError: (error) => { | ||
const message = | ||
error.message || "Unknown error while deleting automation."; | ||
console.error(message); | ||
}, | ||
onSettled: onDelete, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Dialog open onOpenChange={onOpenChange}> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Delete Automation</DialogTitle> | ||
</DialogHeader> | ||
<DialogDescription> | ||
Are you sure you want to delete {automation.name} | ||
</DialogDescription> | ||
<DialogFooter> | ||
<DialogTrigger asChild> | ||
<Button variant="outline">Close</Button> | ||
</DialogTrigger> | ||
<Button | ||
variant="destructive" | ||
onClick={() => handleOnClick(automation.id)} | ||
loading={isPending} | ||
> | ||
Delete | ||
</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
ui-v2/src/components/automations/automations-delete-dialog/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AutomationsDeleteDialog } from "./automations-delete-dialog"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters