@@ -20,6 +52,19 @@ const HomePage = () => {
className="border border-orange-400 outline-none rounded-2xl text-center py-2 px-20 text-orange-400 bg-transparent"
/>
+
{postList.map((post) => (
diff --git a/src/routes/PostCreatePage.jsx b/src/routes/PostCreatePage.jsx
index a33c7af4..1dd42922 100644
--- a/src/routes/PostCreatePage.jsx
+++ b/src/routes/PostCreatePage.jsx
@@ -1,7 +1,13 @@
+import { useState, useEffect } from "react";
import posts from "../data/posts";
+import { BigPost } from "../components/Posts";
const PostCreatePage = () => {
- const post = {
+ const [isSubmitted, setIsSubmitted] = useState(false);
+ const [tags, setTags] = useState([]);
+ const [tagInputValue, setTagInputValue] = useState("");
+ const [autoCompletes, setAutoCompletes] = useState([]);
+ const [post, setPost] = useState({
id: posts.length,
title: "",
content: "",
@@ -9,14 +15,82 @@ const PostCreatePage = () => {
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]);
+ }, []);
+
+ 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 handleTag = (e) => {
+ setTagInputValue(e.target.value);
+ if (e.target.value) {
+ const autoCompleteData = tags.filter((tag) =>
+ tag.includes(e.target.value)
+ );
+ setAutoCompletes(autoCompleteData);
+ } else {
+ setAutoCompletes([]);
+ }
+ };
+ const handleChange = (e) => {
+ setPost({ ...post, [e.target.id]: e.target.value });
};
const onSubmit = (e) => {
+ e.preventDefault();
+ const createdPost = {
+ ...post,
+ like_users: [],
+ tags: post.tags.map((tag, idx) => {
+ return { id: idx + 1, content: tag };
+ }),
+ };
+ setPost(createdPost);
+ setIsSubmitted(true);
alert("게시글을 등록합니다.");
//TODO : api connect(create post)
};
- return (
+ 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),
+ });
+ };
+
+ return isSubmitted ? (
+
+
+
+ ) : (
게시글 작성
+
+ {autoCompletes &&
+ autoCompletes.map((autoComplete) => (
+
+ ))}
+
{post.tags && (
+
diff --git a/src/routes/PostEditPage.jsx b/src/routes/PostEditPage.jsx
index ff86e811..b28fbea4 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 [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) => (
+
+ ))}
+
{post.tags && (
{post.tags.map((tag) => (
@@ -69,8 +165,8 @@ const PostEditPage = () => {
#{tag}
))}
diff --git a/src/routes/SignInPage.jsx b/src/routes/SignInPage.jsx
index 537272d3..1a17be90 100644
--- a/src/routes/SignInPage.jsx
+++ b/src/routes/SignInPage.jsx
@@ -1,25 +1,61 @@
+import { useState } from "react";
+
const SignInPage = () => {
- const handleSignInSubmit = () => {
- alert("로그인 하기"); // TODO: add api call for sign in
- };
+ const [signInData, setSignInData] = useState({
+ username: "",
+ password: "",
+ });
+
+ const handleSignInData = (e) => {
+ const { id, value } = e.target;
+ setSignInData({ ...signInData, [id]: value });
+ };
+
+ const handleSignInSubmit = (e) => {
+ e.preventDefault(); // to prevent reloading the page
+ console.log(signInData);
+ alert("로그인 하기"); // TODO: add api call for sign in
+ };
+
+ return (
+
+ );
};
export default SignInPage;
diff --git a/src/routes/SignUpPage.jsx b/src/routes/SignUpPage.jsx
index 9b87106b..dc91ac8a 100644
--- a/src/routes/SignUpPage.jsx
+++ b/src/routes/SignUpPage.jsx
@@ -1,5 +1,22 @@
+import { useState } from "react";
+
const SignUpPage = () => {
- const handleSignUpSubmit = () => {
+ const [signUpData, setSignUpData] = useState({
+ email: "",
+ password: "",
+ confirm_password: "",
+ username: "",
+ college: "",
+ major: "",
+ });
+
+ const handleSignUpData = (e) => {
+ const { id, value } = e.target;
+ setSignUpData({ ...signUpData, [id]: value });
+ };
+ const handleSignUpSubmit = (e) => {
+ e.preventDefault(); // to prevent reloading the page
+ console.log(signUpData);
alert("회원가입 하기"); // TODO: add api call for sign up
};
@@ -10,17 +27,38 @@ const SignUpPage = () => {
-
+
-
+
-
+