diff --git a/src/apis/api.js b/src/apis/api.js index 34a29073..1aa6d512 100644 --- a/src/apis/api.js +++ b/src/apis/api.js @@ -111,8 +111,14 @@ export const updateComment = async (id, data) => { }; // 과제 !! -export const deleteComment = async (id) => { - +export const deleteComment = async (id, data) => { + const response = await instanceWithToken.delete(`/comment/${id}/`, data); + if (response.status === 204) { + console.log("COMMENT DELETE SUCCESS"); + window.location.reload(); + } else { + console.log("[ERROR] error while deleting comment"); + } }; export const getUser = async () => { diff --git a/src/components/Comment/CommentElement.jsx b/src/components/Comment/CommentElement.jsx index fd48385e..3e4cbd27 100644 --- a/src/components/Comment/CommentElement.jsx +++ b/src/components/Comment/CommentElement.jsx @@ -1,12 +1,12 @@ import { useState, useEffect } from "react"; +import { getCookie } from "../../utils/cookie"; +import { getUser, updateComment } from "../../apis/api"; -const CommentElement = (props) => { - const { comment, handleCommentDelete, postId } = props; - const [content, setContent] = useState(comment.content); +const CommentElement = ({ comment, handleCommentDelete }) => { const [isEdit, setIsEdit] = useState(false); - - const [onChangeValue, setOnChangeValue] = useState(content); // 수정 취소 시 직전 content 값으로 변경을 위한 state - + const [onChangeValue, setOnChangeValue] = useState(comment.content); // 수정 취소 시 직전 content 값으로 변경을 위한 state + const [user, setUser] = useState(); + // comment created_at 전처리 const date = new Date(comment.created_at); const year = date.getFullYear(); @@ -15,18 +15,24 @@ const CommentElement = (props) => { let day = date.getDate(); day = day < 10 ? `0${day}` : day; - const handleEditComment = () => { // add api call for editing comment - setContent(onChangeValue); - setIsEdit(!isEdit); - console.log({ - post: postId, - comment: comment.id, - content: content - }); - }; + useEffect(() => { + // access_token이 있으면 유저 정보 가져옴 + const fetchUser = async () => { + + try { if (getCookie("access_token")) + + { const user = await getUser(); setUser(user); } + + } catch (error) { console.error("Error fetching user:", error); } + + }; + + fetchUser(); + }, []); - useEffect(() => { // add api call to check if user is the author of the comment - }, []); + const handleEditComment = () => { // add api call for editing comment + updateComment(comment.id, { content: onChangeValue}) + }; return (
@@ -34,24 +40,23 @@ const CommentElement = (props) => { {isEdit ? ( setOnChangeValue(e.target.value)} /> ) : ( -

{content}

+

{comment.content}

)} {year}.{month}.{day}
-
{isEdit ? ( <> - + - ) : ( + ) : user?.id === comment?.author ? ( <> - )} + ) : null}
); diff --git a/src/components/Comment/index.jsx b/src/components/Comment/index.jsx index c8c4a27e..df766e4e 100644 --- a/src/components/Comment/index.jsx +++ b/src/components/Comment/index.jsx @@ -1,53 +1,83 @@ -import { useState } from "react"; -import comments from "../../data/comments"; // dummy data +import { useState, useEffect } from "react"; import CommentElement from "./CommentElement"; +import { getComments, createComment, deleteComment } from "../../apis/api"; +import { getCookie } from "../../utils/cookie"; const Comment = ({ postId }) => { - const [commentList, setCommentList] = useState(comments); // state for comments - const [newContent, setNewContent] = useState(""); // state for new comment - - const handleCommentSubmit = (e) => { - e.preventDefault(); - setCommentList([ // TODO: add api call for creating comment - ...commentList, - { - id: commentList.length + 1, - content: newContent, - created_at: new Date().toISOString(), - post: postId, - author: { - id: 1, - username: "user1" - } - } - ]); - console.log({ - post: postId, - content: newContent - }); - setNewContent(""); + const [commentList, setCommentList] = useState([]); // state for comments + const [comment, setComment] = useState({ + post: postId, + content: "", + }); // state for new comment + const [isUserLoggedIn, setIsUserLoggedIn] = useState(false); + + useEffect(() => { + + const getCommentsAPI = async () => { + const comments = await getComments(postId); + setCommentList(comments); }; + getCommentsAPI(); - const handleCommentDelete = (commentId) => { - console.log("comment: ", commentId); - setCommentList(commentList.filter((comment) => comment.id !== commentId)); // TODO: add api call for deleting comment - }; + const loggedIn = getCookie("access_token") ? true : false; + setIsUserLoggedIn(loggedIn); + }, []); + + const handleChange = (e) => { + setComment({ ...comment, content: e.target.value }); + }; + + const handleCommentSubmit = (e) => { + e.preventDefault(); + if (!isUserLoggedIn) { + alert("댓글을 작성하려면 로그인하세요") + return; + } + createComment(comment); + }; + + const handleCommentDelete = (commentId) => { + const confirmDelete = window.confirm("정말 삭제하시겠습니까?"); + if (!confirmDelete) return; + try { + deleteComment(commentId); + } catch (error) { + console.error(error); + } + }; - return ( -
-

Comments

- {commentList.map((comment) => { - return ( - - ); - })} - -
- setNewContent(e.target.value)} /> - -
-
- ); + return ( +
+

Comments

+ {commentList.map((comment) => { + return ( + + ); + })} + +
+ + +
+
+ ); }; export default Comment; diff --git a/src/routes/PostDetailPage.jsx b/src/routes/PostDetailPage.jsx index 2d019896..fef75332 100644 --- a/src/routes/PostDetailPage.jsx +++ b/src/routes/PostDetailPage.jsx @@ -4,15 +4,13 @@ import { BigPost } from "../components/Posts"; import Comment from "../components/Comment"; import { getPost, getUser, deletePost } from "../apis/api"; import { getCookie } from "../utils/cookie"; - const PostDetailPage = () => { const { postId } = useParams(); const [post, setPost] = useState(null); - - const [user, setUser] = useState() - + const [user, setUser] = useState(); + useEffect(() => { // access_token이 있으면 유저 정보 가져옴 if (getCookie("access_token")) { @@ -31,6 +29,7 @@ const PostDetailPage = () => { }; getPostAPI(); }, [postId]); + // 작성했던 getPost()를 호출한 후, setPostList를 통해 postList에 저장 const navigate = useNavigate(); const onClickDelete = async () => {