diff --git a/src/apis/api.js b/src/apis/api.js index ab6543d4..8c6e42c3 100644 --- a/src/apis/api.js +++ b/src/apis/api.js @@ -88,12 +88,16 @@ export const getComments = async (postId) => { }; export const createComment = async (data) => { - const response = await instanceWithToken.post("/comment/", data); - if (response.status === 201) { - console.log("COMMENT SUCCESS"); - window.location.reload(); // 새로운 코멘트 생성시 새로고침으로 반영 - } else { - console.log("[ERROR] error while creating comment"); + try { + const response = await instanceWithToken.post("/comment/", data); + if (response.status === 201) { + console.log("COMMENT SUCCESS"); + window.location.reload(); // 새로운 코멘트 생성시 새로고침으로 반영 + } else { + console.log("[ERROR] error while creating comment"); + } + } catch (e) { + console.log("Error!"); } }; @@ -108,7 +112,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("COMMENT DELETE SUCCESS"); + window.location.reload(); + } else { + console.log("[ERROR] error while deleting comment"); + } +}; 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..01f8f987 100644 --- a/src/components/Comment/CommentElement.jsx +++ b/src/components/Comment/CommentElement.jsx @@ -1,59 +1,88 @@ import { useState, useEffect } from "react"; +import { getCookie } from "../../utils/cookie"; +import { getUser, updateComment } from "../../apis/api"; +import { useNavigate } from "react-router-dom"; 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 - }); + 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; + + const handleEditComment = () => { + // add api call for editing comment + setContent(onChangeValue); + setIsEdit(!isEdit); + + const newComment = { + post: postId, + content: onChangeValue, }; + updateComment(comment.id, newComment); + }; + + useEffect(() => { + // add api call to check if user is the author of the comment + if (getCookie("access_token")) { + const getUserAPI = async () => { + const user = await getUser(); + setUser(user); + }; + getUserAPI(); + } + }, []); + + return ( +
{content}
+ )} + + + {year}.{month}.{day} + +{content}
- )} - - {year}.{month}.{day} -