Skip to content

Commit

Permalink
[UI v2] feat: Adds useDeleteDeploymentSchedule hook
Browse files Browse the repository at this point in the history
  • Loading branch information
devinvillarosa committed Feb 11, 2025
1 parent cd4129c commit 56d5697
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
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

0 comments on commit 56d5697

Please sign in to comment.