refactor: clean code and remove unnecessary dependencies

This commit is contained in:
ner committed 2024-09-30 18:54:13 +03:00
1 parent eceae6fec7
commit ebf599eed6
92 files changed
+1152 -2842

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import { useMemo } from 'react';
import { QueryKey, useInfiniteQuery } from '@tanstack/react-query';
interface Props {
queryKey: QueryKey;
enabled?: boolean;
queryFn: ({ page }: { page: string }) => Promise<Pagination<Show>>;
}
const useInfiniteList = ({ queryKey, queryFn, enabled }: Props) => {
const { data, hasNextPage, fetchNextPage, isFetched, isLoading } = useInfiniteQuery({
enabled,
queryKey,
queryFn: ({ pageParam }) => queryFn({ page: pageParam }),
getNextPageParam(pagination) {
if (!pagination || pagination.page === pagination.total) return undefined;
return `${pagination.page + 1}`;
},
initialPageParam: '1'
});
const shows = useMemo(() => {
return (
data?.pages.reduce<Show[]>((shows, page) => {
shows.push(...page.results);
return shows;
}, []) || []
);
}, [data?.pages]);
return { shows, fetchNextPage: hasNextPage ? fetchNextPage : undefined, isFetched, isLoading };
};
export default useInfiniteList;