Skip to content

Commit

Permalink
Merge branch 'main' into feat/토너먼트-전체조회-api용-타입-추가-#1094
Browse files Browse the repository at this point in the history
  • Loading branch information
Clearsu authored Nov 16, 2023
2 parents 05a252d + d985158 commit 4cf9a04
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions utils/infinityScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,37 @@ export function InfinityScroll(
}
);
}

interface PagenatedResponse {
totalPage: number;
}

// 무한스크롤 제네릭 함수
// Todo: 이 함수로 프로젝트 내 무한스크롤 모두 대체
export function InfiniteScroll<T extends PagenatedResponse>(
queryKey: string | string[],
fetchFunction: (page: number) => Promise<T>,
errorCode: string
) {
const setError = useSetRecoilState(errorState);
return useInfiniteQuery<T, Error>(
queryKey,
({ pageParam = 1 }) => fetchFunction(pageParam),
{
getNextPageParam: (lastPage, allPages) => {
const nextPage = allPages.length + 1;
return nextPage > lastPage.totalPage ? undefined : nextPage;
},
onError: (e: unknown) => {
if (axios.isAxiosError(e)) {
setError(errorCode);
} else {
// axios에서 발생한 에러가 아닌 경우
setError('JY03');
}
},
retry: 0,
keepPreviousData: true,
}
);
}

0 comments on commit 4cf9a04

Please sign in to comment.