diff --git a/src/routes/PostEditPage.jsx b/src/routes/PostEditPage.jsx
index ff86e811..80d0cc9b 100644
--- a/src/routes/PostEditPage.jsx
+++ b/src/routes/PostEditPage.jsx
@@ -1,24 +1,104 @@
-import { useEffect, useState } from "react";
+import { useState, useEffect } 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 { postId } = useParams();
+ 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(() => {
const post = posts.find((post) => post.id === parseInt(postId));
const originalPost = { ...post, tags: post.tags.map((tag) => tag.content) };
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)
+ //TODO : api connect
};
- return (
+ return isSubmitted ? (
+
+
+
+ ) : (
게시글 수정
+ {autoCompletes &&
+ autoCompletes.map((autoComplete) => (
+
+ ))}
+
))}
@@ -84,4 +180,4 @@ const PostEditPage = () => {
);
};
-export default PostEditPage;
+export default PostEditPage;
\ No newline at end of file