Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI v2] feat: Adds AutomationEnableToggle component #16588

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions ui-v2/src/api/automations/automations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,11 @@ describe("automations queries and mutations", () => {
);

// ------------ Invoke mutation
const { id, actions, name, description, enabled, trigger } =
EDITED_AUTOMATION;
const { id, enabled } = EDITED_AUTOMATION;
act(() =>
useUpdateAutomationResult.current.updateAutomation({
id,
name,
description,
enabled,
trigger,
actions,
}),
);

Expand Down
2 changes: 1 addition & 1 deletion ui-v2/src/api/automations/automations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const useUpdateAutomation = () => {
mutationFn: ({
id,
...body
}: components["schemas"]["AutomationUpdate"] & { id: string }) =>
}: components["schemas"]["AutomationPartialUpdate"] & { id: string }) =>
getQueryService().PATCH("/automations/{id}", {
body,
params: { path: { id } },
Expand Down
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",
};
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();
});
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(
devinvillarosa marked this conversation as resolved.
Show resolved Hide resolved
{
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)}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AutomationEnableToggle } from "./automation-enable-toggle";
Loading