import React, { useState, MouseEvent, useEffect } from "react"; import { Link, useLocation, useNavigate } 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 { posts: PostType[]; } interface Newpost { state: PostType; } export default function BoardPage() { const [posts, setPosts] = useState(); const location = useLocation() as Newpost; const newPost = location.state; 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); }, []); // posts const getDataList = async () => { const res = await postApi.getData(); setPosts(res); //posts = res }; const titleHandleClick = async (event: MouseEvent) => { const postId = event.currentTarget.id; const newpost = posts?.find((element) => { if (element._id === postId) { return element; } }); if (!(newpost?._id === undefined)) { const post = newpost; const res = await postApi.addCounts(post._id, post.counts); // console.log(res); setPosts(res); } }; const GoLogin = async (event: React.MouseEvent) => { if (!user.isLoggedIn) { alert("로그인이 필요합니다."); navigate("/login", { replace: true }); } }; return (
자유 게시판
여행지 후기를 남겨주세요!
제목
게시 날짜
조회수
{posts?.map((post, i) => ( ))}
); }