Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/apis/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@ 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");
} else {
console.log("[ERROR] error while deleting comment");
}
};

export const getUser = async () => {
const response = await instanceWithToken.get("/account/info/");
Expand Down
4 changes: 3 additions & 1 deletion src/apis/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ instanceWithToken.interceptors.response.use(
(error) => {
// ์„œ๋ฒ„๊ฐ€ ์˜ค๋ฅ˜๋ฅผ ์‘๋‹ตํ–ˆ์„ ๋•Œ ์ฒ˜๋ฆฌ - ์ฝ˜์†” ์ฐ์–ด์ฃผ๊ณ , ํ”„๋ก ํŠธ์—๊ฒŒ ๋ณด๋‚ด์ง€ ์•Š๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ด
console.log("Response Error!!");
return Promise.reject(error);
//return Promise.reject(error);
alert(`${error}`);
return error;
}
);
128 changes: 74 additions & 54 deletions src/components/Comment/CommentElement.jsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,79 @@
import { useState, useEffect } from "react";
import { getUser, updateComment } 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 (
<div className="w-full flex flex-row justify-between items-center mb-5">
<div className="w-3/4 flex flex-col gap-1">
{isEdit ? (
<input className="input mb-2" value={onChangeValue} onChange={(e) => setOnChangeValue(e.target.value)} />
) : (
<p className="text-lg">{content}</p>
)}

<span className="text-base text-gray-300">{year}.{month}.{day}</span>
</div>

<div className="flex flex-row items-center gap-3">
{isEdit ? (
<>
<button onClick={() => { setIsEdit(!isEdit); setOnChangeValue(content); }}>์ทจ์†Œ</button>
<button onClick={handleEditComment}>์™„๋ฃŒ</button>
</>
) : (
<>
<button onClick={() => handleCommentDelete(comment.id)}>์‚ญ์ œ</button>
<button onClick={() => setIsEdit(!isEdit)}>์ˆ˜์ •</button>
</>
)}
</div>
</div>
);
const { comment, handleCommentDelete, postId } = props;
const [isEdit, setIsEdit] = useState(false);
const [user, setUser] = useState();

const [onChangeValue, setOnChangeValue] = useState(comment.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(() => {
// 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();
}
}, []);

const handleEditComment = () => {
// add api call for editing comment
updateComment(comment.id, { content: onChangeValue });
};

return (
<div className="w-full flex flex-row justify-between items-center mb-5">
<div className="w-3/4 flex flex-col gap-1">
{isEdit ? (
<input
className="input mb-2"
value={onChangeValue}
onChange={(e) => setOnChangeValue(e.target.value)}
/>
) : (
<p className="text-lg">{comment.content}</p>
)}

<span className="text-base text-gray-300">
{year}.{month}.{day}
</span>
</div>

<div className="flex flex-row items-center gap-3">
{isEdit ? (
<>
<button
onClick={() => {
setIsEdit(!isEdit);
setOnChangeValue(comment.content);
}}
>
์ทจ์†Œ
</button>
<button onClick={handleEditComment}>์™„๋ฃŒ</button>
</>
) : user?.id === comment?.author ? (
<>
<button onClick={() => handleCommentDelete(comment.id)}>
์‚ญ์ œ
</button>
<button onClick={() => setIsEdit(!isEdit)}>์ˆ˜์ •</button>
</>
) : null}
</div>
</div>
);
};
export default CommentElement;
118 changes: 74 additions & 44 deletions src/components/Comment/index.jsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,83 @@
import { useState } from "react";
import comments from "../../data/comments"; // dummy data
import { useEffect, useState } from "react";
//import comments from "../../data/comments"; // dummy data
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: "",
});
const [isUserLoggedIn, setIsUserLoggedIn] = useState(false); // ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ์ƒํƒœ, ์šฐ์„  false๋กœ ์ดˆ๊ธฐํ™”

const handleCommentDelete = (commentId) => {
console.log("comment: ", commentId);
setCommentList(commentList.filter((comment) => comment.id !== commentId)); // TODO: add api call for deleting comment
useEffect(() => {
const getCommentsAPI = async () => {
const comments = await getComments(postId);
setCommentList(comments);
};
getCommentsAPI();

// ๋กœ๊ทธ์ธ ์—ฌ๋ถ€ ํ™•์ธ
const loggedIn = getCookie("access_token") ? true : false;
setIsUserLoggedIn(loggedIn);
}, []);

const handleChange = (e) => {
setComment({ ...comment, content: e.target.value });
};

const handleCommentSubmit = (e) => {
e.preventDefault();
createComment(comment);
};

const handleCommentDelete = (commentId) => {
const confirmDelete = window.confirm("์ •๋ง ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
if (!confirmDelete) return;
try {
deleteComment(commentId);
} catch (error) {
console.error(error);
}
window.location.reload();
};

return (
<div className="w-full mt-5 self-start">
<h1 className="text-3xl font-bold my-5">Comments</h1>
{commentList.map((comment) => {
return (
<CommentElement
key={comment.id}
comment={comment}
handleCommentDelete={handleCommentDelete}
postId={postId}
/>
);
})}

return (
<div className="w-full mt-5 self-start">
<h1 className="text-3xl font-bold my-5">Comments</h1>
{commentList.map((comment) => {
return (
<CommentElement key={comment.id} comment={comment} handleCommentDelete={handleCommentDelete} postId={postId} />
);
})}

<form className="flex flex-row mt-10 gap-3" onSubmit={handleCommentSubmit}>
<input type="text" value={newContent} placeholder="๋Œ“๊ธ€์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”" className="input" style={{ width: "calc(100% - 100px)" }} onChange={(e) => setNewContent(e.target.value)} />
<button type="submit" className="button">์ž‘์„ฑ</button>
</form>
</div>
);
{isUserLoggedIn ? (
<form
className="flex flex-row mt-10 gap-3"
onSubmit={handleCommentSubmit}
>
<input
type="text"
value={comment.content}
placeholder="๋Œ“๊ธ€์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
className="input"
style={{ width: "calc(100% - 100px)" }}
onChange={handleChange}
/>
<button type="submit" className="button">
์ž‘์„ฑ
</button>
</form>
) : null}
</div>
);
};

export default Comment;
8 changes: 0 additions & 8 deletions src/routes/HomePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ const HomePage = () => {
const [searchTags, setSearchTags] = useState([]);
const [searchValue, setSearchValue] = useState("");

useEffect(() => {
const getPostsAPI = async () => {
const posts = await getPosts();
setPostList(posts);
};
getPostsAPI();
}, []);

useEffect(() => {
const getPostsAPI = async () => {
const posts = await getPosts();
Expand Down