Skip to content

Commit

Permalink
feat(mrc): add fetchall capabilities IcebergV6Hook
Browse files Browse the repository at this point in the history
ref: MANAGER-16335

Signed-off-by: Tristan WAGNER <[email protected]>
  • Loading branch information
tristanwagner committed Jan 15, 2025
1 parent 3a5ea55 commit aba4f36
Showing 1 changed file with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { IcebergFetchParamsV6, fetchIcebergV6 } from '@ovh-ux/manager-core-api';
import { useInfiniteQuery } from '@tanstack/react-query';
import { ColumnSort } from '../../components';
import { defaultPageSize } from './index';

interface IcebergV6Hook {
queryKey: string[];
defaultSorting?: ColumnSort;
shouldFetchAll?: boolean;
}

export const API_V6_MAX_PAGE_SIZE = 4999;

/**
* @deprecated use fetchIcebergV6 from @ovh-ux/manager-core-api
*/
export const getResourcesIcebergV6 = fetchIcebergV6;

export function useResourcesIcebergV6<T = unknown>({
route,
pageSize = 10,
pageSize = defaultPageSize,
queryKey,
defaultSorting = undefined,
shouldFetchAll = false,
}: IcebergFetchParamsV6 & IcebergV6Hook) {
const [sorting, setSorting] = useState<ColumnSort>(defaultSorting);

const { data: dataSelected, ...rest } = useInfiniteQuery({
const {
data: dataSelected,
hasNextPage,
fetchNextPage,
...rest
} = useInfiniteQuery({
initialPageParam: 1,
queryKey: [...queryKey, pageSize, sorting],
queryKey: [...queryKey, shouldFetchAll ? 'all' : pageSize, sorting],
staleTime: Infinity,
retry: false,
queryFn: ({ pageParam: pageIndex }) =>
fetchIcebergV6<T>({
route,
pageSize,
pageSize: shouldFetchAll ? API_V6_MAX_PAGE_SIZE : pageSize,
page: pageIndex,
sortBy: sorting?.id || null,
sortReverse: sorting?.desc,
Expand All @@ -53,8 +63,16 @@ export function useResourcesIcebergV6<T = unknown>({
},
});

useEffect(() => {
if (shouldFetchAll && hasNextPage) {
fetchNextPage();
}
}, [dataSelected]);

return {
...(dataSelected ?? { ...dataSelected, totalCount: 0 }),
hasNextPage,
fetchNextPage,
...rest,
sorting,
setSorting,
Expand Down

0 comments on commit aba4f36

Please sign in to comment.