|
1 | | -import { twMerge } from 'tailwind-merge'; |
| 1 | +import { useInfiniteQuery } from '@tanstack/react-query'; |
| 2 | +import { useEffect } from 'react'; |
| 3 | +import { useInView } from 'react-intersection-observer'; |
| 4 | +import { useNavigate } from 'react-router'; |
2 | 5 |
|
| 6 | +import { getSharePostList } from '@/apis/share'; |
3 | 7 | import BackgroundBottom from '@/components/BackgroundBottom'; |
4 | 8 | import NoticeRollingPaper from '@/components/NoticeRollingPaper'; |
5 | 9 | import PageTitle from '@/components/PageTitle'; |
6 | 10 |
|
7 | 11 | import LetterPreview from './components/LetterPreview'; |
8 | 12 |
|
9 | 13 | const LetterBoardPage = () => { |
10 | | - const isMyBoard = window.location.pathname.includes('/mypage'); |
| 14 | + const navigate = useNavigate(); |
| 15 | + const { ref, inView } = useInView(); |
| 16 | + |
| 17 | + const fetchPostList = async (page: number = 1) => { |
| 18 | + try { |
| 19 | + const response = await getSharePostList(page); |
| 20 | + if (!response) throw new Error('게시글 목록을 불러오는데 실패했습니다.'); |
| 21 | + console.log('page', response); |
| 22 | + return response as SharePostResponse; |
| 23 | + } catch (e) { |
| 24 | + console.error(e); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + const { data, isLoading, isError, fetchNextPage, hasNextPage, isFetchingNextPage } = |
| 29 | + useInfiniteQuery({ |
| 30 | + queryKey: ['sharePostList'], |
| 31 | + queryFn: ({ pageParam = 1 }) => fetchPostList(pageParam), |
| 32 | + enabled: true, |
| 33 | + initialPageParam: 1, |
| 34 | + getNextPageParam: (res) => { |
| 35 | + if (!res || res.currentPage >= res.totalPages) { |
| 36 | + return undefined; |
| 37 | + } |
| 38 | + return res.currentPage + 1; |
| 39 | + }, |
| 40 | + staleTime: 1000 * 60 * 5, |
| 41 | + gcTime: 1000 * 60 * 10, |
| 42 | + }); |
| 43 | + |
| 44 | + const postLists = data?.pages.flatMap((page) => page?.content) || []; |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (!hasNextPage) return; |
| 48 | + if (inView && !isFetchingNextPage) { |
| 49 | + fetchNextPage(); |
| 50 | + } |
| 51 | + }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]); |
| 52 | + |
| 53 | + if (isError) { |
| 54 | + navigate('/notFound'); |
| 55 | + } |
11 | 56 |
|
12 | 57 | return ( |
13 | 58 | <> |
14 | | - <main className={twMerge('flex grow flex-col px-5 pt-20 pb-10', !isMyBoard && 'mt-[-25px]')}> |
15 | | - {isMyBoard ? ( |
16 | | - <PageTitle className="mx-auto mb-11">내가 올린 게시물</PageTitle> |
| 59 | + <main className="mt-[-25px] flex grow flex-col px-5 pt-20 pb-10"> |
| 60 | + <> |
| 61 | + <NoticeRollingPaper /> |
| 62 | + <PageTitle className="mx-auto mt-4">게시판</PageTitle> |
| 63 | + <p className="text-gray-60 caption-m mt-4.5 text-center"> |
| 64 | + 따숨이에게 힘이 되었던 다양한 편지들을 모아두었어요 |
| 65 | + </p> |
| 66 | + </> |
| 67 | + {isLoading ? ( |
| 68 | + <p>loading</p> |
17 | 69 | ) : ( |
18 | | - <> |
19 | | - <NoticeRollingPaper /> |
20 | | - <PageTitle className="mx-auto mt-4">게시판</PageTitle> |
21 | | - <p className="text-gray-60 caption-m mt-4.5 text-center"> |
22 | | - 따숨이에게 힘이 되었던 다양한 편지들을 모아두었어요 |
23 | | - </p> |
24 | | - </> |
| 70 | + <section className="mt-6 grid grid-cols-2 gap-x-5 gap-y-4"> |
| 71 | + {postLists.map((item, index) => { |
| 72 | + return ( |
| 73 | + <LetterPreview |
| 74 | + key={index} |
| 75 | + id={item?.sharePostId || 0} |
| 76 | + to={item?.receiverZipCode || 'ERROR'} |
| 77 | + from={item?.writerZipCode || 'ERROR'} |
| 78 | + content={item?.content || 'no Data'} |
| 79 | + ref={index === postLists.length - 1 ? ref : null} |
| 80 | + /> |
| 81 | + ); |
| 82 | + })} |
| 83 | + </section> |
25 | 84 | )} |
26 | | - <section className="mt-6 grid grid-cols-2 gap-x-5 gap-y-4"> |
27 | | - {Array.from({ length: 10 }).map((_, index) => ( |
28 | | - <LetterPreview |
29 | | - key={index} |
30 | | - id={`${index}`} |
31 | | - to="12E21" |
32 | | - from="12E21" |
33 | | - content="저희가 주고 받은 행운의 편지 저희가 주고 받은 행운의 편지 저희가 주고 받은 행운의 편지 |
34 | | - 저희가 주고 받은 행운의 편지" |
35 | | - /> |
36 | | - ))} |
37 | | - </section> |
38 | 85 | </main> |
39 | 86 | <BackgroundBottom /> |
40 | 87 | </> |
|
0 commit comments