Skip to content

Commit

Permalink
fix: pass packageId as the query id param.
Browse files Browse the repository at this point in the history
Avoid reusing the serializeRequestParamsForHub hack, so that we can be more efficient and more typesafe.
  • Loading branch information
chirino committed Aug 21, 2024
1 parent adb3989 commit 5a04e11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
43 changes: 32 additions & 11 deletions client/src/app/hooks/table-controls/getHubRequestParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

import { HubRequestParams } from "@app/api/models";
import {
IGetFilterHubRequestParamsArgs,
getFilterHubRequestParams,
IGetFilterHubRequestParamsArgs,
serializeFilterForHub,
serializeFilterRequestParamsForHub,
} from "./filtering";
import {
IGetSortHubRequestParamsArgs,
getSortHubRequestParams,
IGetSortHubRequestParamsArgs,
serializeSortRequestParamsForHub,
} from "./sorting";
import {
IGetPaginationHubRequestParamsArgs,
getPaginationHubRequestParams,
IGetPaginationHubRequestParamsArgs,
serializePaginationRequestParamsForHub,
} from "./pagination";

Expand Down Expand Up @@ -79,13 +80,33 @@ interface HubRequestParamsQuery {
* Like serializeRequestParamsForHub but returns a plain object instead of URLSearchParams.
*/
export const requestParamsQuery = (
deserializedParams: HubRequestParams
p: HubRequestParams
): HubRequestParamsQuery => {
const params = serializeRequestParamsForHub(deserializedParams);
return {
limit: Number(params.get("limit")),
offset: Number(params.get("offset")),
q: params.get("q") ?? undefined,
sort: params.get("sort") ?? undefined,
};
let limit = undefined as number | undefined;
let offset = undefined as number | undefined;
if (p.page) {
limit = p.page.itemsPerPage;
offset = (p.page.pageNumber - 1) * p.page.itemsPerPage;
}

let sort = undefined as string | undefined;
if (p.sort) {
sort = `${p.sort.field}:${p.sort.direction}`;
}

let q = undefined as string | undefined;
if (p.filters) {
q = p.filters
.filter((f) => {
const { value } = f;
return typeof value === "string" || typeof value === "number"
? true
: value.list.length > 0;
})
.sort((a, b) => a.field.localeCompare(b.field))
.map(serializeFilterForHub)
.join("&");
}

return { limit, offset, q, sort };
};
3 changes: 1 addition & 2 deletions client/src/app/queries/sboms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ export const useFetchSbomsByPackageId = (
dataOf(
listRelatedSboms({
client,
path: { id: packageId },
query: { ...requestParamsQuery(params) },
query: { id: packageId, ...requestParamsQuery(params) },
})
),
});
Expand Down

0 comments on commit 5a04e11

Please sign in to comment.