Skip to content

Commit

Permalink
Migrate query/organizations.ts to use @app/client (#144)
Browse files Browse the repository at this point in the history
Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino authored Aug 27, 2024
1 parent 49b300c commit 6a975e9
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions client/src/app/queries/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
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";

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,
};
};

0 comments on commit 6a975e9

Please sign in to comment.