From 47c9aca8061fa717bb31fdf68c3ec98e61207968 Mon Sep 17 00:00:00 2001 From: kangjuhyup Date: Thu, 10 Oct 2024 21:32:38 +0900 Subject: [PATCH] feat : confirm observe --- src/pages/board/component/comment/index.tsx | 6 ++-- src/pages/board/component/input/index.tsx | 16 ++++++++-- src/pages/board/hook/rest/comment.ts | 34 ++++++++++++++++++++- src/pages/board/hook/useInput.ts | 5 ++- src/pages/board/hook/useList.ts | 12 +++++--- src/pages/board/index.tsx | 9 ++++-- 6 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/pages/board/component/comment/index.tsx b/src/pages/board/component/comment/index.tsx index 88f049f..fc17738 100644 --- a/src/pages/board/component/comment/index.tsx +++ b/src/pages/board/component/comment/index.tsx @@ -11,11 +11,11 @@ import classes from "./Comment.module.css"; import useList from "../../hook/useList"; interface CommentListProps { - refresh: () => void; + refresh: boolean; } -export const CommentList = ({}: CommentListProps) => { - const { listIdx, setListIdx, comments } = useList(); +export const CommentList = ({ refresh }: CommentListProps) => { + const { listIdx, setListIdx, comments } = useList(refresh); return ( diff --git a/src/pages/board/component/input/index.tsx b/src/pages/board/component/input/index.tsx index 39f80c4..0cb0927 100644 --- a/src/pages/board/component/input/index.tsx +++ b/src/pages/board/component/input/index.tsx @@ -17,7 +17,11 @@ import useInput from "../../hook/useInput"; import { useDisclosure } from "@mantine/hooks"; import useErrorStore from "../../../../store/useError"; -const CommentInput = () => { +interface CommentInputProps { + onConfirm: () => void; +} + +const CommentInput = ({ onConfirm }: CommentInputProps) => { const { setError } = useErrorStore(); const { avartarList, combobox } = useAvartar(); const [opened, { toggle, close }] = useDisclosure(false); @@ -34,6 +38,8 @@ const CommentInput = () => { company, setAvartar, avartar, + observeComment, + confirmComment, } = useInput(); useEffect(() => { @@ -41,11 +47,17 @@ const CommentInput = () => { }, [avartarList]); useEffect(() => { - if (addResponse) { + if (addResponse && email) { toggle(); + observeComment(email); } }, [addResponse]); + useEffect(() => { + console.log(confirmComment); + if (confirmComment) onConfirm(); + }, [confirmComment]); + const options = avartarList.map((item) => ( diff --git a/src/pages/board/hook/rest/comment.ts b/src/pages/board/hook/rest/comment.ts index 307055b..fded6c2 100644 --- a/src/pages/board/hook/rest/comment.ts +++ b/src/pages/board/hook/rest/comment.ts @@ -31,6 +31,7 @@ const useComment = () => { useState | null>(null); const [addResponse, setAddResponse] = useState(false); const [loading, setLoading] = useState(false); + const [confirmComment, setConfirm] = useState(false); const { setError } = useErrorStore(); const fetchComments = async (query: GetCommentsRequest) => { @@ -92,7 +93,38 @@ const useComment = () => { } }; - return { fetchComments, comments, addComment, addResponse, loading }; + const observeComment = async (email: string) => { + const eventSource = new EventSource( + `${import.meta.env.VITE_INTRO_API_URL}/comment/observe?email=${email}` + ); + + eventSource.onmessage = (event) => { + const data = JSON.parse(event.data); + setConfirm(data.confirm); + if (data.confirm) { + eventSource.close(); + } + }; + + eventSource.onerror = (err) => { + setError("댓글 확인 실패."); + eventSource.close(); + }; + + return () => { + eventSource.close(); + }; + }; + + return { + fetchComments, + comments, + addComment, + addResponse, + observeComment, + confirmComment, + loading, + }; }; export default useComment; diff --git a/src/pages/board/hook/useInput.ts b/src/pages/board/hook/useInput.ts index 0d07ebe..8477372 100644 --- a/src/pages/board/hook/useInput.ts +++ b/src/pages/board/hook/useInput.ts @@ -9,7 +9,8 @@ const useInput = () => { const [avartar, setAvartar] = useState(); const [comment, setComment] = useState(); - const { addComment, addResponse } = useComment(); + const { addComment, addResponse, observeComment, confirmComment } = + useComment(); return { setName, @@ -26,6 +27,8 @@ const useInput = () => { company, addComment, addResponse, + observeComment, + confirmComment, }; }; export default useInput; diff --git a/src/pages/board/hook/useList.ts b/src/pages/board/hook/useList.ts index 332d323..a8b92f1 100644 --- a/src/pages/board/hook/useList.ts +++ b/src/pages/board/hook/useList.ts @@ -1,6 +1,6 @@ import { useEffect, useState } from "react"; import useComment from "./rest/comment"; -const useList = () => { +const useList = (refresh: boolean) => { const { comments, fetchComments } = useComment(); const [listIdx, setListIdx] = useState(1); @@ -8,15 +8,17 @@ const useList = () => { fetchComments({ limit: 5, skip: (listIdx - 1) * 5 }); }, [listIdx]); - const refresh = () => { - setListIdx(1); - }; + useEffect(() => { + if (refresh) { + if (listIdx == 1) fetchComments({ limit: 5, skip: 0 }); + setListIdx(1); + } + }, [refresh]); return { comments: comments?.data, listIdx, setListIdx, - refresh, }; }; export default useList; diff --git a/src/pages/board/index.tsx b/src/pages/board/index.tsx index 6a54396..0640963 100644 --- a/src/pages/board/index.tsx +++ b/src/pages/board/index.tsx @@ -1,16 +1,21 @@ import { Container, Title } from "@mantine/core"; import { CommentList } from "./component/comment"; import CommentInput from "./component/input"; +import { useEffect, useState } from "react"; function Board() { + const [trigger, setTrigger] = useState(false); + useEffect(() => { + console.log("trigger => ", trigger); + }, [trigger]); return ( 동료 메세지 - {}} /> - + + setTrigger(true)} /> ); }