Skip to content

Commit 98d5221

Browse files
committed
Feat: 댓글 등록 api 연결 #65
1 parent 8085777 commit 98d5221

File tree

8 files changed

+91
-17
lines changed

8 files changed

+91
-17
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function BoardsList({
4747
setShowModal={setShowModal}
4848
comments={board.replyListDto}
4949
commentsCount={board.replyCount}
50+
boardId={board.id}
5051
/>
5152
</div>
5253
))

components/pages/main/Feed/FeedHeader/Upload.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default function Upload() {
7777
파일
7878
</Button>
7979
{isUploading ? (
80-
<div>업로드 </div>
80+
<div>업로드 중입니다</div>
8181
) : (
8282
<Button
8383
size="lg"

components/ui/BoardCard/BoardCardPackage/CommentWrapper.tsx

+38
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
import Image from 'next/image';
2+
import { useState } from 'react';
3+
import { postComment } from '@/service/board';
4+
import Toast from '@/utils/notification';
25
import FlexBox from '../../FlexBox';
6+
import Button from '../../Button';
37

48
export function BoardCardCommentWrapper({
59
children,
610
isModal = false,
711
commentsNum,
12+
boardId,
813
}: {
914
children: React.ReactNode;
1015
isModal?: boolean;
1116
commentsNum: number;
17+
boardId: number;
1218
}) {
19+
const [commentText, setCommentText] = useState('');
20+
const [isUploading, setIsUploading] = useState(false);
21+
22+
// 데이터 전송을 위한 함수
23+
const onUploadComment = async () => {
24+
if (!commentText) {
25+
return;
26+
}
27+
setIsUploading(true);
28+
try {
29+
const response = await postComment({
30+
boardId,
31+
parentId: 1,
32+
content: commentText,
33+
});
34+
if (response.content) {
35+
Toast.success('댓글이 성공적으로 업로드되었습니다.');
36+
setCommentText('');
37+
} else {
38+
Toast.error('업로드에 실패했습니다. 잠시 후 다시 시도해주세요.');
39+
}
40+
} catch (error) {
41+
Toast.error('오류가 발생했습니다. 잠시 후 다시 시도해주세요.');
42+
} finally {
43+
setIsUploading(false);
44+
}
45+
};
1346
return (
1447
<FlexBox
1548
direction="column"
@@ -59,7 +92,12 @@ export function BoardCardCommentWrapper({
5992
type="text"
6093
placeholder="댓글로 이웃과 소통해보세요!"
6194
className="border rounded-[10px] py-[16px] px-[20px] w-full body4 text-grey-400"
95+
value={commentText}
96+
onChange={(event) => setCommentText(event.target.value)}
6297
/>
98+
<Button onClickAction={() => onUploadComment()} disabled={isUploading}>
99+
등록
100+
</Button>
63101
</FlexBox>
64102
</FlexBox>
65103
);

components/ui/BoardCard/FeedBoardCard.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface FeedBoardCardProps {
1010
setShowModal: Dispatch<SetStateAction<boolean>>;
1111
comments: Comment[] | undefined;
1212
commentsCount: number;
13+
boardId: number;
1314
}
1415

1516
export default function FeedBoardCard({
@@ -19,6 +20,7 @@ export default function FeedBoardCard({
1920
setShowModal,
2021
comments,
2122
commentsCount,
23+
boardId,
2224
}: FeedBoardCardProps) {
2325
return (
2426
<FlexBox
@@ -33,7 +35,10 @@ export default function FeedBoardCard({
3335
imgs={imgs}
3436
onClickModal={() => setShowModal(true)}
3537
>
36-
<BoardCard.BoardCardCommentWrapper commentsNum={commentsCount}>
38+
<BoardCard.BoardCardCommentWrapper
39+
commentsNum={commentsCount}
40+
boardId={boardId}
41+
>
3742
<FlexBox
3843
direction="column"
3944
justify="start"

components/ui/BoardCard/ModalBoardCard.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import { BoardCardModal } from '@/components/ui/BoardCard/BoardCardPackage/Board
33

44
interface ModalBoardCardProps {
55
userId: string;
6-
imgs?: string[];
6+
imgs: string[];
77
content: string;
88
comments: Comment[] | undefined;
9+
commentsCount: number;
910
}
1011
export default function ModalBoardCard({
1112
userId,
1213
imgs,
1314
content,
1415
comments,
16+
commentsCount,
1517
}: ModalBoardCardProps) {
16-
const filteredCommentsCount = comments ? comments.length : 0;
17-
1818
return (
1919
<BoardCardModal imgs={imgs}>
2020
<BoardCardModal.Header userId={userId} />
2121
<BoardCardModal.Content type="modal" content={content} imgs={imgs}>
2222
<BoardCardModal.BoardCardCommentWrapper
23-
commentsNum={filteredCommentsCount}
23+
commentsNum={commentsCount}
2424
isModal
2525
>
2626
{comments?.map((comment) => (

components/ui/BoardModal/index.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ export default function BoardModal({
3333
{board ? (
3434
<ModalBoardCard
3535
userId={board.writer}
36-
imgs={[
37-
// TODO: 이미지 연결
38-
'/Feed/desktop/tempPostPic/tempPostPic1.svg',
39-
'/Feed/desktop/tempPostPic/tempPostPic3.svg',
40-
]}
36+
imgs={board.fileNames}
4137
content={board.title}
4238
comments={comments}
39+
commentsCount={board.replyCount}
4340
/>
4441
) : (
4542
<div>내용이 없습니다.</div>

hooks/queries/useGetBoardList.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useEffect } from 'react';
44
import { useInView } from 'react-intersection-observer';
55
import { useInfiniteQuery } from '@tanstack/react-query';
6-
import getBoardList from '@/service/board';
6+
import { getBoardList } from '@/service/board';
77
import { BoardList } from '@/types/types';
88

99
interface InfiniteScrollProps {

service/board.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ interface PostBoardType {
55
content: string;
66
}
77

8-
interface TempPostListApiProps {
8+
interface PostCommentType {
9+
boardId: number;
10+
parentId: number;
11+
content: string;
12+
}
13+
interface GetListApiProps {
914
pageParam: number;
1015
pageSize: number;
1116
}
@@ -24,10 +29,7 @@ export async function postBoard(postBoardData: PostBoardType) {
2429
return response.json();
2530
}
2631

27-
export default async function getBoardList({
28-
pageParam,
29-
pageSize,
30-
}: TempPostListApiProps) {
32+
export async function getBoardList({ pageParam, pageSize }: GetListApiProps) {
3133
try {
3234
const url = `/endpoint/api/board/list?pageNumber=${pageParam}&pageSize=${pageSize}`;
3335
const response = await fetch(url);
@@ -43,3 +45,34 @@ export default async function getBoardList({
4345
throw error;
4446
}
4547
}
48+
49+
export async function postComment(postCommentData: PostCommentType) {
50+
const url = `endpoint/api/reply/register`;
51+
52+
const response = await fetch(url, {
53+
method: 'POST',
54+
credentials: 'include',
55+
headers: {
56+
'Content-Type': 'application/json',
57+
},
58+
body: JSON.stringify(postCommentData),
59+
});
60+
return response.json();
61+
}
62+
63+
// export async function getCommentList({ pageParam, pageSize }: GetListApiProps) {
64+
// try {
65+
// const url = `/endpoint/api/reply/list?pageNumber=${pageParam}&pageSize=${pageSize}`;
66+
// const response = await fetch(url);
67+
// if (!response.ok) {
68+
// throw new Error(`서버오류:${response.status}`);
69+
// }
70+
// return await response.json();
71+
// } catch (error) {
72+
// if (error instanceof AuthError) {
73+
// window.location.replace('/auth/login');
74+
// alert(error.message);
75+
// }
76+
// throw error;
77+
// }
78+
// }

0 commit comments

Comments
 (0)