diff --git a/src/components/posts/Post.jsx b/src/components/posts/Post.jsx index 67af756..d3d5e38 100644 --- a/src/components/posts/Post.jsx +++ b/src/components/posts/Post.jsx @@ -1,14 +1,31 @@ /** @jsx createVNode */ import { createVNode } from "../../lib"; import { toTimeFormat } from "../../utils/index.js"; +import { globalStore } from "../../stores/index.js"; + +export const Post = ({ author, time, content, likeUsers }) => { + const { loggedIn, currentUser, posts } = globalStore.getState(); + const activationLike = likeUsers.includes(currentUser?.username); + const handleLike = () => { + if (!currentUser) { + alert("로그인 후 이용해주세요"); + return; + } + + const newPosts = posts.map((post) => { + if (post.author === author && post.time === time) { + return { + ...post, + likeUsers: activationLike + ? post.likeUsers.filter((user) => user !== currentUser.username) + : [...post.likeUsers, currentUser?.username], + }; + } + return post; + }); + globalStore.setState({ posts: newPosts }); + }; -export const Post = ({ - author, - time, - content, - likeUsers, - activationLike = false, -}) => { return (
@@ -20,6 +37,7 @@ export const Post = ({

{content}

좋아요 {likeUsers.length} diff --git a/src/components/posts/PostForm.jsx b/src/components/posts/PostForm.jsx index 36a2513..45d7abb 100644 --- a/src/components/posts/PostForm.jsx +++ b/src/components/posts/PostForm.jsx @@ -1,7 +1,25 @@ /** @jsx createVNode */ import { createVNode } from "../../lib"; +import { globalStore } from "../../stores/index.js"; export const PostForm = () => { + const handleSubmit = () => { + const content = document.getElementById("post-content").value; + if (!content) { + alert("내용을 입력해주세요."); + return; + } + const { posts, currentUser } = globalStore.getState(); + const newPost = { + id: posts.length + 1, + author: currentUser?.username, + time: Date.now(), + content, + likeUsers: [], + }; + + globalStore.setState({ posts: [...posts, newPost] }); + }; return (