8000 [UI v2] feat: Adds RQ for getting block type info by slug (#17733) · CodersSampling/prefect@8855b34 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8855b34

Browse files
[UI v2] feat: Adds RQ for getting block type info by slug (PrefectHQ#17733)
Co-authored-by: Alex Streed <desertaxle@users.noreply.github.com>
1 parent 366d251 commit 8855b34

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

ui-v2/src/api/block-types/block-types.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { describe, expect, it } from "vitest";
66

77
import { createFakeBlockType } from "@/mocks";
88

9-
import { type BlockType, buildListFilterBlockTypesQuery } from "./block-types";
9+
import {
10+
type BlockType,
11+
buildGetBlockTypesQuery,
12+
buildListFilterBlockTypesQuery,
13+
} from "./block-types";
1014

1115
describe("block types queries", () => {
1216
const seedBlockTypesData = () => [createFakeBlockType()];
@@ -19,6 +23,14 @@ describe("block types queries", () => {
1923
);
2024
};
2125

26+
const mockGetBlockTypeBySlugAPI = (blockType: BlockType) => {
27+
server.use(
28+
http.get(buildApiUrl("/block_types/slug/:slug"), () => {
29+
return HttpResponse.json(blockType);
30+
}),
31+
);
32+
};
33+
2234
it("stores block types list data", async () => {
2335
// ------------ Mock API requests when cache is empty
2436
const mockList = seedBlockTypesData();
@@ -34,4 +46,20 @@ describe("block types queries", () => {
3446
await waitFor(() => expect(result.current.isSuccess).toBe(true));
3547
expect(result.current.data).toEqual(mockList);
3648
});
49+
50+
it("stores block type info by slug", async () => {
51+
// ------------ Mock API requests when cache is empty
52+
const mockBlockType = createFakeBlockType();
53+
mockGetBlockTypeBySlugAPI(mockBlockType);
54+
55+
// ------------ Initialize hooks to test
56+
const { result } = renderHook(
57+
() => useSuspenseQuery(buildGetBlockTypesQuery(mockBlockType.slug)),
58+
{ wrapper: createWrapper() },
59+
);
60+
61+
// ------------ Assert
62+
await waitFor(() => expect(result.current.isSuccess).toBe(true));
63+
expect(result.current.data).toEqual(mockBlockType);
64+
});
3765
});

ui-v2/src/api/block-types/block-types.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ export type BlockTypesFilter =
88
/**
99
* ```
1010
* 🏗️ Block Types queries construction 👷
11-
* all => ['"block-types'] // key to match ['"block-types', ...
12-
* lists => ['"block-types', 'list'] // key to match ['"block-types, 'list', ...
11+
* all => ['block-types'] // key to match ['block-types', ...
12+
* lists => ['block-types', 'list'] // key to match ['block-types, 'list', ...
1313
* listFilters => ['"block-types', 'list', 'fil 8000 ter']
1414
* listFilter => ['"block-types', 'list', 'filter', { ...filter1 }]
15+
* details => ['block-types', 'detail']
16+
* detailsSlug => ['block-types', 'detail', 'slug']
17+
* detailSlug => ['block-types', 'detail', 'slug', $slug ]
1518
* ```
1619
* */
1720
export const queryKeyFactory = {
@@ -20,6 +23,10 @@ export const queryKeyFactory = {
2023
listFilters: () => [...queryKeyFactory.lists(), "filter"] as const,
2124
listFilter: (filter: BlockTypesFilter) =>
2225
[...queryKeyFactory.listFilters(), filter] as const,
26+
details: () => [...queryKeyFactory.all(), "detail"] as const,
27+
detailsSlug: () => [...queryKeyFactory.details(), "slug"] as const,
28+
detailSlug: (slug: string) =>
29+
[...queryKeyFactory.detailsSlug(), slug] as const,
2330
};
2431

2532
// ----- 🔑 Queries 🗄️
@@ -34,7 +41,21 @@ export const buildListFilterBlockTypesQuery = (
3441
body: filter,
3542
});
3643
if (!res.data) {
37-
throw new Error("'data' exoected");
44+
throw new Error("'data' expected");
45+
}
46+
return res.data;
47+
},
48+
});
49+
50+
export const buildGetBlockTypesQuery = (slug: string) =>
51+
queryOptions({
52+
queryKey: queryKeyFactory.detailSlug(slug),
53+
queryFn: async () => {
54+
const res = await getQueryService().GET("/block_types/slug/{slug}", {
55+
params: { path: { slug } },
56+
});
57+
if (!res.data) {
58+
throw new Error("'data' expected");
3859
}
3960
return res.data;
4061
},

ui-v2/src/api/block-types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export {
22
type BlockType,
33
buildListFilterBlockTypesQuery,
4+
buildGetBlockTypesQuery,
45
} from "./block-types";

0 commit comments

Comments
 (0)
0