Skip to content

Commit 411184b

Browse files
committed
Refactor: infinite query의 select활용해서 가져온 데이터 정제 #65
1 parent a877c35 commit 411184b

File tree

10 files changed

+101
-88
lines changed

10 files changed

+101
-88
lines changed

components/pages/main/Feed/BoardsList/index.tsx

+5-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@ export default function BoardsList() {
1616
{isLoading ? (
1717
<FeedBoardLoading />
1818
) : (
19-
boardList?.pages &&
20-
boardList?.pages?.map((page) =>
21-
page.content.length > 0 ? (
22-
page.content.map((board) => (
23-
<div key={board.id} className="w-full">
24-
<FeedBoardCard board={board} />
25-
</div>
26-
))
27-
) : (
28-
<div>아직 게시물이 없어요ㅠㅠ</div>
29-
),
30-
)
19+
boardList?.pages?.map((board) => (
20+
<div key={board.id} className="w-full">
21+
<FeedBoardCard board={board} />
22+
</div>
23+
))
3124
)}
3225
{hasNextPage ? null : <div>🐾 더이상 게시물이 없어요 🐾</div>}
3326
<Observer>

components/pages/mypage/MyPageMain/MyBoardsList/index.tsx

+7-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ export default function MyBoardsList() {
1414
return (
1515
<div className="grid w-full gap-5 tablet:grid-cols-2 tablet:mt-4">
1616
{myBoards &&
17-
myBoards?.pages?.map((page) =>
18-
page.content.map((board) => (
19-
<div key={board.id} className="w-full">
20-
<Link key={board.id} href={`/board/${board.id}`}>
21-
<MyBoardCard board={board} />
22-
</Link>
23-
</div>
24-
)),
25-
)}
17+
myBoards?.pages?.map((board) => (
18+
<div key={board.id} className="w-full">
19+
<Link key={board.id} href={`/board/${board.id}`}>
20+
<MyBoardCard board={board} />
21+
</Link>
22+
</div>
23+
))}
2624
<Observer>
2725
<div>로딩중...</div>
2826
</Observer>

components/ui/BoardCard/BoardIdCard.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ export default function BoardIdCard({ boardId }: { boardId: number }) {
2525
likedCount={board.likedCount}
2626
isLiked={board.boardLiked}
2727
>
28-
{commentList?.pages.map((page) =>
29-
page.content.map((comment) => (
30-
<BoardCardId.BoardCardModalComments
31-
boardId={board.id}
32-
id={comment.id}
33-
userName={comment.nickname}
34-
content={comment.content}
35-
isUser={comment.replyWriter}
36-
userImage={comment.userImageUrl}
37-
/>
38-
)),
39-
)}
28+
{commentList?.pages.map((comment) => (
29+
<BoardCardId.BoardCardModalComments
30+
boardId={board.id}
31+
id={comment.id}
32+
userName={comment.nickname}
33+
content={comment.content}
34+
isUser={comment.replyWriter}
35+
userImage={comment.userImageUrl}
36+
/>
37+
))}
4038
<Observer>로딩중...</Observer>
4139
</BoardCardId.BoardCardCommentWrapper>
4240
</BoardCardId.Content>

components/ui/BoardCard/ModalBoardCard.tsx

+10-12
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ export default function ModalBoardCard({ boardId }: { boardId: number }) {
2525
likedCount={board.likedCount}
2626
isLiked={board.boardLiked}
2727
>
28-
{commentList?.pages.map((page) =>
29-
page.content.map((comment) => (
30-
<BoardCardModal.ModalComments
31-
boardId={board.id}
32-
id={comment.id}
33-
userName={comment.nickname}
34-
content={comment.content}
35-
isUser={comment.replyWriter}
36-
userImage={comment.userImageUrl}
37-
/>
38-
)),
39-
)}
28+
{commentList?.pages.map((comment) => (
29+
<BoardCardModal.ModalComments
30+
boardId={board.id}
31+
id={comment.id}
32+
userName={comment.nickname}
33+
content={comment.content}
34+
isUser={comment.replyWriter}
35+
userImage={comment.userImageUrl}
36+
/>
37+
))}
4038
<Observer>로딩중...</Observer>
4139
</BoardCardModal.BoardCardCommentWrapper>
4240
</BoardCardModal.Content>

hooks/common/useInfiniteScroll.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
'use client';
22

3-
import { useInfiniteQuery } from '@tanstack/react-query';
3+
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query';
44
import { useEffect } from 'react';
55
import { useInView } from 'react-intersection-observer';
66

7-
interface InfiniteScrollProps<T> {
7+
interface InfiniteScrollProps<T, P = undefined> {
88
queryKey: string[];
99
firstPageParam: number;
1010
queryFn: (pageNumber: number, ...params: number[]) => Promise<T>;
1111
getNextPageParamFn: (page: T) => void;
1212
params: number[];
13+
selectFn?: (data: InfiniteData<T>) => InfiniteData<P>;
1314
}
1415

15-
export default function useInfiniteScroll<T>({
16+
export default function useInfiniteScroll<T, P = undefined>({
1617
queryKey,
1718
firstPageParam,
1819
queryFn,
1920
getNextPageParamFn,
2021
params,
21-
}: InfiniteScrollProps<T>) {
22+
selectFn,
23+
}: InfiniteScrollProps<T, P>) {
2224
const { data, fetchNextPage, isLoading, hasNextPage, isFetchingNextPage } =
2325
useInfiniteQuery({
2426
queryKey,
2527
queryFn: ({ pageParam = firstPageParam }): Promise<T> =>
2628
queryFn(pageParam, ...params),
2729
getNextPageParam: getNextPageParamFn,
30+
select: selectFn,
2831
});
2932

3033
// 무한 스크롤 화면 가장 아래 부분 탐지하는 observer

hooks/queries/useGetBoardList.ts

-25
This file was deleted.

hooks/queries/useGetBoardList.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client';
2+
3+
import { getBoardList } from '@/service/board';
4+
import { Board, BoardList } from '@/types/types';
5+
import { queryKeys } from '@/constant/query-keys';
6+
import useInfiniteScroll from '../common/useInfiniteScroll';
7+
8+
export default function useGetBoardList() {
9+
const { data, isLoading, Observer, hasNextPage } = useInfiniteScroll<
10+
BoardList,
11+
Board
12+
>({
13+
queryKey: [queryKeys.BOARD_LIST],
14+
firstPageParam: 0,
15+
queryFn: getBoardList,
16+
getNextPageParamFn: (boardList) =>
17+
boardList.last ? undefined : boardList.number + 1,
18+
params: [],
19+
selectFn: (d) => ({
20+
pages: d.pages.flatMap((page) => page.content),
21+
pageParams: d.pageParams,
22+
}),
23+
});
24+
25+
return {
26+
Observer,
27+
data,
28+
isLoading,
29+
hasNextPage,
30+
};
31+
}

hooks/queries/useGetCommentList.tsx

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
'use client';
22

33
import { getCommentList } from '@/service/board';
4-
import { CommentList } from '@/types/types';
4+
import { CommentList, Comment } from '@/types/types';
55
import { queryKeys } from '@/constant/query-keys';
66
import useInfiniteScroll from '../common/useInfiniteScroll';
77

88
export default function useGetCommentList(boardId: number) {
9-
const { data, isLoading, Observer, hasNextPage } =
10-
useInfiniteScroll<CommentList>({
11-
queryKey: [queryKeys.COMMENT_LIST],
12-
firstPageParam: 0,
13-
queryFn: getCommentList,
14-
getNextPageParamFn: (boardList) =>
15-
boardList.last ? undefined : boardList.number + 1,
16-
params: [boardId],
17-
});
9+
const { data, isLoading, Observer, hasNextPage } = useInfiniteScroll<
10+
CommentList,
11+
Comment
12+
>({
13+
queryKey: [queryKeys.COMMENT_LIST],
14+
firstPageParam: 0,
15+
queryFn: getCommentList,
16+
getNextPageParamFn: (boardList) =>
17+
boardList.last ? undefined : boardList.number + 1,
18+
params: [boardId],
19+
selectFn: (d) => ({
20+
pages: d.pages.flatMap((page) => page.content),
21+
pageParams: d.pageParams,
22+
}),
23+
});
1824

1925
return {
2026
Observer,

hooks/queries/useGetMyBoardList.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { getMyBoardList } from '@/service/myPage';
2-
import { MyBoardList } from '@/types/types';
2+
import { Board, MyBoardList } from '@/types/types';
33
import { queryKeys } from '@/constant/query-keys';
44
import useInfiniteScroll from '../common/useInfiniteScroll';
55

66
export default function useGetMyBoardList() {
7-
const { data, isLoading, Observer } = useInfiniteScroll<MyBoardList>({
7+
const { data, isLoading, Observer } = useInfiniteScroll<MyBoardList, Board>({
88
queryKey: [queryKeys.MY_BOARD_LIST],
99
firstPageParam: 0,
1010
queryFn: getMyBoardList,
1111
getNextPageParamFn: (boardlist) =>
1212
boardlist.last ? undefined : boardlist.number + 1,
1313
params: [],
14+
selectFn: (d) => ({
15+
pages: d.pages.flatMap((page) => page.content),
16+
pageParams: d.pageParams,
17+
}),
1418
});
1519

1620
return {

hooks/queries/useGetMyBookmarkedBoardList.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import { getBookmarkedBoardList } from '@/service/myPage';
2-
import { BookmarkedBoardList } from '@/types/types';
2+
import { Board, BookmarkedBoardList } from '@/types/types';
33
import { queryKeys } from '@/constant/query-keys';
44
import useInfiniteScroll from '../common/useInfiniteScroll';
55

66
export default function useGetBookmarkedBoardList() {
7-
const { data, isLoading, Observer } = useInfiniteScroll<BookmarkedBoardList>({
7+
const { data, isLoading, Observer } = useInfiniteScroll<
8+
BookmarkedBoardList,
9+
Board
10+
>({
811
queryKey: [queryKeys.MY_BOOKMARKED_LIST],
912
firstPageParam: 0,
1013
queryFn: getBookmarkedBoardList,
1114
getNextPageParamFn: (boardlist) =>
1215
boardlist.last ? undefined : boardlist.number + 1,
1316
params: [],
17+
selectFn: (d) => ({
18+
pages: d.pages.flatMap((page) => page.content),
19+
pageParams: d.pageParams,
20+
}),
1421
});
1522

1623
return {

0 commit comments

Comments
 (0)