From e5a9f71fed0028ace13b4ebab7b97e45d634f9a7 Mon Sep 17 00:00:00 2001 From: keyonnaise Date: Wed, 25 Dec 2024 21:52:42 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=85=20=EB=AA=A8=EB=93=A0=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=ED=86=B5=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eslint.config.js | 1 + src/__tests__/chapter1-2/basic.test.jsx | 2 +- src/components/posts/Post.jsx | 36 +++++++-- src/components/posts/PostForm.jsx | 23 ++++++ src/lib/createElement.js | 57 ++++++++++++- src/lib/createObserver.js | 2 +- src/lib/createStore.js | 7 +- src/lib/createVNode.js | 11 ++- src/lib/eventManager.js | 103 +++++++++++++++++++++++- src/lib/helpers.js | 9 +++ src/lib/normalizeVNode.js | 24 ++++++ src/lib/renderElement.js | 19 ++++- src/lib/updateElement.js | 85 ++++++++++++++++++- src/pages/HomePage.jsx | 12 ++- src/pages/LoginPage.jsx | 2 + src/render.jsx | 2 + 16 files changed, 368 insertions(+), 27 deletions(-) create mode 100644 src/lib/helpers.js diff --git a/eslint.config.js b/eslint.config.js index 9e887bc..da7af3e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -6,6 +6,7 @@ import eslintPluginPrettier from "eslint-plugin-prettier/recommended"; /** @type {import('eslint').Linter.Config[]} */ export default [ { languageOptions: { globals: { ...globals.browser, ...globals.node } } }, + { rules: { "prettier/prettier": ["error", { endOfLine: "auto" }] } }, pluginJs.configs.recommended, eslintPluginPrettier, eslintConfigPrettier, diff --git a/src/__tests__/chapter1-2/basic.test.jsx b/src/__tests__/chapter1-2/basic.test.jsx index 60f7572..edf456e 100644 --- a/src/__tests__/chapter1-2/basic.test.jsx +++ b/src/__tests__/chapter1-2/basic.test.jsx @@ -598,7 +598,7 @@ describe("Chapter1-2 > 기본과제 > 가상돔 만들기 > ", () => { const clickHandler = vi.fn(); const initialVNode = (
- +
); renderElement(initialVNode, $container); diff --git a/src/components/posts/Post.jsx b/src/components/posts/Post.jsx index 67af756..cb19c89 100644 --- a/src/components/posts/Post.jsx +++ b/src/components/posts/Post.jsx @@ -1,14 +1,35 @@ /** @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 = ({ author, time, content, likeUsers }) => { + const { currentUser } = globalStore.getState(); + const activationLike = likeUsers.includes(currentUser?.username); + + const toggleLike = () => { + if (!currentUser) { + alert("로그인 후 이용해주세요"); + return; + } + + globalStore.setState(({ posts, ...rest }) => { + const foundIndex = posts.findIndex((post) => post.author === author); + + const updatedPost = { + ...posts[foundIndex], + likeUsers: activationLike + ? likeUsers.filter((likeUser) => likeUser !== currentUser.username) + : [...likeUsers, currentUser.username], + }; + + const clonedPosts = [...posts]; + clonedPosts[foundIndex] = updatedPost; + + return { ...rest, posts: clonedPosts }; + }); + }; + return (
@@ -21,6 +42,7 @@ export const Post = ({
좋아요 {likeUsers.length} diff --git a/src/components/posts/PostForm.jsx b/src/components/posts/PostForm.jsx index 36a2513..b879aed 100644 --- a/src/components/posts/PostForm.jsx +++ b/src/components/posts/PostForm.jsx @@ -1,7 +1,29 @@ /** @jsx createVNode */ import { createVNode } from "../../lib"; +import { globalStore } from "../../stores"; export const PostForm = () => { + const handleSubmit = () => { + const $textarea = document.querySelector("#post-content"); + const value = $textarea.value; + + globalStore.setState((prev) => { + const { username } = prev.currentUser; + const postIds = prev.posts.map((post) => post.id); + const id = Math.max(...postIds) + 1; + + const newPost = { + id, + author: username, + time: Date.now(), + content: value, + likeUsers: [], + }; + + return { ...prev, posts: [...prev.posts, newPost] }; + }); + }; + return (