Skip to content

Commit

Permalink
fix: restrict request in infinite scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
SeojinSeojin committed Sep 20, 2023
1 parent b7805ea commit ad24e89
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/hooks/useStackedFetchBase/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { to } from 'await-to-js';
import { useEffect, useReducer } from 'react';
import { debounce } from '@src/utils/debounce';
import { reducer } from './reducer';
import { Action, State } from './types';

Expand All @@ -14,7 +13,7 @@ function useStackedFetchBase<T>(
});

useEffect(() => {
const fetchList = debounce(async () => {
const fetchList = async () => {
dispatch({
_TAG: 'FETCH',
isInitialFetching,
Expand All @@ -32,7 +31,7 @@ function useStackedFetchBase<T>(
if (response) {
dispatch({ _TAG: 'SUCCESS', isInitialFetching, data: response });
}
});
};

fetchList();
}, [willFetch, isInitialFetching]);
Expand Down
18 changes: 16 additions & 2 deletions src/views/ReviewPage/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import useBooleanState from '@src/hooks/useBooleanState';
import useStackedFetchBase from '@src/hooks/useStackedFetchBase';
import { api } from '@src/lib/api';
import { ExtraPart } from '@src/lib/types/universal';
import useInfiniteScroll from './useInfiniteScroll';

const useFetch = (selected: ExtraPart) => {
const { ref, count, setCount } = useInfiniteScroll();
const jobRecordRef = useRef<Record<number, 'QUEUED' | 'DONE'>>({});
const isIncrementable = () =>
Object.values(jobRecordRef.current).every((value) => value === 'DONE');

const { ref, count, setCount } = useInfiniteScroll(isIncrementable);
const [canGetMoreReviews, setCanGetMoreReviews, setCanNotGetMoreReviews] = useBooleanState(true);

useEffect(() => {
jobRecordRef.current[count] = 'QUEUED';
}, [count]);

useEffect(() => {
function initializeStates() {
setCount(1);
setCanGetMoreReviews();
for (const key in jobRecordRef.current) {
if (Object.hasOwnProperty.call(jobRecordRef.current, key)) {
delete jobRecordRef.current[key];
}
}
}
return () => {
initializeStates();
Expand All @@ -23,6 +36,7 @@ const useFetch = (selected: ExtraPart) => {
setCanNotGetMoreReviews();
const response = await api.reviewAPI.getReviews(selected, count);
response.hasNextPage ? setCanGetMoreReviews() : setCanNotGetMoreReviews();
jobRecordRef.current[count] = 'DONE';
return response.reviews;
}, [selected, count, setCanGetMoreReviews, setCanNotGetMoreReviews]);
const state = useStackedFetchBase(willFetch, count === 1);
Expand Down
6 changes: 4 additions & 2 deletions src/views/ReviewPage/hooks/useInfiniteScroll.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useState } from 'react';
import useIntersectionObserver from '@src/hooks/useIntersectionObserver';

export default function useInfiniteScroll() {
export default function useInfiniteScroll(isIncrementable: () => boolean) {
const [count, setCount] = useState(1);

const ref = useIntersectionObserver(
async (entry, observer) => {
setCount((prevCount) => prevCount + 1);
if (isIncrementable()) {
setCount((prevCount) => prevCount + 1);
}
observer.unobserve(entry.target);
},
{ rootMargin: '80px' },
Expand Down

0 comments on commit ad24e89

Please sign in to comment.