Skip to content

Commit

Permalink
feat: 게시글 추가 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
devJayve committed Dec 22, 2024
1 parent 6a2f991 commit 520f730
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/components/posts/Post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const Post = ({ id, author, time, content, likeUsers, loggedIn }) => {
const activationLike =
currentUser && likeUsers.includes(currentUser.username);

function onClickLike() {
const handleToggleLike = () => {
if (!loggedIn) {
alert("로그인 후 이용해주세요");
return;
}
toggleLike(id);
}
};

return (
<div className="bg-white rounded-lg shadow p-4 mb-4">
Expand All @@ -28,7 +28,7 @@ export const Post = ({ id, author, time, content, likeUsers, loggedIn }) => {
<p>{content}</p>
<div className="mt-2 flex justify-between text-gray-500">
<span
onClick={onClickLike}
onClick={handleToggleLike}
className={`like-button cursor-pointer${activationLike ? " text-blue-500" : ""}`}
>
좋아요 {likeUsers.length}
Expand Down
21 changes: 19 additions & 2 deletions src/components/posts/PostForm.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
/** @jsx createVNode */
import { createVNode } from "../../lib";
import { globalStore } from "../../stores/index.js";

export const PostForm = () => {
const { addPost } = globalStore.actions;

const handlePostSubmit = (e) => {
e.preventDefault();
const contentInput = document.getElementById("post-content");
const content = contentInput.value.trim();
if (!content) return;

addPost(content);
contentInput.value = "";
};
return (
<div className="mb-4 bg-white rounded-lg shadow p-4">
<form
onSubmit={handlePostSubmit}
className="mb-4 bg-white rounded-lg shadow p-4"
>
<textarea
id="post-content"
name="content"
placeholder="무슨 생각을 하고 계신가요?"
className="w-full p-2 border rounded"
/>
<button
id="post-submit"
type="submit"
className="mt-2 bg-blue-600 text-white px-4 py-2 rounded"
>
게시
</button>
</div>
</form>
);
};
1 change: 0 additions & 1 deletion src/pages/HomePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const HomePage = () => {
})}
</div>
</main>

<Footer />
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/stores/globalStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,22 @@ export const globalStore = createStore(
}),
};
},
addPost(state, content) {
const currentUser = state.currentUser;
if (!currentUser) return state;

const newPost = {
id: Date.now(),
author: currentUser.username,
time: Date.now(),
content,
likeUsers: [],
};

return {
...state,
posts: [newPost, ...state.posts],
};
},
},
);

0 comments on commit 520f730

Please sign in to comment.