From b2da7671e2df452fd2f43ba9dd6c53dadff426fb Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Tue, 27 Aug 2024 14:52:11 -0400 Subject: [PATCH] Migrate query/products.ts to use @app/client (#142) Signed-off-by: Hiram Chirino --- .../app/pages/product-details/overview.tsx | 5 +- .../product-details/product-versions.tsx | 5 +- client/src/app/queries/products.ts | 46 +++++++++++-------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/client/src/app/pages/product-details/overview.tsx b/client/src/app/pages/product-details/overview.tsx index 6380b293..cb978c7d 100644 --- a/client/src/app/pages/product-details/overview.tsx +++ b/client/src/app/pages/product-details/overview.tsx @@ -13,11 +13,10 @@ import { Stack, StackItem, } from "@patternfly/react-core"; - -import { Product } from "@app/api/models"; +import { ProductDetails } from "../../client"; interface OverviewProps { - product: Product; + product: ProductDetails; } export const Overview: React.FC = ({ product }) => { diff --git a/client/src/app/pages/product-details/product-versions.tsx b/client/src/app/pages/product-details/product-versions.tsx index d3fd6d2d..95c586f5 100644 --- a/client/src/app/pages/product-details/product-versions.tsx +++ b/client/src/app/pages/product-details/product-versions.tsx @@ -17,6 +17,7 @@ import { } from "@app/components/TableControls"; import { useLocalTableControls } from "@app/hooks/table-controls"; import { formatDate } from "@app/utils/utils"; +import { ProductDetails } from "../../client"; interface TableData { sbomId: string; @@ -25,7 +26,7 @@ interface TableData { } interface ProductVersionsProps { - product: Product; + product: ProductDetails; } export const ProductVersions: React.FC = ({ @@ -42,7 +43,7 @@ export const ProductVersions: React.FC = ({ return { sbomId: sbom.sbom_id, sbomVersion: sbom.version, - }; + } as TableData; }); setAllSboms(sbombs); diff --git a/client/src/app/queries/products.ts b/client/src/app/queries/products.ts index cee4de49..a1fce231 100644 --- a/client/src/app/queries/products.ts +++ b/client/src/app/queries/products.ts @@ -2,9 +2,15 @@ import { useMutation, useQuery } from "@tanstack/react-query"; import { AxiosError } from "axios"; import { HubRequestParams } from "@app/api/models"; -import { getProductById, getProducts } from "@app/api/rest"; -import { deleteProduct, ProductDetails } from "@app/client"; +import { + deleteProduct, + getProduct, + listProducts, + ProductDetails, +} from "@app/client"; import { client } from "@app/axios-config/apiInit"; +import { convertQuery, dataOf } from "./dataOf"; +import { requestParamsQuery } from "../hooks/table-controls"; export const ProductsQueryKey = "products"; @@ -12,34 +18,35 @@ export const useFetchProducts = ( params: HubRequestParams = {}, refetchDisabled: boolean = false ) => { - const { data, isLoading, error, refetch } = useQuery({ + const query = useQuery({ queryKey: [ProductsQueryKey, params], - queryFn: () => getProducts(params), + queryFn: () => + dataOf( + listProducts({ + client, + query: { ...requestParamsQuery(params) }, + }) + ), refetchInterval: !refetchDisabled ? 5000 : false, }); return { + ...convertQuery(query), result: { - data: data?.data || [], - total: data?.total ?? 0, - params: data?.params ?? params, + data: query.data?.items || [], + total: query.data?.total ?? 0, + params: params, }, - isFetching: isLoading, - fetchError: error, - refetch, }; }; -export const useFetchProductById = (id: number | string) => { - const { data, isLoading, error } = useQuery({ +export const useFetchProductById = (id: string) => { + const query = useQuery({ queryKey: [ProductsQueryKey, id], - queryFn: () => getProductById(id), - enabled: id !== undefined, + queryFn: () => dataOf(getProduct({ client, path: { id } })), }); - return { - product: data, - isFetching: isLoading, - fetchError: error as AxiosError, + ...convertQuery(query), + product: query.data, }; }; @@ -48,8 +55,7 @@ export const useDeleteProductMutation = ( onSuccess?: (payload: ProductDetails, id: string) => void ) => { return useMutation({ - mutationFn: async (id: string) => - (await deleteProduct({ client, path: { id } })).data, + mutationFn: (id: string) => dataOf(deleteProduct({ client, path: { id } })), mutationKey: [ProductsQueryKey], onSuccess, onError,