diff --git a/src/apis/api.js b/src/apis/api.js index 41ddee1d..99fffb75 100644 --- a/src/apis/api.js +++ b/src/apis/api.js @@ -113,7 +113,15 @@ export const updateComment = async (id, data) => { }; // 과제 !! -export const deleteComment = async (id) => {}; +export const deleteComment = async (id) => { + const response = await instanceWithToken.delete(`/comment/${id}/`); + if (response.status === 204) { + console.log("DELETE SUCCESS"); + window.location.reload(); + } else { + console.log("[ERROR] error while deleting delete"); + } +}; export const getUser = async () => { const response = await instanceWithToken.get("/account/info/"); diff --git a/src/components/Comment/CommentElement.jsx b/src/components/Comment/CommentElement.jsx index fd48385e..0bfdc2e3 100644 --- a/src/components/Comment/CommentElement.jsx +++ b/src/components/Comment/CommentElement.jsx @@ -1,59 +1,96 @@ import { useState, useEffect } from "react"; +import { updateComment } from "../../apis/api"; +import { getUser } from "../../apis/api"; +import { getCookie } from "../../utils/cookie"; const CommentElement = (props) => { - const { comment, handleCommentDelete, postId } = props; - const [content, setContent] = useState(comment.content); - const [isEdit, setIsEdit] = useState(false); - - const [onChangeValue, setOnChangeValue] = useState(content); // 수정 취소 시 직전 content 값으로 변경을 위한 state - - // comment created_at 전처리 - const date = new Date(comment.created_at); - const year = date.getFullYear(); - let month = date.getMonth() + 1; - month = month < 10 ? `0${month}` : month; - 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(() => { // add api call to check if user is the author of the comment - }, []); - - return ( -
-
- {isEdit ? ( - setOnChangeValue(e.target.value)} /> - ) : ( -

{content}

- )} - - {year}.{month}.{day} -
- -
- {isEdit ? ( - <> - - - - ) : ( - <> - - - - )} -
-
- ); + const { comment, handleCommentDelete, postId } = props; + const [content, setContent] = useState(comment.content); + const [isEdit, setIsEdit] = useState(false); + const [user, setUser] = useState(); + + const [onChangeValue, setOnChangeValue] = useState(content); // 수정 취소 시 직전 content 값으로 변경을 위한 state + + // comment created_at 전처리 + const date = new Date(comment.created_at); + const year = date.getFullYear(); + let month = date.getMonth() + 1; + month = month < 10 ? `0${month}` : month; + let day = date.getDate(); + day = day < 10 ? `0${day}` : day; + + useEffect(() => { + // access_token이 있으면 유저 정보 가져옴 + if (getCookie("access_token")) { + const getUserAPI = async () => { + const user = await getUser(); + setUser(user); + }; + getUserAPI(); + } + }, []); + + const handleEditComment = () => { + // add api call for editing comment + updateComment(comment.id, { content: onChangeValue }); + setIsEdit(!isEdit); + console.log({ + post: postId, + comment: comment.id, + content: content, + }); + }; + + useEffect(() => { + // add api call to check if user is the author of the comment + }, []); + + console.log(comment); + return ( +
+
+ {isEdit ? ( + setOnChangeValue(e.target.value)} + /> + ) : ( +

{content}

+ )} + + + {year}.{month}.{day} + +
+ +
+ {user?.id === comment?.author ? ( + <> + {isEdit ? ( + <> + + + + ) : ( + <> + + + + )} + + ) : null} +
+
+ ); }; export default CommentElement; diff --git a/src/components/Comment/index.jsx b/src/components/Comment/index.jsx index c8c4a27e..ae7c354c 100644 --- a/src/components/Comment/index.jsx +++ b/src/components/Comment/index.jsx @@ -1,53 +1,69 @@ -import { useState } from "react"; -import comments from "../../data/comments"; // dummy data +import { useState, useEffect } from "react"; +import comments from "../../data/comments"; +import { getComments, createComment, deleteComment } from "../../apis/api"; import CommentElement from "./CommentElement"; const Comment = ({ postId }) => { - const [commentList, setCommentList] = useState(comments); // state for comments - const [newContent, setNewContent] = useState(""); // state for new comment + const [commentList, setCommentList] = useState([]); // 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(""); + useEffect(() => { + const getCommentsAPI = async () => { + const comments = await getComments(postId); + setCommentList(comments); }; + console.log("get comment success"); + getCommentsAPI(); + }, []); - const handleCommentDelete = (commentId) => { - console.log("comment: ", commentId); - setCommentList(commentList.filter((comment) => comment.id !== commentId)); // TODO: add api call for deleting comment - }; + const handleCommentSubmit = (e) => { + e.preventDefault(); + createComment({ + post: postId, + content: newContent, + }); //api.js에서 data에 해당하는부분은 {} 배열로 주어져야함. + console.log({ + post: postId, + content: newContent, + }); + setNewContent(""); + }; + + const handleCommentDelete = (commentId) => { + deleteComment(commentId); // TODO: add api call for deleting comment + }; + return ( +
+

Comments

+ {commentList.map((comment) => { + return ( + + ); + })} - return ( -
-

Comments

- {commentList.map((comment) => { - return ( - - ); - })} - -
- setNewContent(e.target.value)} /> - -
-
- ); +
+ setNewContent(e.target.value)} + /> + +
+
+ ); }; export default Comment; diff --git a/src/routes/PostDetailPage.jsx b/src/routes/PostDetailPage.jsx index 8dbdddde..24157553 100644 --- a/src/routes/PostDetailPage.jsx +++ b/src/routes/PostDetailPage.jsx @@ -44,6 +44,7 @@ const PostDetailPage = () => { } }; + return ( post && (