Skip to content

Commit

Permalink
Feat: 댓글 등록 #65
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyeeeah committed Oct 16, 2023
1 parent e4607fb commit 6f526dd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
39 changes: 11 additions & 28 deletions components/ui/BoardCard/BoardCardPackage/CommentWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Image from 'next/image';
import { useState } from 'react';
import { postComment } from '@/service/board';
import Toast from '@/utils/notification';
import usePostComment from '@/hooks/mutations/usePostComment';
import FlexBox from '../../FlexBox';
import Button from '../../Button';

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

// 데이터 전송을 위한 함수
const onUploadComment = async () => {
if (!commentText) {
return;
}
setIsUploading(true);
try {
const response = await postComment({
boardId,
parentId: 1,
content: commentText,
});
if (response.content) {
Toast.success('댓글이 성공적으로 업로드되었습니다.');
setCommentText('');
} else {
Toast.error('업로드에 실패했습니다. 잠시 후 다시 시도해주세요.');
}
} catch (error) {
Toast.error('오류가 발생했습니다. 잠시 후 다시 시도해주세요.');
} finally {
setIsUploading(false);
}
const postNewComment = () => {
commentMutate({
boardId,
parentId: 1,
content: commentText,
});
};

return (
<FlexBox
direction="column"
Expand Down Expand Up @@ -97,8 +80,8 @@ export function BoardCardCommentWrapper({
value={commentText}
onChange={(event) => setCommentText(event.target.value)}
/>
<Button onClickAction={() => onUploadComment()} disabled={isUploading}>
등록
<Button onClickAction={postNewComment} disabled={isLoading}>
{isLoading ? <p>등록 중...</p> : <span>등록</span>}
</Button>
</FlexBox>
</FlexBox>
Expand Down
3 changes: 2 additions & 1 deletion hooks/common/useRelativeTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ export default function useRelativeTime(createdDate: string): string {
const daysAgo = Math.floor(timeDifference / (24 * 60 * 60 * 1000));
return `${daysAgo}일 전`;
}
// 시간 단위
if (
// 시간 단위
timeDifference < 24 * 60 * 60 * 1000 &&
timeDifference >= 60 * 60 * 1000
) {
const hoursAgo = Math.floor(timeDifference / (60 * 60 * 1000));
return `${hoursAgo}시간 전`;
}
// 1시간 이내 일 경우
if (timeDifference < 60 * 60 * 1000) {
return '1시간 전';
}
Expand Down
20 changes: 20 additions & 0 deletions hooks/mutations/usePostComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useQueryClient, useMutation } from '@tanstack/react-query';
import Toast from '@/utils/notification';
import { PostCommentType } from '@/types/types';
import { postComment } from '@/service/board';

export default function usePostComment() {
const queryClient = useQueryClient();
const { mutate, isLoading } = useMutation({
mutationFn: (postCommentData: PostCommentType) =>
postComment(postCommentData),
onSuccess: () => {
Toast.success('댓글이 성공적으로 업로드 되었습니다.');
return queryClient.invalidateQueries(['comment']);
},
onError: () => {
Toast.error('댓글을 업로드하지 못했습니다. 잠시후 다시 시도해주세요.🥲');
},
});
return { mutate, isLoading };
}
12 changes: 1 addition & 11 deletions service/board.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { AuthError } from '@/lib/error';

interface PostBoardType {
title: string;
content: string;
}

interface PostCommentType {
boardId: number;
parentId: number;
content: string;
}
import { PostBoardType, PostCommentType } from '@/types/types';

export async function postBoard(postBoardData: PostBoardType) {
const url = `endpoint/api/board/register`;
Expand Down
11 changes: 11 additions & 0 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ export interface Comment {
// children: string[];
}

export interface PostBoardType {
title: string;
content: string;
}

export interface PostCommentType {
boardId: number;
parentId: number;
content: string;
}

export interface LocationInfoType {
predictions: string[];
location: {
Expand Down

0 comments on commit 6f526dd

Please sign in to comment.