-
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: Adds AutomationEnableToggle component (#16588)
- Loading branch information
1 parent
b558313
commit 3de969c
Showing
6 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
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
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
19 changes: 19 additions & 0 deletions
19
.../src/components/automations/automation-enable-toggle/automation-enable-toggle.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,19 @@ | ||
import { createFakeAutomation } from "@/mocks"; | ||
import { reactQueryDecorator, toastDecorator } from "@/storybook/utils"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { AutomationEnableToggle } from "./automation-enable-toggle"; | ||
|
||
const MOCK_DATA = createFakeAutomation(); | ||
|
||
const meta = { | ||
title: "Components/Automations/AutomationEnableToggle", | ||
component: AutomationEnableToggle, | ||
decorators: [reactQueryDecorator, toastDecorator], | ||
args: { data: MOCK_DATA }, | ||
} satisfies Meta<typeof AutomationEnableToggle>; | ||
|
||
export default meta; | ||
|
||
export const Story: StoryObj = { | ||
name: "AutomationEnableToggle", | ||
}; |
28 changes: 28 additions & 0 deletions
28
ui-v2/src/components/automations/automation-enable-toggle/automation-enable-toggle.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,28 @@ | ||
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 } from "vitest"; | ||
import { AutomationEnableToggle } from "./automation-enable-toggle"; | ||
|
||
const MOCK_DATA = createFakeAutomation({ enabled: true }); | ||
|
||
test("AutomationEnableToggle can toggle switch", async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<> | ||
<Toaster /> | ||
<AutomationEnableToggle data={MOCK_DATA} /> | ||
</>, | ||
{ wrapper: createWrapper() }, | ||
); | ||
|
||
expect( | ||
screen.getByRole("switch", { name: /toggle automation/i }), | ||
).toBeChecked(); | ||
|
||
await user.click(screen.getByRole("switch", { name: /toggle automation/i })); | ||
expect(screen.getByText(/automation disabled/i)).toBeVisible(); | ||
}); |
43 changes: 43 additions & 0 deletions
43
ui-v2/src/components/automations/automation-enable-toggle/automation-enable-toggle.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,43 @@ | ||
import { Automation, useUpdateAutomation } from "@/api/automations"; | ||
import { Switch } from "@/components/ui/switch"; | ||
import { useToast } from "@/hooks/use-toast"; | ||
|
||
type AutomationEnableToggleProps = { | ||
data: Automation; | ||
}; | ||
export const AutomationEnableToggle = ({ | ||
data, | ||
}: AutomationEnableToggleProps) => { | ||
const { toast } = useToast(); | ||
|
||
const { updateAutomation } = useUpdateAutomation(); | ||
|
||
const handleCheckedChange = (checked: boolean, id: string) => { | ||
updateAutomation( | ||
{ | ||
enabled: checked, | ||
id, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
toast({ | ||
description: `Automation ${checked ? "enabled" : "disabled"}`, | ||
}); | ||
}, | ||
onError: (error) => { | ||
const message = | ||
error.message || "Unknown error while updating automation"; | ||
console.error(message); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
return ( | ||
<Switch | ||
aria-label="toggle automation" | ||
checked={data.enabled} | ||
onCheckedChange={(checked) => handleCheckedChange(checked, data.id)} | ||
/> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
ui-v2/src/components/automations/automation-enable-toggle/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 { AutomationEnableToggle } from "./automation-enable-toggle"; |