diff --git a/frontend/src/pages/Bucket/BucketDetail/Comment/CommentInput.tsx b/frontend/src/pages/Bucket/BucketDetail/Comment/CommentInput.tsx index a2f40c96..2fed6fb5 100644 --- a/frontend/src/pages/Bucket/BucketDetail/Comment/CommentInput.tsx +++ b/frontend/src/pages/Bucket/BucketDetail/Comment/CommentInput.tsx @@ -1,14 +1,18 @@ -import { ChangeEvent, useRef } from 'react' +import { ChangeEvent, KeyboardEvent, useRef } from 'react' import { useCommentStore } from '../../../../store/detailStore' +import { postBucketComment } from '../api' +import { useQueryClient } from '@tanstack/react-query' interface ICommentInput { + bucketId: string setIsInputFocused: React.Dispatch> } // Todo: Pwa 환경에서 해당 컴포넌트가 언마운트 될때 키보드가 내려가는지 확인 필요 -const CommentInput = ({ setIsInputFocused }: ICommentInput) => { +const CommentInput = ({ bucketId, setIsInputFocused }: ICommentInput) => { const { commentText, setCommentText } = useCommentStore() const inputRef = useRef(null) + const queryClient = useQueryClient() // QueryClient 인스턴스 사용 const handleCommentChange = (event: ChangeEvent) => { const text = event.currentTarget.value @@ -20,6 +24,23 @@ const CommentInput = ({ setIsInputFocused }: ICommentInput) => { const handleBlurInput = () => { setIsInputFocused(false) } + const handlePressEnter = async (event: KeyboardEvent) => { + if (event.key !== 'Enter') { + return + } + const commentRes = await postBucketComment(bucketId, commentText) + if (commentRes === 'success') { + setCommentText('') + queryClient.refetchQueries({ queryKey: ['commentList', bucketId] }) + } + } + const handleSubmitComment = async () => { + const commentRes = await postBucketComment(bucketId, commentText) + if (commentRes === 'success') { + setCommentText('') + queryClient.refetchQueries({ queryKey: ['commentList', bucketId] }) + } + } return (
@@ -33,8 +54,13 @@ const CommentInput = ({ setIsInputFocused }: ICommentInput) => { onFocus={handleFocusInput} onBlur={handleBlurInput} className="px-3 grow focus:outline-none" + onKeyDown={handlePressEnter} /> -
diff --git a/frontend/src/pages/Bucket/BucketDetail/Comment/CommentList.tsx b/frontend/src/pages/Bucket/BucketDetail/Comment/CommentList.tsx index cf8a654e..966e85ad 100644 --- a/frontend/src/pages/Bucket/BucketDetail/Comment/CommentList.tsx +++ b/frontend/src/pages/Bucket/BucketDetail/Comment/CommentList.tsx @@ -11,7 +11,6 @@ interface ICommentListProps { id: string } -// Todo : Api 데이터 타입 지정 후 as 삭제 예정 const CommentList = ({ isInputFocused, setIsInputShown, id }: ICommentListProps) => { const { commentText } = useCommentStore() const { pageType } = useDetailPageTypeStore() @@ -26,7 +25,7 @@ const CommentList = ({ isInputFocused, setIsInputShown, id }: ICommentListProps) if (lastElementInView && hasNextPage) { fetchNextPage() } - }, [lastElementInView, hasNextPage]) + }, [lastElementInView, hasNextPage, commentListData]) useEffect(() => { // 타겟이 보일 때 -> 항상 input이 보이도록 설정 diff --git a/frontend/src/pages/Bucket/BucketDetail/api.ts b/frontend/src/pages/Bucket/BucketDetail/api.ts index 43139f22..e7191afd 100644 --- a/frontend/src/pages/Bucket/BucketDetail/api.ts +++ b/frontend/src/pages/Bucket/BucketDetail/api.ts @@ -76,15 +76,28 @@ export const getBucketCommentList = async ({ const commentRes = await instance.get( `comment/bucket/${id}?page=${pageParam}&size=${fetchSize}` ) - // console.log(commentRes.data.bucketCommentList.commentList) + + console.log('comment list 받아오기!!') return commentRes.data.bucketCommentList.commentList } - +// - Post Request +interface IPostCommentRes { + result: string + message: string +} +export const postBucketComment = async ( + id: string, + context: string +): Promise<'success' | 'fail'> => { + const commentRes = await instance.post(`comment/bucket/${id}`, { context }) + return commentRes.data.result === 'ok' ? 'success' : 'fail' +} // :: LikeButton interface IPostLikeRes { result: string message: string } +// Todo : postBucketLikeStatus 이름 변경 필요, LikeRes 소문자로 변경하기 export const postLikeStatus = async (commentId: number): Promise<'success' | 'fail'> => { const LikeRes = await instance.put(`comment/bucket/like/${commentId}`) return LikeRes.data.result === 'ok' ? 'success' : 'fail' diff --git a/frontend/src/pages/Bucket/BucketDetail/index.tsx b/frontend/src/pages/Bucket/BucketDetail/index.tsx index e1108959..63df6c88 100644 --- a/frontend/src/pages/Bucket/BucketDetail/index.tsx +++ b/frontend/src/pages/Bucket/BucketDetail/index.tsx @@ -113,7 +113,9 @@ const BucketDetail = () => { )} - {isInputShown && } + {isInputShown && bucketId && ( + + )} ) }