diff --git a/src/apis/api.js b/src/apis/api.js
index e8256722..9d2068dd 100644
--- a/src/apis/api.js
+++ b/src/apis/api.js
@@ -1,6 +1,6 @@
import { instance, instanceWithToken } from "./axios";
-import axios from "axios";
+// Account 관련 API들
export const signIn = async (data) => {
const response = await instance.post("/account/signin/", data);
if (response.status === 200) {
@@ -20,6 +20,17 @@ export const signUp = async (data) => {
return response;
};
+export const getUser = async () => {
+ const response = await instanceWithToken.get("/account/info/");
+ if (response.status === 200) {
+ console.log("GET USER SUCCESS");
+ } else {
+ console.log("[ERROR] error while updating comment");
+ }
+ return response.data;
+};
+
+// 추가
export const getPosts = async () => {
const response = await instance.get("/post/");
return response.data;
@@ -64,6 +75,7 @@ export const deletePost = async (id, navigate) => {
// 과제!!
export const likePost = async (postId) => {};
+// Tag 관련 API들
export const getTags = async () => {
const response = await instance.get("/tag/");
return response.data;
@@ -78,6 +90,10 @@ export const createTag = async (data) => {
}
return response; // response 받아서 그 다음 처리
};
+
+// 추가
+
+// Comment 관련 API들
export const getComments = async (postId) => {
const response = await instance.get(`/comment/?post=${postId}`);
return response.data;
@@ -102,13 +118,14 @@ export const updateComment = async (id, data) => {
console.log("[ERROR] error while updating comment");
}
};
-export const deleteComment = async (id) => {};
-export const getUser = async () => {
- const response = await instanceWithToken.get("/account/info/");
- if (response.status === 200) {
- console.log("GET USER SUCCESS");
+
+// 과제 !!
+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 updating comment");
+ console.log("[ERROR] error while deleting post");
}
- return response.data;
};
diff --git a/src/apis/axios.js b/src/apis/axios.js
index 2e85164e..a50557e0 100644
--- a/src/apis/axios.js
+++ b/src/apis/axios.js
@@ -13,6 +13,8 @@ export const instance = axios.create();
// Token 있어야 접근 가능한 API들 - 얘는 토큰을 넣어줘야 해요
export const instanceWithToken = axios.create();
+// ⬇️ 추가
+// instanceWithToken에는 쿠키에서 토큰을 찾고 담아줍시다!
instanceWithToken.interceptors.request.use(
// 요청을 보내기전 수행할 일
// 사실상 이번 세미나에 사용할 부분은 이거밖에 없어요
diff --git a/src/components/Comment/CommentElement.jsx b/src/components/Comment/CommentElement.jsx
index fd48385e..fa425a14 100644
--- a/src/components/Comment/CommentElement.jsx
+++ b/src/components/Comment/CommentElement.jsx
@@ -1,59 +1,83 @@
import { useState, useEffect } from "react";
+import { updateComment, 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(null); // state for user
+
+ 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 = async () => {
+ await updateComment(comment.id, { content: onChangeValue });
+ setIsEdit(!isEdit);
+ };
+
+ // get user info
+ useEffect(() => {
+ if (getCookie("access_token")) {
+ const getUserAPI = async () => {
+ const user = await getUser();
+ setUser(user);
+ };
+ getUserAPI();
+ }
+ }, []);
+
+ return (
+
+
+ {isEdit ? (
+
setOnChangeValue(e.target.value)}
+ />
+ ) : (
+
{content}
+ )}
+
+
+ {year}.{month}.{day}
+
+
+
+
+ {user?.id === comment.author.id ? (
+ isEdit ? (
+ <>
+
+
+ >
+ ) : (
+ <>
+
+
+ >
+ )
+ ) : null}
+ {}
+
+
+ );
};
export default CommentElement;
diff --git a/src/components/Comment/index.jsx b/src/components/Comment/index.jsx
index c8c4a27e..8da95de7 100644
--- a/src/components/Comment/index.jsx
+++ b/src/components/Comment/index.jsx
@@ -1,53 +1,76 @@
-import { useState } from "react";
-import comments from "../../data/comments"; // dummy data
+import { useState, useEffect } from "react";
+import { getComments, createComment, deleteComment } from "../../apis/api"; // dummy data
import CommentElement from "./CommentElement";
+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 [commentList, setCommentList] = useState([]); // state for comments
+ const [newContent, setNewContent] = useState({
+ post: postId,
+ content: "",
+ });
+ const [isSignedIn, setIsSignedIn] = useState();
- 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);
};
+ getCommentsAPI();
+ const signIn = getCookie("access_token") ? true : false;
+ setIsSignedIn(signIn);
+ }, []);
- const handleCommentDelete = (commentId) => {
- console.log("comment: ", commentId);
- setCommentList(commentList.filter((comment) => comment.id !== commentId)); // TODO: add api call for deleting comment
- };
+ const handleCommentSubmit = async (e) => {
+ e.preventDefault();
+ await createComment({ post: postId, content: newContent });
+ setNewContent("");
+ };
+
+ const handleCommentDelete = async (commentId) => {
+ const confirmDelete = window.confirm("정말 삭제하시겠습니까?");
+ if (!confirmDelete) return;
+ try {
+ await deleteComment(commentId);
+ } catch (error) {
+ console.error(error);
+ } // TODO: add api call for deleting comment
+ };
+
+ return (
+
+
Comments
+ {commentList.map((comment) => {
+ return (
+
+ );
+ })}
- return (
-
-
Comments
- {commentList.map((comment) => {
- return (
-
- );
- })}
-
-
-
- );
+ {isSignedIn ? (
+
+ ) : null}
+
+ );
};
export default Comment;
diff --git a/src/routes/PostDetailPage.jsx b/src/routes/PostDetailPage.jsx
index 76e77169..ca30185b 100644
--- a/src/routes/PostDetailPage.jsx
+++ b/src/routes/PostDetailPage.jsx
@@ -2,29 +2,30 @@ import { useState, useEffect } from "react";
import { useParams, Link, useNavigate } from "react-router-dom";
import { BigPost } from "../components/Posts";
import Comment from "../components/Comment";
+
import { getPost, getUser, deletePost } from "../apis/api";
import { getCookie } from "../utils/cookie";
-import posts from "../data/posts";
-
const PostDetailPage = () => {
const { postId } = useParams();
const [post, setPost] = useState(null);
- const [user, setUser] = useState();
+
+ const [user, setUser] = useState(null);
+
useEffect(() => {
const getPostAPI = async () => {
- const posts = await getPost(postId);
- setPost(posts);
+ const post = await getPost(postId);
+ setPost(post);
};
getPostAPI();
}, [postId]);
+
useEffect(() => {
// access_token이 있으면 유저 정보 가져옴
if (getCookie("access_token")) {
const getUserAPI = async () => {
const user = await getUser();
setUser(user);
- console.log(user);
};
getUserAPI();
}
@@ -34,6 +35,7 @@ const PostDetailPage = () => {
const onClickDelete = async () => {
const confirmDelete = window.confirm("정말 삭제하시겠습니까?");
if (!confirmDelete) return;
+
try {
await deletePost(postId, navigate);
} catch (error) {