Skip to content

Commit e965317

Browse files
committed
Fix: 게시물 리스트 조회 수정 #65
1 parent 2adaac5 commit e965317

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

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

+34-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { Dispatch, SetStateAction } from 'react';
55
import useGetBoardList from '@/hooks/queries/useGetBoardList';
66
import { Board } from '@/types/types';
7+
import FeedBoardLoading from '@/components/ui/Loading/FeedBoardLoading';
78
import FlexBox from '../../../../ui/FlexBox';
89
import FeedBoardCard from '../../../../ui/BoardCard/FeedBoardCard';
910

@@ -14,36 +15,45 @@ export default function BoardsList({
1415
setSelectedBoard: Dispatch<SetStateAction<Board | null>>;
1516
setShowModal: Dispatch<SetStateAction<boolean>>;
1617
}) {
17-
const { Observer, data: boardList } = useGetBoardList({
18+
const {
19+
Observer,
20+
data: boardList,
21+
isLoading,
22+
} = useGetBoardList({
1823
infiniteQueryKey: ['boards'],
1924
});
2025

2126
return (
22-
<FlexBox direction="column" className="gap-10">
23-
{boardList?.pages ? (
27+
<FlexBox direction="column" className="w-full gap-10">
28+
{isLoading ? (
29+
<FeedBoardLoading />
30+
) : (
31+
boardList?.pages &&
2432
boardList?.pages?.map((page) =>
25-
page.content.map((board) => (
26-
<div
27-
key={board.id}
28-
onClick={() => {
29-
setSelectedBoard(board);
30-
}}
31-
className="w-full"
32-
>
33-
<FeedBoardCard
34-
userId={board.writer}
35-
content={board.content}
36-
// TODO: 이미지 연결
37-
imgs={[]}
38-
setShowModal={setShowModal}
39-
comments={board.replyListDto}
40-
commentsCount={board.replyCount}
41-
/>
42-
</div>
43-
)),
33+
page.content.length > 0 ? (
34+
page.content.map((board) => (
35+
<div
36+
key={board.id}
37+
onClick={() => {
38+
setSelectedBoard(board);
39+
}}
40+
className="w-full"
41+
>
42+
<FeedBoardCard
43+
userId={board.writer}
44+
content={board.content}
45+
// TODO: 이미지 연결
46+
imgs={board.fileNames}
47+
setShowModal={setShowModal}
48+
comments={board.replyListDto}
49+
commentsCount={board.replyCount}
50+
/>
51+
</div>
52+
))
53+
) : (
54+
<div>아직 게시물이 없어요ㅠㅠ</div>
55+
),
4456
)
45-
) : (
46-
<div>아직 게시물이 없어요ㅠㅠ</div>
4757
)}
4858
<Observer>
4959
<div>로딩스피너...</div>

components/pages/main/Feed/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { useState } from 'react';
44
import BoardsList from '@/components/pages/main/Feed/BoardsList';
55
import FlexBox from '@/components/ui/FlexBox';
66
import FeedHeader from '@/components/pages/main/Feed/FeedHeader';
7-
import { BoardList } from '@/types/types';
7+
import { Board } from '@/types/types';
88
import BoardModal from '../../../ui/BoardModal';
99

1010
export default function Feed() {
1111
const [showModal, setShowModal] = useState(false);
12-
const [selectedBoard, setSelectedBoard] = useState<BoardList | null>(null);
12+
const [selectedBoard, setSelectedBoard] = useState<Board | null>(null);
1313

1414
return (
1515
<FlexBox
1616
direction="column"
17-
className={`gap-10 ${showModal ? 'overflow-hidden' : null}`}
17+
className={`gap-10 w-full ${showModal ? 'overflow-hidden' : null}`}
1818
>
1919
<FeedHeader />
2020
<BoardsList

components/ui/BoardCard/BoardCardPackage/Content.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function BoardCardContent({
2121
children: React.ReactNode;
2222
type: 'mainPC' | 'modal' | 'myPage';
2323
content: string;
24-
imgs?: string[];
24+
imgs: string[];
2525
onClickModal?: () => void;
2626
}) {
2727
const [imgNum, setImgNum] = useState(0);
@@ -59,7 +59,7 @@ export default function BoardCardContent({
5959
</FlexBox>
6060
);
6161

62-
if (imgs) {
62+
if (imgs.length > 0) {
6363
// 이미지가 있는 경우
6464
return (
6565
// eslint-disable-next-line react/jsx-no-useless-fragment

hooks/queries/useGetBoardList.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useEffect } from 'react';
44
import { useInView } from 'react-intersection-observer';
55
import { useInfiniteQuery } from '@tanstack/react-query';
66
import getBoardList from '@/service/board';
7+
import { BoardList } from '@/types/types';
78

89
interface InfiniteScrollProps {
910
infiniteQueryKey: string[];
@@ -16,15 +17,14 @@ export default function useGetBoardList({
1617
infiniteQueryKey,
1718
pageParameter = 1,
1819
pageSize = 5,
19-
inViewThreshold = 1,
2020
}: InfiniteScrollProps) {
2121
const { data, fetchNextPage, isLoading, hasNextPage, isFetchingNextPage } =
2222
useInfiniteQuery({
2323
queryKey: infiniteQueryKey,
24-
queryFn: ({ pageParam = pageParameter }) =>
24+
queryFn: ({ pageParam = pageParameter }): Promise<BoardList> =>
2525
getBoardList({ pageParam, pageSize }),
26-
getNextPageParam: (lastPage, allPages) =>
27-
allPages.length < 20 ? allPages.length + 1 : undefined,
26+
getNextPageParam: (boardlist) =>
27+
boardlist.last ? undefined : boardlist.number + 1,
2828
select: (d) => ({
2929
pages: d.pages.flatMap((page) => page),
3030
pageParams: d.pageParams,
@@ -33,7 +33,7 @@ export default function useGetBoardList({
3333

3434
// 무한 스크롤 화면 가장 아래 부분 탐지하는 observer
3535
function Observer({ children }: { children: React.ReactNode }) {
36-
const { ref, inView } = useInView({ threshold: inViewThreshold });
36+
const { ref, inView } = useInView({ threshold: 1 });
3737

3838
useEffect(() => {
3939
if (inView && hasNextPage) {
@@ -50,5 +50,6 @@ export default function useGetBoardList({
5050
return {
5151
Observer,
5252
data,
53+
isLoading,
5354
};
5455
}

service/board.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { AuthError } from '@/lib/error';
2-
import { BoardList } from '@/types/types';
32

43
interface PostBoardType {
54
title: string;
@@ -26,15 +25,13 @@ export async function postBoard(postBoardData: PostBoardType) {
2625
},
2726
body: formData,
2827
});
29-
// return response.json();
30-
const data = await response.json();
31-
console.log(data);
28+
return response.json();
3229
}
3330

3431
export default async function getBoardList({
3532
pageParam,
3633
pageSize,
37-
}: TempPostListApiProps): Promise<BoardList[]> {
34+
}: TempPostListApiProps) {
3835
try {
3936
const url = `/endpoint/api/board/list?_page=${pageParam}&_limit=${pageSize}`;
4037
const response = await fetch(url);

0 commit comments

Comments
 (0)