mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-27 02:04:24 +01:00
feat: added infinite scroll to feed page
This commit is contained in:
34
src/utils/hooks/useInfiniteQuery.ts
Normal file
34
src/utils/hooks/useInfiniteQuery.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { QueryResult } from "@apollo/client";
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
export const useInfiniteQuery = (query: QueryResult, dataField: string) => {
|
||||
const [fetchingMore, setFetchingMore] = useState(false)
|
||||
const [reachedLastPage, setReachedLastPage] = useState(false)
|
||||
|
||||
|
||||
const fetchMore = useCallback(
|
||||
() => {
|
||||
if (!fetchingMore && !reachedLastPage) {
|
||||
setFetchingMore(true);
|
||||
// console.log(feedQuery.variables?.skip);
|
||||
|
||||
query.fetchMore({
|
||||
variables: {
|
||||
skip: query.data?.[dataField].length,
|
||||
}
|
||||
}).then((d) => {
|
||||
if (d.data?.[dataField].length === 0)
|
||||
setReachedLastPage(true)
|
||||
|
||||
setFetchingMore(false)
|
||||
})
|
||||
}
|
||||
}
|
||||
, [dataField, fetchingMore, query, reachedLastPage]
|
||||
)
|
||||
|
||||
return {
|
||||
isFetchingMore: fetchingMore,
|
||||
fetchMore: fetchMore
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user