|
1 | 1 | 'use client';
|
2 | 2 |
|
3 |
| -import { useCallback, useEffect, useRef, useState } from 'react'; |
| 3 | +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; |
4 | 4 |
|
5 |
| -import { getInfiniteScrollData } from '@/features/post/post-list/api/post-infinite-scroll'; |
6 |
| -import { PostItem } from '@/features/post/post-list/ui/post-item'; |
7 |
| -import { formatToLocaleDate } from '@/shared/lib/format-date'; |
8 |
| -import { Post } from '@/shared/types/post-types'; |
| 5 | +import { mapPostToViewModel } from '@/entities/post'; |
| 6 | +import { Post } from '@/entities/post'; |
| 7 | +import { getInfiniteScrollData } from '@/features/post/post-list'; |
| 8 | +import { PostItem } from '@/features/post/post-list'; |
9 | 9 |
|
10 |
| -type InfiniteScrollProps = { |
| 10 | +type PostInfiniteScrollProps = { |
11 | 11 | postList: Post[];
|
12 | 12 | lastPostId: string | null;
|
13 | 13 | hasMore: boolean;
|
14 | 14 | };
|
15 | 15 |
|
16 |
| -export function InfiniteScroll({ |
| 16 | +export function PostInfiniteScroll({ |
17 | 17 | postList,
|
18 | 18 | lastPostId,
|
19 | 19 | hasMore,
|
20 |
| -}: InfiniteScrollProps) { |
| 20 | +}: PostInfiniteScrollProps) { |
21 | 21 | const [loading, setLoading] = useState(false);
|
22 | 22 | const [posts, setPosts] = useState<Post[]>(postList);
|
23 | 23 | const [cursor, setCursor] = useState(lastPostId ? lastPostId : null);
|
@@ -55,24 +55,26 @@ export function InfiniteScroll({
|
55 | 55 | };
|
56 | 56 | }, [loadMorePosts]);
|
57 | 57 |
|
| 58 | + const postViewModels = useMemo( |
| 59 | + () => posts.map((post) => mapPostToViewModel(post)), |
| 60 | + [posts] |
| 61 | + ); |
| 62 | + |
58 | 63 | return (
|
59 | 64 | <div>
|
60 |
| - {posts.map(({ id, title, content, author, createdAt }) => { |
61 |
| - const localeCreatedAt = formatToLocaleDate(createdAt); |
62 |
| - return ( |
63 |
| - <PostItem |
64 |
| - key={id} |
65 |
| - linkPostId={id} |
66 |
| - title={title} |
67 |
| - content={content} |
68 |
| - author={author} |
69 |
| - localeCreatedAt={localeCreatedAt} |
70 |
| - /> |
71 |
| - ); |
72 |
| - })} |
| 65 | + {postViewModels.map((postViewModel) => ( |
| 66 | + <PostItem |
| 67 | + key={postViewModel.id} |
| 68 | + linkPostId={postViewModel.id} |
| 69 | + title={postViewModel.title} |
| 70 | + content={postViewModel.content} |
| 71 | + author={postViewModel.author} |
| 72 | + localeCreatedAt={postViewModel.localeCreatedAt} |
| 73 | + /> |
| 74 | + ))} |
73 | 75 | <h3
|
74 | 76 | ref={target}
|
75 |
| - className="mx-8 mb-4 mt-8 text-center text-9xl font-semibold" |
| 77 | + className="mx-8 mb-4 mt-8 text-center text-2xl font-semibold" |
76 | 78 | >
|
77 | 79 | {posts.at(-1)?.id === cursor
|
78 | 80 | ? '*************더 많은 게시글 로딩 중****************'
|
|
0 commit comments