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

{content}

좋아요 {likeUsers.length} diff --git a/src/components/posts/PostForm.jsx b/src/components/posts/PostForm.jsx index 36a2513..4d73149 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"; export const PostForm = () => { + const { posts, currentUser } = globalStore.getState(); + + const handleClick = () => { + globalStore.setState({ + posts: [ + ...posts, + { + id: posts.length + 1, + author: currentUser.username, + time: Date.now(), + content: document.getElementById("post-content").value, + likeUsers: [], + }, + ], + }); + }; + return (