Skip to content

Commit

Permalink
fix: reset lastPageReached state on search change for infinite scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
amalv committed Dec 29, 2023
1 parent a0083c2 commit d868c69
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { setContext } from "@apollo/client/link/context";

export const darkModeVar = makeVar(
window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
window?.matchMedia("(prefers-color-scheme: dark)")?.matches
);

const API_URL =
Expand Down
18 changes: 12 additions & 6 deletions src/components/LibraryPage/components/Books/hooks/useBooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,25 @@ export const useBooks = ({ search, limit }: UseBooksProps) => {
const [lastPageReached, setLastPageReached] = useState(false);
const [isErrorSnackbarOpen, setIsErrorSnackbarOpen] = useState(false);
// Fetch books
const { loading, error, data, fetchMore } = useQuery<BooksData, BooksVars>(
BOOKS_QUERY,
{
variables: { title: search, author: search, limit, cursor: "0" },
}
);
const { loading, error, data, fetchMore, refetch } = useQuery<
BooksData,
BooksVars
>(BOOKS_QUERY, {
variables: { title: search, author: search, limit, cursor: "0" },
});

useEffect(() => {
if (error) {
setIsErrorSnackbarOpen(true);
}
}, [error]);

useEffect(() => {
setLastPageReached(false);
lastPageReachedRef.current = false;
refetch({ title: search, author: search, limit, cursor: "0" });
}, [search, refetch, limit]);

const handleCloseSnackbar = getCloseSnackbarHandler(setIsErrorSnackbarOpen);
const updateQuery = getUpdateQuery(setLastPageReached);
const loader = useRef(null);
Expand Down

0 comments on commit d868c69

Please sign in to comment.