Skip to content

Commit 92e7fe5

Browse files
authored
Merge pull request #136 from CodeForStartup/feat/scroll-to-load-more
feat: home page - scroll to load more
2 parents b6c67d2 + 97aee26 commit 92e7fe5

File tree

6 files changed

+4345
-20
lines changed

6 files changed

+4345
-20
lines changed

apps/web/@/hooks/useInfinityScroll.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const useInfiniteScroll = (callback: () => void, root: HTMLElement | null, isFet
77

88
const handleIntersection = useCallback(
99
(entries: IntersectionObserverEntry[]) => {
10+
console.log("entries", entries)
1011
if (entries[0].isIntersecting && !isFetching) {
1112
callback?.()
1213
}
@@ -15,12 +16,13 @@ const useInfiniteScroll = (callback: () => void, root: HTMLElement | null, isFet
1516
)
1617

1718
useEffect(() => {
18-
if (!root || !node || isFetching) return
19+
if (!node || isFetching) return
1920

2021
if (observer.current) {
2122
observer.current.disconnect()
2223
}
2324

25+
console.log("observer.current", observer.current)
2426
observer.current = new IntersectionObserver(handleIntersection, {
2527
root,
2628
rootMargin: "100px",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use client"
2+
3+
import { useCallback, useState } from "react"
4+
import { useSearchParams } from "next/navigation"
5+
6+
import { getPosts, PostStatus, TPostItem } from "database"
7+
8+
import useInfiniteScroll from "@/hooks/useInfinityScroll"
9+
10+
import PostItem from "../posts/post-item"
11+
12+
export default function PostList() {
13+
const searchParams = useSearchParams()
14+
const [page, setPage] = useState(1)
15+
const [hasMore, setHasMore] = useState(true)
16+
const [loading, setLoading] = useState(false)
17+
const [posts, setPosts] = useState<TPostItem[]>([])
18+
19+
const loadMoreBooks = useCallback(async () => {
20+
setLoading(true)
21+
const newPosts = await getPosts({
22+
...searchParams,
23+
postStatus: PostStatus.PUBLISHED,
24+
page: page.toString(),
25+
})
26+
setPosts((prevPosts) => [...prevPosts, ...newPosts?.data?.data])
27+
setHasMore(page < newPosts?.data?.pagination?.totalPages)
28+
setLoading(false)
29+
}, [page, searchParams])
30+
31+
const { setNode } = useInfiniteScroll(loadMoreBooks, null, loading)
32+
33+
return (
34+
<div>
35+
{posts?.map((post) => (
36+
<PostItem
37+
key={post.id}
38+
post={post}
39+
/>
40+
))}
41+
42+
<div
43+
ref={setNode}
44+
className="h-10 w-full"
45+
>
46+
{loading && <div>Loading...</div>}
47+
</div>
48+
</div>
49+
)
50+
}

apps/web/app/[lang]/(public)/page.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Metadata } from "next"
22

33
import { getPosts, PostStatus } from "database"
44

5+
import useInfiniteScroll from "@/hooks/useInfinityScroll"
56
import Filter from "@/molecules/home/filter"
7+
import PostList from "@/molecules/post-list"
68
import PostItem from "@/molecules/posts/post-item"
79

810
export const metadata: Metadata = {
@@ -15,22 +17,11 @@ export default async function Page({
1517
}: {
1618
searchParams: { [key: string]: string | string[] | undefined }
1719
}) {
18-
const posts = await getPosts({
19-
...searchParams,
20-
postStatus: PostStatus.PUBLISHED,
21-
})
22-
2320
return (
2421
<div className="">
2522
<Filter />
26-
<div className="mt-4">
27-
{posts?.data?.data?.map((post) => (
28-
<PostItem
29-
key={post.id}
30-
post={post}
31-
/>
32-
))}
33-
</div>
23+
24+
<PostList />
3425
</div>
3526
)
3627
}

apps/web/app/[lang]/(public-fullwidth)/author/[authorId]/page.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ export const metadata = {
1414

1515
export default async function Page({ params, searchParams }) {
1616
const { data, total } = await getPosts({
17-
searchParams: {
18-
authorId: params?.authorId,
19-
...searchParams,
20-
},
17+
authorId: params?.authorId,
18+
...searchParams,
2119
})
2220

2321
return (

0 commit comments

Comments
 (0)