-
Notifications
You must be signed in to change notification settings - Fork 2
Yeonghun week5 hw #27
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
Yeonghunj
wants to merge
2
commits into
main
Choose a base branch
from
yeonghun-week5-hw
base: main
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,82 @@ | ||
| import React, { useState } from "react"; | ||
|
|
||
| const CommentElement = ({ | ||
| id, | ||
| content, | ||
| createdAt, | ||
| handleCommentDelete, | ||
| handleCommentEdit, | ||
| }) => { | ||
| const [isEditing, setIsEditing] = useState(false); | ||
| const [editedContent, setEditedContent] = useState(content); | ||
|
|
||
| const date = new Date(createdAt); | ||
| 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 = () => { | ||
| if (editedContent.trim() !== "") { | ||
| handleCommentEdit(id, editedContent); | ||
| setIsEditing(false); | ||
| alert("λκΈ μμ μλ£"); | ||
| } else { | ||
| alert("λκΈ λ΄μ©μ μ λ ₯ν΄μ£ΌμΈμ."); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="w-full mb-5"> | ||
| <div className="flex justify-between items-center"> | ||
| {isEditing ? ( | ||
| <input | ||
| type="text" | ||
| value={editedContent} | ||
| onChange={(e) => setEditedContent(e.target.value)} | ||
| className="flex-grow border p-2 rounded mr-2 text-black" | ||
| /> | ||
| ) : ( | ||
| <div className="flex-grow"> | ||
| <p className="text-white">{content}</p> | ||
| </div> | ||
| )} | ||
|
|
||
| {isEditing ? ( | ||
| <> | ||
| <button | ||
| onClick={() => setIsEditing(false)} | ||
| className="text-red-500 mx-1 p-2" | ||
| > | ||
| μ·¨μ | ||
| </button> | ||
| <button onClick={handleEditComment} className="text-white mx-1 p-2"> | ||
| μλ£ | ||
| </button> | ||
| </> | ||
| ) : ( | ||
| <div> | ||
| <button | ||
| onClick={() => setIsEditing(true)} | ||
| className="text-white mx-1 p-2" | ||
| > | ||
| μμ | ||
| </button> | ||
| <button | ||
| onClick={() => handleCommentDelete(id)} | ||
| className="text-white mx-1 p-2" | ||
| > | ||
| μμ | ||
| </button> | ||
| </div> | ||
| )} | ||
| </div> | ||
| <div className="text-gray-300 text-sm mt-2"> | ||
| {year}.{month}.{day} | ||
| </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,87 @@ | ||
| import React, { useState } from "react"; | ||
| import commentsData from "../../data/comments"; | ||
| import CommentElement from "./CommentElement"; | ||
|
|
||
| const Comment = ({ postId }) => { | ||
| // λκΈλ€μ μ μ₯νκΈ° μν state | ||
| const [comments, setComments] = useState(commentsData); | ||
| // μλ‘μ΄ λκΈμ μΆκ°νκΈ° μν state | ||
| const [newComment, setNewComment] = useState(""); | ||
|
|
||
| const handleCommentSubmit = (e) => { | ||
| e.preventDefault(); | ||
| // νμ¬ λ μ§μ μκ°μ ν¬ν¨νλ μ λκΈ κ°μ²΄ μμ± | ||
| const now = new Date(); | ||
| const createdAt = now.toISOString().split("T")[0]; | ||
|
|
||
| const commentToAdd = { | ||
| id: comments.length + 1, | ||
| content: newComment, | ||
| created_at: createdAt, | ||
| author: { id: "1", username: "currentUser" }, | ||
| }; | ||
| setComments([...comments, commentToAdd]); | ||
| setNewComment(""); | ||
| alert("λκΈ μμ±"); | ||
| }; | ||
|
|
||
| const handleCommentEdit = (id, editedContent) => { | ||
| setComments( | ||
| comments.map((comment) => { | ||
| if (comment.id === id) { | ||
| return { ...comment, content: editedContent }; | ||
| } | ||
| return comment; | ||
| }) | ||
| ); | ||
| }; | ||
|
|
||
| const handleCommentDelete = (commentId) => { | ||
| const updatedComments = comments.filter( | ||
| (comment) => comment.id !== commentId | ||
| ); | ||
| setComments(updatedComments); | ||
| alert("λκΈ μμ "); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="w-full mt-5 self-start"> | ||
| <h1 className="text-3xl font-bold my-5">Comments</h1> | ||
| {comments.map((comment) => ( | ||
| <CommentElement | ||
| key={comment.id} | ||
| id={comment.id} | ||
| content={comment.content} | ||
| createdAt={comment.created_at} | ||
| postId={comment.post} | ||
| author={comment.author} | ||
| handleCommentDelete={handleCommentDelete} | ||
| handleCommentEdit={handleCommentEdit} | ||
| /> | ||
| ))} | ||
|
|
||
| <form | ||
| className="flex flex-row items-center justify-center mt-10 gap-2" | ||
| onSubmit={handleCommentSubmit} | ||
| > | ||
| <div className="flex w-full gap-2"> | ||
| <input | ||
| type="text" | ||
| placeholder="λκΈμ μ λ ₯ν΄μ£ΌμΈμ" | ||
| value={newComment} | ||
| onChange={(e) => setNewComment(e.target.value)} | ||
| className="border p-2 rounded flex-grow text-black" | ||
| /> | ||
| <button | ||
| type="submit" | ||
| className="bg-orange-400 text-white p-2 rounded" | ||
| > | ||
| μμ± | ||
| </button> | ||
| </div> | ||
| </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 |
|---|---|---|
| @@ -1,21 +1,39 @@ | ||
| import { useState, useEffect } from "react"; | ||
|
Contributor
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. κ³Όμ PRμλ κ³Όμ λ΄μ©λ§ λ΄κΈ°λλ‘ ν΄μ£ΌμΈμ! μΈλ―Έλμμ νλ μ½λ μμ± λ΄μ©μ μ΄λ¦ λΈλμΉμ νΈμ¬ν΄λκ³ κ³Όμ λΈλμΉλ‘ λ°κΏμ κ³Όμ μ½λ μμ±νμλ©΄ λ©λλΉ~ |
||
| import { Link } from "react-router-dom"; | ||
|
|
||
| import lion from "../../assets/images/lion.jpeg"; | ||
|
|
||
| const Header = () => { | ||
| const [isUserLoggedIn, setIsUserLoggedIn] = useState(false); // λ‘κ·ΈμΈ μ¬λΆ μν, μ°μ falseλ‘ μ΄κΈ°ν | ||
|
|
||
| useEffect(() => { | ||
| // TODO: μ΄ν api μ°κ²° μ μ ν¨ν tokenμ΄ μλ€λ©΄ setIsUserLoggedIn(true)λ‘ μν λ³κ²½νλ μ½λ μμ± | ||
| }, []); | ||
|
|
||
| const handleSignOut = () => { | ||
| // TODO: μ΄ν api μ°κ²° μ token μ κ±° | ||
| }; | ||
| return ( | ||
| <div className="flex items-center justify-between w-full gap-5 bg-black px-5 py-2.5 h-20"> | ||
| <Link to="/" className="flex flex-row items-center gap-5"> | ||
| <img src={lion} alt="lion" className="max-h-16 rounded-full" /> | ||
| <div className="text-white text-xl">SNULION BLOG</div> | ||
| </Link> | ||
| <div className="flex"> | ||
| <Link to="/signin" className="mr-10 p-3 uppercase text-lg"> | ||
| sign in | ||
| </Link> | ||
| <Link to="/signup" className="mr-10 p-3 uppercase text-lg"> | ||
| sign up | ||
| </Link> | ||
| {isUserLoggedIn ? ( | ||
| <Link to="/" className="mr-10 p-3 uppercase text-lg"> | ||
| sign out | ||
| </Link> | ||
| ) : ( | ||
| <> | ||
| <Link to="/signin" className="mr-10 p-3 uppercase text-lg"> | ||
| sign in | ||
| </Link> | ||
| <Link to="/signup" className="mr-10 p-3 uppercase text-lg"> | ||
| sign up | ||
| </Link> | ||
| </> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
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
| 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
Oops, something went wrong.
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.
μμ±ν΄μ€ λ°©μμΌλ‘ propsλ₯Ό λκ²¨μ€ μλ μμ§λ§
commentκ°μ²΄λ₯Ό λ겨주λ 건 μ΄λ¨κΉμ? μΌμΌμ΄ propsλ₯Ό μ κΈ°μλ λ²κ±°λ‘κ³CommentElementμμ μ§κΈ λͺ¨λ propsλ₯Ό νμ©νκ³ μμ§ μμΌλκΉcommentμ체λ₯Ό λ겨주κ³CommentElementμμ νμν λ΄μ©μ λμ€νΈλμ²λ§ ν λΉμΌλ‘ νμ©νλ κ²λ μ’μ κ² κ°μμ!