11'use client' ;
22
3- import { useEffect , useState } from 'react' ;
3+ import { useEffect , useMemo , useState } from 'react' ;
44import CommunityFilter from './CommunityFilter' ;
55import CommunityTab from './CommunityTab' ;
66import PostCard from './PostCard' ;
77import WriteBtn from './WriteBtn' ;
88import { Post } from '../types/post' ;
9- import { getApi } from '@/app/ api/config/appConfig ' ;
10- import { fetchPost } from '../api/fetchPost ' ;
9+ import { fetchPostByTab } from '../ api/fetchPost ' ;
10+ import { useSearchParams } from 'next/navigation ' ;
1111
1212function Community ( ) {
1313 const [ posts , setPosts ] = useState < Post [ ] | null > ( [ ] ) ;
1414 const [ isLoading , setIsLoading ] = useState ( true ) ;
15+ const [ lastLoadedId , setLastLoadedId ] = useState < number | null > ( null ) ;
16+
17+ const searchParams = useSearchParams ( ) ;
18+
19+ const category = useMemo ( ( ) => searchParams . get ( 'category' ) || 'all' , [ searchParams ] ) ;
20+ const filter = useMemo (
21+ ( ) => ( searchParams . get ( 'postSortStatus' ) as 'LATEST' | 'POPULAR' | 'COMMENTS' ) || 'LATEST' ,
22+ [ searchParams ]
23+ ) ;
1524
1625 const [ isEnd , setIsEnd ] = useState ( false ) ;
1726
27+ useEffect ( ( ) => {
28+ setPosts ( [ ] ) ;
29+ setIsEnd ( false ) ;
30+ setLastLoadedId ( null ) ;
31+ loadInitialPosts ( ) ;
32+ } , [ category , filter ] ) ;
33+
34+ const loadInitialPosts = async ( ) => {
35+ const category = searchParams . get ( 'category' ) || 'all' ;
36+ const filter =
37+ ( searchParams . get ( 'postSortStatus' ) as 'LATEST' | 'POPULAR' | 'COMMENTS' ) || 'LATEST' ;
38+
39+ setIsLoading ( true ) ;
40+ setIsEnd ( false ) ;
41+
42+ // const latestPost = posts?.sort((a, b) => Number(b.createdAt) - Number(a.createdAt))
43+ // const latestLike = latestPost?.sort((a,b) => b.likeCount - a.likeCount );
44+ // const latestComment = latestPost?.sort((a,b) => b.commentCount - a.commentCount );
45+
46+ try {
47+ const newPosts = await fetchPostByTab ( {
48+ category,
49+ filter,
50+ } ) ;
51+
52+ if ( ! newPosts || newPosts . length === 0 ) {
53+ setIsEnd ( true ) ;
54+ setPosts ( [ ] ) ;
55+ } else {
56+ setPosts ( newPosts ) ;
57+ }
58+ } finally {
59+ setIsLoading ( false ) ;
60+ }
61+ } ;
62+
1863 const loadMorePosts = async ( lastPostId : number ) => {
1964 if ( isEnd || isLoading ) return ;
20- console . log ( '시작' ) ;
65+ if ( ! posts || posts . length === 0 ) return ;
66+ console . log ( '시작' , lastPostId ) ;
67+
68+ const lastPost = posts [ posts . length - 1 ] ;
69+ if ( lastPostId === lastPost . postId ) return ;
70+ setLastLoadedId ( lastPost . postId ) ;
2171
2272 setIsLoading ( true ) ;
2373 try {
24- const newPosts = await fetchPost ( lastPostId ) ;
25- console . log ( newPosts ) ;
74+ const category = searchParams . get ( 'category' ) || 'all' ;
75+ const filter =
76+ ( searchParams . get ( 'postSortStatus' ) as 'LATEST' | 'POPULAR' | 'COMMENTS' ) || 'LATEST' ;
77+
78+ const newPosts = await fetchPostByTab ( {
79+ category,
80+ filter,
81+ lastId : lastPostId ,
82+ } ) ;
2683
27- if ( newPosts ?. length === 0 ) {
84+ if ( ! newPosts || newPosts ?. length === 0 ) {
2885 setIsEnd ( true ) ;
86+ console . log ( '끝' ) ;
2987 } else {
3088 setPosts ( ( prev ) => [ ...( prev ?? [ ] ) , ...( newPosts ?? [ ] ) ] ) ;
3189 }
@@ -40,7 +98,7 @@ function Community() {
4098 aria-label = "탭과 글쓰기"
4199 className = "flex justify-between item-center sm:flex-row flex-col gap-4 mt-1"
42100 >
43- < CommunityTab setPosts = { setPosts } setIsLoading = { setIsLoading } />
101+ < CommunityTab setPosts = { setPosts } setIsLoading = { setIsLoading } setIsEnd = { setIsEnd } />
44102 < WriteBtn />
45103 </ section >
46104
0 commit comments