From 6a975e9171e0c43378ae8a6a38d6ed4e8682849e Mon Sep 17 00:00:00 2001 From: Hiram Chirino Date: Tue, 27 Aug 2024 14:54:38 -0400 Subject: [PATCH] Migrate query/organizations.ts to use @app/client (#144) Signed-off-by: Hiram Chirino --- client/src/app/queries/organizations.ts | 39 +++++++++++++------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/client/src/app/queries/organizations.ts b/client/src/app/queries/organizations.ts index 260c11cb..664abc3b 100644 --- a/client/src/app/queries/organizations.ts +++ b/client/src/app/queries/organizations.ts @@ -1,8 +1,10 @@ import { useQuery } from "@tanstack/react-query"; -import { AxiosError } from "axios"; import { HubRequestParams } from "@app/api/models"; -import { getOrganizationById, getOrganizations } from "@app/api/rest"; +import { convertQuery, dataOf } from "./dataOf"; +import { getOrganization, listOrganizations } from "../client"; +import { client } from "../axios-config/apiInit"; +import { requestParamsQuery } from "../hooks/table-controls"; export const OrganizationsQueryKey = "organizations"; @@ -10,33 +12,34 @@ export const useFetchOrganizations = ( params: HubRequestParams = {}, refetchDisabled: boolean = false ) => { - const { data, isLoading, error, refetch } = useQuery({ + const query = useQuery({ queryKey: [OrganizationsQueryKey, params], - queryFn: () => getOrganizations(params), + queryFn: () => + dataOf( + listOrganizations({ + 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 useFetchOrganizationById = (id: number | string) => { - const { data, isLoading, error } = useQuery({ +export const useFetchOrganizationById = (id: string) => { + const query = useQuery({ queryKey: [OrganizationsQueryKey, id], - queryFn: () => getOrganizationById(id), - enabled: id !== undefined, + queryFn: () => dataOf(getOrganization({ client, path: { id } })), }); - return { - organization: data, - isFetching: isLoading, - fetchError: error as AxiosError, + ...convertQuery(query), + organization: query.data, }; };