-
Notifications
You must be signed in to change notification settings - Fork 2
[week5][이예은] 과제 제출 #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yeeunleel
wants to merge
2
commits into
yeeun-week5
Choose a base branch
from
yeeun-week5-hw
base: yeeun-week5
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { useState, useEffect } from "react"; | ||
|
|
||
| const CommentElement = ({ comment, handleCommentDelete, handleCommentEdit }) => { | ||
|
|
||
| /* TODO: props 받기 | ||
| Hint: src/components/Comment/index.jsx에서 어떠한 props를 넘겨주는지 확인해보세요! */ | ||
|
|
||
| /* TODO: 댓글을 수정하는 input의 value를 관리하기 위한 state 작성 | ||
| Hint: 댓글의 내용을 저장하는 state와 수정 중인지 여부를 저장하는 state를 따로 만드는 게 좋겠죠? */ | ||
| const [isEditing, setIsEditing] = useState(false); | ||
| const [editedComment, setEditedComment] = useState(comment.content); | ||
|
|
||
| // 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 | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div className="w-full flex flex-row justify-between items-center mb-5"> | ||
| <div className="w-3/4 flex flex-col gap-1"> | ||
| {/* // TODO: 수정 중일 때와 아닐 때를 나눠서 보여줘야 해요! */} | ||
|
|
||
| {isEditing ? ( | ||
| <input | ||
| className="input" | ||
| onChange={(e) => setEditedComment(e.target.value)} | ||
| value={editedComment} | ||
| ></input> | ||
| ) : ( | ||
| <p>{comment.content}</p> | ||
| )} | ||
| {/* // 날짜 */} | ||
| <span className="text-base text-gray-300"> | ||
| {year}.{month}.{day} | ||
| </span> | ||
| </div> | ||
|
|
||
| <div className="flex flex-row items-center gap-3"> | ||
| {isEditing ? ( | ||
| <> | ||
| <button | ||
| onClick={() => { | ||
| setIsEditing(false); | ||
| }} | ||
| > | ||
| 취소 | ||
| </button> | ||
| <button | ||
| onClick={() => { | ||
| setIsEditing(false); | ||
| handleCommentEdit(comment.id, editedComment); | ||
| }} | ||
| > | ||
| 완료 | ||
| </button> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <button | ||
| onClick={() => { | ||
| setIsEditing(true); | ||
| }} | ||
| > | ||
| 수정 | ||
| </button> | ||
| <button | ||
| onClick={() => { | ||
| setEditedComment([]); | ||
| handleCommentDelete(comment.id); | ||
| }} | ||
| > | ||
| 삭제 | ||
| </button> | ||
| </> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
| export default CommentElement; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { useState } from "react"; | ||
| import comments from "../../data/comments"; // dummy data | ||
| import CommentElement from "./CommentElement"; | ||
|
|
||
| const Comment = ({ postId }) => { | ||
| // TODO: comments를 저장하기 위한 state를 만들어주세요 | ||
| const [commentList, setCommentList] = useState(comments); | ||
| // TODO: 새로운 댓글을 추가하기 위한 state를 만들어주세요 | ||
| const [newComment, setNewComment] = useState([]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 새로운 댓글은 아무래도 string 형태의 데이터일 것이니, 초기화 시 빈 배열보다는 빈 문자열 ("")로 하는 것이 더 적합하지 않을까 생각해요! |
||
|
|
||
| const handleCommentSubmit = (e) => { | ||
| e.preventDefault(); | ||
| const newCommentObject = { | ||
| id: commentList.length + 1, | ||
| content: newComment, | ||
| created_at: Date(), | ||
| }; | ||
| setCommentList([...commentList, newCommentObject]); | ||
| setNewComment(""); | ||
| alert("댓글 작성"); // add api call for creating comment | ||
| }; | ||
|
|
||
| const handleCommentDelete = (commentId) => { | ||
| setCommentList(commentList.filter((comment) => comment.id !== commentId)); | ||
| alert("댓글 삭제"); // add api call for deleting comment | ||
| }; | ||
|
|
||
| const handleCommentEdit = (commentId, editedComment) => { | ||
| setCommentList((commentList) => { | ||
| return commentList.map((comment) => { | ||
| if (comment.id === commentId) { | ||
| return { ...comment, content: editedComment }; | ||
| } | ||
| return comment; | ||
| }); | ||
| }); | ||
| alert("댓글 수정"); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="w-full mt-5 self-start"> | ||
| <h1 className="text-3xl font-bold my-5">Comments</h1> | ||
| {commentList.map((comment) => ( | ||
| <CommentElement | ||
| comment={comment} | ||
| handleCommentDelete={handleCommentDelete} | ||
| handleCommentEdit={handleCommentEdit} | ||
| /> | ||
| ))} | ||
| <form | ||
| onSubmit={handleCommentSubmit} | ||
| className="flex flex-row items-center justify-center mt-10 gap-2" | ||
| > | ||
| <input | ||
| required | ||
| className="input" | ||
| value={newComment} | ||
| onChange={(e) => setNewComment(e.target.value)} | ||
| ></input> | ||
| <button type="submit" className="button w-24 h-12 py-10"> | ||
| 작성 | ||
| </button> | ||
| </form> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Comment; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // dummy data | ||
| const comments = [ | ||
| { | ||
| "id": 1, | ||
| "content": "새해 복 많이 받으세요^^", | ||
| "created_at": "2024-01-01T15:09:43Z", | ||
| "post": 1, | ||
| "author": { | ||
| "id": 2, | ||
| "username": "user2" | ||
| } | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "content": "축구 2대 0;;;", | ||
| "created_at": "2024-02-07T15:09:43Z", | ||
| "post": 1, | ||
| "author": { | ||
| "id": 3, | ||
| "username": "user3" | ||
| } | ||
| }, | ||
| { | ||
| "id": 3, | ||
| "content": "망할 개강이야...", | ||
| "created_at": "2024-03-02T15:09:43Z", | ||
| "post": 1, | ||
| "author": { | ||
| "id": 4, | ||
| "username": "user4" | ||
| } | ||
| }, | ||
| ] | ||
|
|
||
| export default comments; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
빈 리스트로 만들어주는 이유가 궁금해요