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
82 changes: 82 additions & 0 deletions src/components/Comment/CommentElement.jsx
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;
87 changes: 87 additions & 0 deletions src/components/Comment/index.jsx
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}
Comment on lines +52 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
key={comment.id}
id={comment.id}
content={comment.content}
createdAt={comment.created_at}
postId={comment.post}
author={comment.author}
handleCommentDelete={handleCommentDelete}
handleCommentEdit={handleCommentEdit}
comment={comment}
handleCommentDelete={handleCommentDelete}
handleCommentEdit={handleCommentEdit}

μž‘μ„±ν•΄μ€€ λ°©μ‹μœΌλ‘œ propsλ₯Ό λ„˜κ²¨μ€„ μˆ˜λ„ μžˆμ§€λ§Œ comment 객체λ₯Ό λ„˜κ²¨μ£ΌλŠ” 건 μ–΄λ–¨κΉŒμš”? 일일이 propsλ₯Ό 적기에도 번거둭고 CommentElementμ—μ„œ μ§€κΈˆ λͺ¨λ“  propsλ₯Ό ν™œμš©ν•˜κ³  μžˆμ§€ μ•ŠμœΌλ‹ˆκΉŒ comment 자체λ₯Ό λ„˜κ²¨μ£Όκ³  CommentElementμ—μ„œ ν•„μš”ν•œ λ‚΄μš©μ„ λ””μŠ€νŠΈλŸ­μ²˜λ§ ν• λ‹ΉμœΌλ‘œ ν™œμš©ν•˜λŠ” 것도 쒋을 것 κ°™μ•„μš”!

/>
))}

<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;
30 changes: 24 additions & 6 deletions src/components/Header/index.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { useState, useEffect } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
);
Expand Down
17 changes: 15 additions & 2 deletions src/components/Posts/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Link } from "react-router-dom";

export const SmallPost = ({ post }) => {
const onClickLike = (e) => {
e.preventDefault();
alert("λ‚˜λ„ μ’‹μ•„!");
// add api call for liking post here
};

return (
<Link
to={`/${post.id}`}
Expand All @@ -15,14 +21,18 @@ export const SmallPost = ({ post }) => {
</span>
))}
</div>
<span className="cursor-pointer">
<span onClick={onClickLike} className="cursor-pointer">
{post.like_users.length > 0 && `❀️ ${post.like_users.length}`}
</span>
</Link>
);
};

export const BigPost = ({ post }) => {
const onClickLike = () => {
alert("λ‚˜λ„ μ’‹μ•„!");
// add api call for liking post here
};
return (
<div className="flex flex-col px-8 py-5 w-full bg-orange-400 ring-4 ring-orange-300 rounded-xl gap-5">
<div className="flex flex-row items-center justify-between gap-3">
Expand All @@ -44,7 +54,10 @@ export const BigPost = ({ post }) => {
</span>
))}
</div>
<span className="flex flex-row text-black cursor-pointer">
<span
onClick={onClickLike}
className="flex flex-row text-black cursor-pointer"
>
❀️ {post.like_users.length > 0 ? post.like_users.length : "0"}
</span>
</div>
Expand Down
35 changes: 35 additions & 0 deletions src/data/comments.js
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;
49 changes: 47 additions & 2 deletions src/routes/HomePage.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import { useState, useEffect } from "react";
import { SmallPost } from "../components/Posts";
import { Link } from "react-router-dom";
import posts from "../data/posts";

const HomePage = () => {
const postList = posts;
const [postList, setPostList] = useState(posts);
const [tags, setTags] = useState([]);
const [searchTags, setSearchTags] = useState([]);
const [searchValue, setSearchValue] = useState("");

const handleChange = (e) => {};
useEffect(() => {
const tagList = posts.reduce((acc, post) => {
for (let tag of post.tags) {
acc.add(tag.content);
}
return acc;
}, new Set());
setTags([...tagList]);
setSearchTags([...tagList]);
}, []);
const handleTagFilter = (e) => {
const { innerText } = e.target;
if (searchValue === innerText.substring(1)) {
setSearchValue("");
setPostList(posts);
} else {
const activeTag = innerText.substring(1);
setSearchValue(activeTag);
const newPosts = posts.filter((post) =>
post.tags.find((tag) => tag.content === activeTag)
);
setPostList(newPosts);
}
};
const handleChange = (e) => {
const { value } = e.target;
const newTags = tags.filter((tag) => tag.includes(value));
setSearchTags(newTags);
};

return (
<div>
Expand All @@ -20,6 +52,19 @@ const HomePage = () => {
className="border border-orange-400 outline-none rounded-2xl text-center py-2 px-20 text-orange-400 bg-transparent"
/>
</div>
<div className="flex mt-5 justify-center">
{searchTags.map((tag) => {
return (
<button
key={tag}
className={tag === searchValue ? "tag active mr-2" : "tag mr-2"}
onClick={handleTagFilter}
>
#{tag}
</button>
);
})}
</div>
<div className="grid grid-cols-3 px-10 mt-10">
{postList.map((post) => (
<SmallPost key={post.id} post={post} />
Expand Down
Loading