-
Notifications
You must be signed in to change notification settings - Fork 2
Taehyeong week5 hw #43
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
TaehyeungKim
wants to merge
20
commits into
taehyeong-week5
Choose a base branch
from
taehyeong-week5-hw
base: taehyeong-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
20 commits
Select commit
Hold shift + click to select a range
3e57612
:art: tag auto-complete finished
TaehyeungKim 71339c8
:sprakled: added comment dummy data
TaehyeungKim 21d874c
:sparkles: added comment template
TaehyeungKim fd319f1
:sparkles: completed comment submit
TaehyeungKim 1d8023a
:art: added custom style to comment edit button
TaehyeungKim 45b5954
:art: finished comment edit
TaehyeungKim 4d57667
:zap: separated new input value from data in comment edit page
TaehyeungKim 08eeca0
:sparkles: finished week5-hw
TaehyeungKim fb7ea3a
:art: added selecting multiple tags in serach
TaehyeungKim d786c58
added the new comment data with time now
TaehyeungKim 40d622c
:bug: fixed the input of the post-create-page to full-width
TaehyeungKim 500e8ba
if the post is created or edited, the route to detail page
TaehyeungKim 0d5be85
:art: edited the button to shrink-0
TaehyeungKim fd5137f
:art: added responsive design to homepage
TaehyeungKim 339b555
:sparkles: added custom media query hook and implemented it to the heβ¦
TaehyeungKim 7e05854
:art: implemented Intl.DateTimeFormat for the comment created_at
TaehyeungKim 7aa88af
:bug: added proper key to individual comment component
TaehyeungKim b22d367
modified responsive target range in the small post
TaehyeungKim 828bb88
implemented join between comment and post id
TaehyeungKim 39360a2
:bug: fixed the error having been occured when adding first comment tβ¦
TaehyeungKim 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
84 changes: 84 additions & 0 deletions
84
lionblog-taehyeong/src/components/Comment/CommentElement.jsx
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,84 @@ | ||
| import { useState, useEffect } from "react"; | ||
|
|
||
| const CommentElement = ({ comment, handleCommentDelete }) => { | ||
| const [isEditMode, setIsEditMode] = useState(false); | ||
| const [content, setContent] = useState(comment.content); | ||
| const [editInputValue, setEditInputValue] = useState(""); | ||
|
|
||
| // comment created_at μ μ²λ¦¬ | ||
| const date = new Intl.DateTimeFormat("en-CA", { | ||
| formatString: "yyyy.mm.dd", | ||
|
|
||
| year: "numeric", | ||
| month: "2-digit", | ||
| day: "2-digit", | ||
| }) | ||
| .format(new Date(comment.created_at)) | ||
| .replaceAll("-", "."); | ||
|
|
||
| const handleEditComment = (e) => { | ||
| e.preventDefault(); | ||
| setContent(editInputValue); | ||
| setIsEditMode(false); | ||
| setEditInputValue(""); | ||
| }; | ||
|
|
||
| const toEditMode = (e) => { | ||
| e.preventDefault(); | ||
| setIsEditMode(true); | ||
| }; | ||
|
|
||
| const toShowMode = (e) => { | ||
| e.preventDefault(); | ||
| setIsEditMode(false); | ||
| }; | ||
|
|
||
| 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"> | ||
| {isEditMode ? ( | ||
| <input | ||
| className="input" | ||
| defaultValue={content} | ||
| onChange={(e) => setEditInputValue(e.target.value)} | ||
| /> | ||
| ) : ( | ||
| <p>{content}</p> | ||
| )} | ||
|
|
||
| <span className="text-base text-gray-300">{date}</span> | ||
| </div> | ||
|
|
||
| <div className="flex flex-row items-center gap-3"> | ||
| {isEditMode ? ( | ||
| <> | ||
| <button className="button-no-bg" onClick={toShowMode}> | ||
| μ·¨μ | ||
| </button> | ||
| <button className="button-no-bg" onClick={handleEditComment}> | ||
| μλ£{" "} | ||
| </button> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <button | ||
| className="button-no-bg" | ||
| type="button" | ||
| onClick={handleCommentDelete(comment.id)} | ||
| > | ||
| μμ | ||
| </button> | ||
| <button className="button-no-bg" type="button" onClick={toEditMode}> | ||
| μμ | ||
| </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,83 @@ | ||
| import { useState, useEffect } from "react"; | ||
| import comments from "../../data/comment"; // dummy data | ||
| import CommentElement from "./CommentElement"; | ||
|
|
||
| const Comment = ({ postId }) => { | ||
| const [commentsData, setCommentsData] = useState([]); | ||
| const [commentInputValue, setCommentInputValue] = useState(""); | ||
|
|
||
| const handleCommentInputChange = (e) => { | ||
| const { value } = e.target; | ||
| setCommentInputValue(value); | ||
| }; | ||
|
|
||
| const handleCommentSubmit = (e) => { | ||
| e.preventDefault(); | ||
| if (commentInputValue.length === 0) return; | ||
|
|
||
| const newId = | ||
| commentsData.length > 0 | ||
| ? commentsData.sort((a, b) => a.id - b.id)[commentsData.length - 1].id + | ||
| 1 | ||
| : 1; | ||
|
|
||
| setCommentsData([ | ||
| ...commentsData, | ||
| { | ||
| id: newId, | ||
| content: commentInputValue, | ||
| created_at: new Date(), | ||
| post: 1, | ||
| author: { | ||
| id: 2, | ||
| username: "user2", | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| setCommentInputValue(""); | ||
| }; | ||
|
|
||
| const handleCommentDelete = (commentId) => (e) => { | ||
| e.preventDefault(); | ||
| const updatedComments = commentsData.filter( | ||
| (comment) => comment.id !== commentId | ||
| ); | ||
| setCommentsData([...updatedComments]); | ||
| console.log(commentId); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| setCommentsData( | ||
| comments.filter((comment) => comment.post === parseInt(postId)) | ||
| ); | ||
| }, [postId]); | ||
|
|
||
| return ( | ||
| <div className="w-full mt-5 self-start mb-8"> | ||
| <h1 className="text-3xl font-bold my-5">Comments</h1> | ||
| {commentsData && | ||
| commentsData.map((comment) => ( | ||
| <CommentElement | ||
| key={comment.id} | ||
| comment={comment} | ||
| handleCommentDelete={handleCommentDelete} | ||
| /> | ||
| ))} | ||
| <form className="flex flex-row items-center justify-center mt-10 gap-2"> | ||
| <input | ||
| type="text" | ||
| className="input" | ||
| placeholder="λκΈμ μ λ ₯ν΄μ£ΌμΈμ." | ||
| onChange={handleCommentInputChange} | ||
| value={commentInputValue} | ||
| /> | ||
| <button className="button" type="submit" onClick={handleCommentSubmit}> | ||
| μμ± | ||
| </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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { useState, useEffect, useCallback } from "react"; | ||
|
|
||
| const getQueryMatches = (query) => { | ||
| if (typeof window !== "undefined") return window.matchMedia(query).matches; | ||
| return false; | ||
| }; | ||
|
|
||
| export const useMediaQuery = (query) => { | ||
| const [matches, setMatches] = useState(getQueryMatches(query)); | ||
|
|
||
| const handleChange = useCallback(() => { | ||
| setMatches(getQueryMatches(query)); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const matchMedia = window.matchMedia(query); | ||
|
|
||
| matchMedia.addEventListener("change", handleChange); | ||
|
|
||
| return () => matchMedia.removeEventListener("change", handleChange); | ||
| }, [query]); | ||
|
|
||
| return matches; | ||
| }; |
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.
μ€ λ€ν¬λͺ¨λ μ ν κΈ°λ₯μ λ§λ 건κ°μ©..?