diff --git a/src/routes/PostEditPage.jsx b/src/routes/PostEditPage.jsx
index ff86e811..233bde6e 100644
--- a/src/routes/PostEditPage.jsx
+++ b/src/routes/PostEditPage.jsx
@@ -1,10 +1,34 @@
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import posts from "../data/posts";
+import { BigPost } from "../components/Posts";
const PostEditPage = () => {
const { postId } = useParams();
- const [post, setPost] = useState({});
+ const [isSubmitted, setIsSubmitted] = useState(false);
+ const [tagInputValue, setTagInputValue] = useState("");
+ const [autoCompletes, setAutoCompletes] = useState([]);
+ const [tags, setTags] = useState([]);
+ const [post, setPost] = useState({
+ id: posts.length,
+ title: "",
+ content: "",
+ author: { id: posts.length, username: "아기사자" },
+ tags: [],
+ like_users: [],
+ created_at: "2024-02-04T07:42:50.658501Z",
+ });
+
+ useEffect(() => {
+ const duplicatedTagList = posts.reduce((acc, post) => {
+ for (let tag of post.tags) {
+ acc.add(tag.content);
+ }
+ return acc;
+ }, new Set());
+ const tagList = [...duplicatedTagList];
+ setTags([...tagList]);
+ }, []);
// 기존 게시글 불러오기
useEffect(() => {
@@ -13,12 +37,69 @@ const PostEditPage = () => {
setPost(originalPost);
}, [postId]);
+ const handleChange = (e) => {
+ setPost({ ...post, [e.target.id]: e.target.value });
+ };
+
+ const handleTag = (e) => {
+ setTagInputValue(e.target.value);
+ if (e.target.value) {
+ const autoCompleteData = tags.filter((tag) =>
+ tag.includes(e.target.value)
+ );
+ setAutoCompletes(autoCompleteData);
+ }
+ };
+
+ const handleAutoCompletes = (autoComplete) => {
+ const selectedTag = tags.find((tag) => tag === autoComplete);
+ if (post.tags.includes(selectedTag)) return;
+ setPost({
+ ...post,
+ tags: [...post.tags, selectedTag],
+ });
+ setTagInputValue("");
+ setAutoCompletes([]);
+ };
+
+ const addTag = (e) => {
+ e.preventDefault();
+ if (post.tags.find((tag) => tag === tagInputValue)) return;
+ setPost({
+ ...post,
+ tags: [...post.tags, tagInputValue],
+ });
+ setTagInputValue("");
+ setAutoCompletes([]);
+ };
+
+ const deleteTag = (tag) => {
+ setPost({
+ ...post,
+ tags: post.tags.filter((t) => t !== tag),
+ });
+ };
+
const onSubmit = (e) => {
+ e.preventDefault();
+ const editedPost = {
+ ...post,
+ like_users: [],
+ tags: post.tags.map((tag, idx) => {
+ return { id: idx + 1, content: tag };
+ }),
+ };
+ setPost(editedPost);
+ setIsSubmitted(true);
alert("게시글을 수정합니다.");
// TODO : api connect(edit post)
};
- return (
+ return isSubmitted ? (
+
+
+
+ ) : (
게시글 수정