Skip to content

Commit ab3a6e6

Browse files
committed
fix: scroll to load more pagination
1 parent 349fa6c commit ab3a6e6

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

apps/web/@/hooks/useInfinityScroll.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useCallback, useEffect, useRef, useState } from "react"
22

33
const useInfiniteScroll = (callback: () => void, root: HTMLElement | null, isFetching: boolean) => {
4-
// const [isFetching, setIsFetching] = useState(false)
54
const observer = useRef<IntersectionObserver | null>(null)
65
const [node, setNode] = useState<HTMLElement | null>(null)
76

@@ -22,7 +21,6 @@ const useInfiniteScroll = (callback: () => void, root: HTMLElement | null, isFet
2221
observer.current.disconnect()
2322
}
2423

25-
console.log("observer.current", observer.current)
2624
observer.current = new IntersectionObserver(handleIntersection, {
2725
root,
2826
rootMargin: "100px",

apps/web/@/molecules/post-list/index.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useSearchParams } from "next/navigation"
55

66
import { getPosts, PostStatus, TPostItem } from "database"
77
import { Loader } from "lucide-react"
8-
import { PostSkeleton } from "ui"
98

109
import useInfiniteScroll from "@/hooks/useInfinityScroll"
1110

@@ -22,19 +21,27 @@ export default function PostList({ containerClassName }: PostListProps) {
2221
const [loading, setLoading] = useState(false)
2322
const [posts, setPosts] = useState<TPostItem[]>([])
2423

25-
const loadMoreBooks = useCallback(async () => {
24+
const loadMorePosts = useCallback(async () => {
25+
console.log("hasMore", hasMore)
26+
if (!hasMore) return
27+
2628
setLoading(true)
2729
const newPosts = await getPosts({
2830
...searchParams,
2931
postStatus: PostStatus.PUBLISHED,
3032
page: page.toString(),
3133
})
34+
console.log("newPosts", newPosts)
3235
setPosts((prevPosts) => [...prevPosts, ...newPosts?.data?.data])
33-
setHasMore(page < newPosts?.data?.pagination?.totalPages)
36+
setHasMore(page < newPosts?.data?.totalPages)
37+
setPage((prevPage) => prevPage + 1)
3438
setLoading(false)
3539
}, [page, searchParams])
3640

37-
const { setNode } = useInfiniteScroll(loadMoreBooks, null, loading)
41+
const { setNode } = useInfiniteScroll(loadMorePosts, null, loading)
42+
43+
console.log("hasMore", hasMore)
44+
console.log("page", page)
3845

3946
return (
4047
<div className={containerClassName}>

0 commit comments

Comments
 (0)