Skip to content

Commit

Permalink
[UI v2] feat: Adds deployment details RQ hook
Browse files Browse the repository at this point in the history
  • Loading branch information
devinvillarosa committed Feb 6, 2025
1 parent d2853c1 commit a5bfe3d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
26 changes: 26 additions & 0 deletions ui-v2/src/api/deployments/deployments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { describe, expect, it } from "vitest";
import type { Deployment } from "./index";
import {
buildCountDeploymentsQuery,
buildDeploymentDetailsQuery,
buildFilterDeploymentsQuery,
buildPaginateDeploymentsQuery,
queryKeyFactory,
Expand Down Expand Up @@ -42,6 +43,14 @@ describe("deployments api", () => {
);
};

const mockGetDeploymentAPI = (deployment: Deployment) => {
server.use(
http.get(buildApiUrl("/deployments/:id"), () => {
return HttpResponse.json(deployment);
}),
);
};

describe("buildPaginateDeploymentsQuery", () => {
it("fetches paginated deployments with default parameters", async () => {
const deployment = createFakeDeployment();
Expand Down Expand Up @@ -148,6 +157,23 @@ describe("deployments api", () => {
});
});

describe("buildDeploymentDetailsQuery", () => {
it("fetches deployment details", async () => {
const queryClient = new QueryClient();

const mockResponse = createFakeDeployment({ id: "my-id" });
mockGetDeploymentAPI(mockResponse);

const { result } = renderHook(
() => useSuspenseQuery(buildDeploymentDetailsQuery("my-id")),
{ wrapper: createWrapper({ queryClient }) },
);

await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data).toEqual(mockResponse);
});
});

describe("useDeleteDeployment", () => {
it("invalidates cache and fetches updated value", async () => {
const mockDeployment = createFakeDeployment();
Expand Down
34 changes: 32 additions & 2 deletions ui-v2/src/api/deployments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export type DeploymentsPaginationFilter =
* list-filter => ['deployments', 'list', 'filter', { ...filter }]
* counts => ['deployments', 'counts']
* count => ['deployments', 'counts', { ...filter }]
* details => ['deployments', 'details']
* detail => ['deployments', 'details', id]
* ```
*/
export const queryKeyFactory = {
Expand All @@ -48,6 +50,8 @@ export const queryKeyFactory = {
counts: () => [...queryKeyFactory.all(), "counts"] as const,
count: (filter: DeploymentsFilter) =>
[...queryKeyFactory.counts(), filter] as const,
details: () => [...queryKeyFactory.all(), "details"] as const,
detail: (id: string) => [...queryKeyFactory.details(), id] as const,
};

// ----------------------------
Expand Down Expand Up @@ -140,14 +144,14 @@ export const buildFilterDeploymentsQuery = (
*
* @example
* ```ts
* const query = buildCountDeploymentsQuery({
* const query = useQuery(buildCountDeploymentsQuery({
* offset: 0,
* limit: 10,
* sort: "NAME_ASC",
* deployments: {
* name: { like_: "my-deployment" }
* }
* });
* }));
* ```
*/
export const buildCountDeploymentsQuery = (
Expand All @@ -163,6 +167,32 @@ export const buildCountDeploymentsQuery = (
},
});

/**
* Builds a query configuration for getting a deployment details
*
* @param id - deployment's id
*
* @returns Query configuration object for use with TanStack Query
*
* @example
* ```ts
* const query = useSuspenseQuery(buildDeploymentDetailsQuery(deployment.id));
* ```
*/
export const buildDeploymentDetailsQuery = (id: string) =>
queryOptions({
queryKey: queryKeyFactory.detail(id),
queryFn: async () => {
const res = await getQueryService().GET("/deployments/{id}", {
params: { path: { id } },
});
if (!res.data) {
throw new Error("'data' expected");
}
return res.data;
},
});

// ----------------------------
// -------- Mutations --------
// ----------------------------
Expand Down
4 changes: 4 additions & 0 deletions ui-v2/src/routes/deployments/deployment.$id.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { buildDeploymentDetailsQuery } from "@/api/deployments";
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/deployments/deployment/$id")({
component: RouteComponent,
loader: ({ context, params }) =>
context.queryClient.ensureQueryData(buildDeploymentDetailsQuery(params.id)),
wrapInSuspense: true,
});

function RouteComponent() {
Expand Down

0 comments on commit a5bfe3d

Please # to comment.