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 useDeleteDeploymentSchedule hook #17077

Merged
merged 1 commit into from
Feb 11, 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
60 changes: 60 additions & 0 deletions ui-v2/src/api/deployments/deployments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
queryKeyFactory,
useCreateDeploymentSchedule,
useDeleteDeployment,
useDeleteDeploymentSchedule,
useUpdateDeploymentSchedule,
} from "./index";

Expand Down Expand Up @@ -331,4 +332,63 @@ describe("deployments api", () => {
).toEqual([MOCK_UPDATED_SCHEDULE]);
});
});

describe("useDeleteDeploymentSchedule", () => {
const MOCK_DEPLOYMENT_ID = "deployment-id";
const MOCK_SCHEDULE_ID = "schedule-id";
const MOCK_SCHEDULE = {
id: MOCK_SCHEDULE_ID,
created: "2024-01-01T00:00:00.000Z",
updated: "2024-01-01T00:00:00.000Z",
deployment_id: "deployment-id",
schedule: {
interval: 3600.0,
anchor_date: "2024-01-01T00:00:00.000Z",
timezone: "UTC",
},
active: false,
max_scheduled_runs: null,
};
const MOCK_DEPLYOMENT = createFakeDeployment({
id: MOCK_DEPLOYMENT_ID,
schedules: [MOCK_SCHEDULE],
});
const MOCK_UPDATED_DEPLOYMENT = {
...MOCK_DEPLYOMENT,
schedules: [],
};

it("invalidates cache and fetches updated value", async () => {
const queryClient = new QueryClient();
// Original cached value
queryClient.setQueryData(queryKeyFactory.all(), [MOCK_DEPLYOMENT]);

// Updated fetch and cached value
mockFetchDeploymentsAPI([MOCK_UPDATED_DEPLOYMENT]);

const { result: useListDeploymentsResult } = renderHook(
() => useQuery(buildPaginateDeploymentsQuery()),
{ wrapper: createWrapper({ queryClient }) },
);

const { result: useDeleteDeploymentScheduleResult } = renderHook(
useDeleteDeploymentSchedule,
{ wrapper: createWrapper({ queryClient }) },
);

act(() =>
useDeleteDeploymentScheduleResult.current.deleteDeploymentSchedule({
deployment_id: MOCK_DEPLOYMENT_ID,
schedule_id: MOCK_SCHEDULE_ID,
}),
);

await waitFor(() =>
expect(useDeleteDeploymentScheduleResult.current.isSuccess).toBe(true),
);
expect(
useListDeploymentsResult.current.data?.results[0].schedules,
).toHaveLength(0);
});
});
});
49 changes: 43 additions & 6 deletions ui-v2/src/api/deployments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,15 @@ type UpdateDeploymentSchedule = {
} & components["schemas"]["DeploymentScheduleUpdate"];

/**
* Hook for update a deployment's schedule
* Hook for updating a deployment's schedule
*
* @returns Mutation object for updating a deployment's schedule with loading/error states and trigger function
*
* @example
* ```ts
* const { updateDeploymentSchedule } = useUpdateDeploymentSchedule();
*
* // Delete a deployment by id
* deleteDeployment({deployment_id, schedule_id, ...body}, {
* updateDeploymentSchedule({deployment_id, schedule_id, ...body}, {
* onSuccess: () => {
* // Handle successful update
* console.log('Deployment schedule updated successfully');
Expand All @@ -321,10 +320,48 @@ export const useUpdateDeploymentSchedule = () => {
params: { path: { schedule_id, id: deployment_id } },
}),
onSettled: () =>
queryClient.invalidateQueries({
queryKey: queryKeyFactory.all(),
}),
queryClient.invalidateQueries({ queryKey: queryKeyFactory.all() }),
});

return { updateDeploymentSchedule, ...rest };
};

type DeleteDeploymentSchedule = {
deployment_id: string;
schedule_id: string;
};
/**
* Hook for deleting a deployment's schedule
*
* @returns Mutation object for deleting a deployment's schedule with loading/error states and trigger function
*
* @example
* ```ts
* const { deleteDeploymentSchedule } = useDeleteDeploymentSchedule();
*
* deleteDeploymentSchedule({deployment_id, schedule_id, ...body}, {
* onSuccess: () => {
* // Handle successful update
* console.log('Deployment schedule deleted successfully');
* },
* onError: (error) => {
* // Handle error
* console.error('Failed to delete deployment schedule:', error);
* }
* });
* ```
*/
export const useDeleteDeploymentSchedule = () => {
const queryClient = useQueryClient();

const { mutate: deleteDeploymentSchedule, ...rest } = useMutation({
mutationFn: ({ deployment_id, schedule_id }: DeleteDeploymentSchedule) =>
getQueryService().DELETE("/deployments/{id}/schedules/{schedule_id}", {
params: { path: { schedule_id, id: deployment_id } },
}),
onSettled: () =>
queryClient.invalidateQueries({ queryKey: queryKeyFactory.all() }),
});

return { deleteDeploymentSchedule, ...rest };
};
4 changes: 4 additions & 0 deletions ui-v2/tests/utils/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const deploymentsHandlers = [
http.patch(buildApiUrl("/deployments/:id/schedules/:schedule_id"), () => {
return HttpResponse.json({ status: 204 });
}),

http.delete(buildApiUrl("/deployments/:id/schedules/:schedule_id"), () => {
return HttpResponse.json({ status: 204 });
}),
];

const flowHandlers = [
Expand Down