diff --git a/frontend/src/board/board.tsx b/frontend/src/board/board.tsx index 955cab0a26142d4af46faf0930aeff263540e7d7..f31317b96660ae38af201ad70b18c8386c73c404 100644 --- a/frontend/src/board/board.tsx +++ b/frontend/src/board/board.tsx @@ -1,11 +1,17 @@ import React, { useState, MouseEvent, useEffect } from "react"; -import { Link, useLocation, useNavigate } from "react-router-dom"; +import { + Link, + Outlet, + useLocation, + useNavigate, + useOutletContext, +} from "react-router-dom"; import { PostType } from "../types"; import Post from "../post/post"; import { postApi } from "../apis"; import { useAuth } from "../auth/auth.context"; -interface Posts { +interface Props { posts: PostType[]; } @@ -14,36 +20,13 @@ interface Newpost { } export default function BoardPage() { - const [posts, setPosts] = useState(); + const posts = useOutletContext(); const location = useLocation() as Newpost; const newPost = location.state; - const deletePostId = location.state._id; const navigate = useNavigate(); const { user } = useAuth(); - // console.log("get newPost Info", newPost); - - const setNewPosts = (newpost: PostType) => { - const postArr = posts?.splice(-1, 0, newPost); - if (!(postArr === undefined)) { - setPosts(postArr); - } - }; - - useEffect(() => { - getDataList(); - setNewPosts(newPost); - }, []); - - const getDataList = async () => { - const res = await postApi.getData(); - setPosts(res); //posts = res - }; - - const deletePost = async (postId: string) => { - const res = await postApi.deletePost(postId); - return res; - }; + console.log("posts", posts); const handleClick = async (event: MouseEvent) => { const postId = event.currentTarget.id; @@ -55,8 +38,6 @@ export default function BoardPage() { if (!(newpost?._id === undefined)) { const post = newpost; const res = await postApi.addCounts(post._id, post.counts); - // console.log(res); - // setPosts(res); } }; @@ -82,7 +63,7 @@ export default function BoardPage() {
@@ -99,6 +80,8 @@ export default function BoardPage() { + + ); } diff --git a/frontend/src/board/posts.tsx b/frontend/src/board/posts.tsx index 80f9175a092b8462d018b33f0ecb826cc3b9a19d..dca8a7c93d3d5a7e5d916b0d30b8b09372a3b42e 100644 --- a/frontend/src/board/posts.tsx +++ b/frontend/src/board/posts.tsx @@ -1,6 +1,51 @@ -import React, { ReactNode } from "react"; -import { Outlet } from "react-router-dom"; +import React, { ReactNode, useState, useEffect } from "react"; +import { Outlet, useLocation } from "react-router-dom"; +import { postApi } from "../apis"; +import { PostType } from "../types"; + +interface FormType { + state: FormData; +} export default function Posts() { - return ; + const [posts, setPosts] = useState([]); + const location = useLocation() as FormType; + const formdata = location.state; + + useEffect(() => { + getDataList(); + }, []); + + //Read + const getDataList = async () => { + const res = await postApi.getData(); + setPosts(res); + }; + + //Create + const createPost = async (filelist: FileList) => { + if (!(filelist === undefined || filelist === null)) { + if (filelist.length === 1) { + formdata.append("picture", filelist?.[0]); + + const res = await postApi.createFileAndPost(formdata); + + return res; + } else { + for (var i = 0; i < filelist.length; i++) { + formdata.append("picture", filelist?.[i]); + } + const res = await postApi.createFileAndPost(formdata); + + return res; + } + } + }; + //Delete + const deletePost = async (postId: string) => { + const res = await postApi.deletePost(postId); + return res; + }; + console.log("sdfa", posts); + return ; } diff --git a/frontend/src/home/header.tsx b/frontend/src/home/header.tsx index 0a15344798f938cfd1fe042a3a442e369e189dd1..b56fd4ca4e5ecea545227b584bc2b5537514afb7 100644 --- a/frontend/src/home/header.tsx +++ b/frontend/src/home/header.tsx @@ -73,7 +73,7 @@ export default function Header() {