Skip to content

Commit 6f526dd

Browse files
committed
Feat: 댓글 등록 #65
1 parent e4607fb commit 6f526dd

File tree

5 files changed

+45
-40
lines changed

5 files changed

+45
-40
lines changed

components/ui/BoardCard/BoardCardPackage/CommentWrapper.tsx

+11-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Image from 'next/image';
22
import { useState } from 'react';
3-
import { postComment } from '@/service/board';
4-
import Toast from '@/utils/notification';
3+
import usePostComment from '@/hooks/mutations/usePostComment';
54
import FlexBox from '../../FlexBox';
65
import Button from '../../Button';
76

@@ -19,32 +18,16 @@ export function BoardCardCommentWrapper({
1918
likedCount: number;
2019
}) {
2120
const [commentText, setCommentText] = useState('');
22-
const [isUploading, setIsUploading] = useState(false);
21+
const { mutate: commentMutate, isLoading } = usePostComment();
2322

24-
// 데이터 전송을 위한 함수
25-
const onUploadComment = async () => {
26-
if (!commentText) {
27-
return;
28-
}
29-
setIsUploading(true);
30-
try {
31-
const response = await postComment({
32-
boardId,
33-
parentId: 1,
34-
content: commentText,
35-
});
36-
if (response.content) {
37-
Toast.success('댓글이 성공적으로 업로드되었습니다.');
38-
setCommentText('');
39-
} else {
40-
Toast.error('업로드에 실패했습니다. 잠시 후 다시 시도해주세요.');
41-
}
42-
} catch (error) {
43-
Toast.error('오류가 발생했습니다. 잠시 후 다시 시도해주세요.');
44-
} finally {
45-
setIsUploading(false);
46-
}
23+
const postNewComment = () => {
24+
commentMutate({
25+
boardId,
26+
parentId: 1,
27+
content: commentText,
28+
});
4729
};
30+
4831
return (
4932
<FlexBox
5033
direction="column"
@@ -97,8 +80,8 @@ export function BoardCardCommentWrapper({
9780
value={commentText}
9881
onChange={(event) => setCommentText(event.target.value)}
9982
/>
100-
<Button onClickAction={() => onUploadComment()} disabled={isUploading}>
101-
등록
83+
<Button onClickAction={postNewComment} disabled={isLoading}>
84+
{isLoading ? <p>등록 중...</p> : <span>등록</span>}
10285
</Button>
10386
</FlexBox>
10487
</FlexBox>

hooks/common/useRelativeTime.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ export default function useRelativeTime(createdDate: string): string {
88
const daysAgo = Math.floor(timeDifference / (24 * 60 * 60 * 1000));
99
return `${daysAgo}일 전`;
1010
}
11+
// 시간 단위
1112
if (
12-
// 시간 단위
1313
timeDifference < 24 * 60 * 60 * 1000 &&
1414
timeDifference >= 60 * 60 * 1000
1515
) {
1616
const hoursAgo = Math.floor(timeDifference / (60 * 60 * 1000));
1717
return `${hoursAgo}시간 전`;
1818
}
19+
// 1시간 이내 일 경우
1920
if (timeDifference < 60 * 60 * 1000) {
2021
return '1시간 전';
2122
}

hooks/mutations/usePostComment.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useQueryClient, useMutation } from '@tanstack/react-query';
2+
import Toast from '@/utils/notification';
3+
import { PostCommentType } from '@/types/types';
4+
import { postComment } from '@/service/board';
5+
6+
export default function usePostComment() {
7+
const queryClient = useQueryClient();
8+
const { mutate, isLoading } = useMutation({
9+
mutationFn: (postCommentData: PostCommentType) =>
10+
postComment(postCommentData),
11+
onSuccess: () => {
12+
Toast.success('댓글이 성공적으로 업로드 되었습니다.');
13+
return queryClient.invalidateQueries(['comment']);
14+
},
15+
onError: () => {
16+
Toast.error('댓글을 업로드하지 못했습니다. 잠시후 다시 시도해주세요.🥲');
17+
},
18+
});
19+
return { mutate, isLoading };
20+
}

service/board.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import { AuthError } from '@/lib/error';
2-
3-
interface PostBoardType {
4-
title: string;
5-
content: string;
6-
}
7-
8-
interface PostCommentType {
9-
boardId: number;
10-
parentId: number;
11-
content: string;
12-
}
2+
import { PostBoardType, PostCommentType } from '@/types/types';
133

144
export async function postBoard(postBoardData: PostBoardType) {
155
const url = `endpoint/api/board/register`;

types/types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ export interface Comment {
6161
// children: string[];
6262
}
6363

64+
export interface PostBoardType {
65+
title: string;
66+
content: string;
67+
}
68+
69+
export interface PostCommentType {
70+
boardId: number;
71+
parentId: number;
72+
content: string;
73+
}
74+
6475
export interface LocationInfoType {
6576
predictions: string[];
6677
location: {

0 commit comments

Comments
 (0)