board.tsx 3.1 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { useState, MouseEvent, useEffect } from "react";
2
import { Link, useLocation, useNavigate } from "react-router-dom";
3
4
import { PostType } from "../types";
import Post from "../post/post";
Lee Soobeom's avatar
Lee Soobeom committed
5
import { postApi } from "../apis";
Kim, MinGyu's avatar
Kim, MinGyu committed
6
import { useAuth } from "../auth/auth.context";
Lee Soobeom's avatar
Lee Soobeom committed
7

8
interface Posts {
Kim, MinGyu's avatar
Kim, MinGyu committed
9
  posts: PostType[];
10
11
}

Lee Soobeom's avatar
Lee Soobeom committed
12
13
14
15
interface Newpost {
  state: PostType;
}

Lee Soobeom's avatar
Lee Soobeom committed
16
export default function BoardPage() {
Lee Soobeom's avatar
Lee Soobeom committed
17
  const [posts, setPosts] = useState<PostType[]>();
Lee Soobeom's avatar
Lee Soobeom committed
18
19
  const location = useLocation() as Newpost;
  const newPost = location.state;
Kim, MinGyu's avatar
Kim, MinGyu committed
20
21
  const navigate = useNavigate();
  const { user } = useAuth();
Lee Soobeom's avatar
Lee Soobeom committed
22

Lee Soobeom's avatar
Lee Soobeom committed
23
  // console.log("get newPost Info", newPost);
Lee Soobeom's avatar
Lee Soobeom committed
24
25
26
27
28
29
30

  const setNewPosts = (newpost: PostType) => {
    const postArr = posts?.splice(-1, 0, newPost);
    if (!(postArr === undefined)) {
      setPosts(postArr);
    }
  };
Lee Soobeom's avatar
Lee Soobeom committed
31
32
33

  useEffect(() => {
    getDataList();
Lee Soobeom's avatar
Lee Soobeom committed
34
    setNewPosts(newPost);
Lee Soobeom's avatar
Lee Soobeom committed
35
  }, []);
36

Lee Soobeom's avatar
Lee Soobeom committed
37
  // posts
Lee Soobeom's avatar
Lee Soobeom committed
38
39
  const getDataList = async () => {
    const res = await postApi.getData();
Lee Soobeom's avatar
Lee Soobeom committed
40
    setPosts(res); //posts = res
Lee Soobeom's avatar
Lee Soobeom committed
41
  };
Lee Soobeom's avatar
Lee Soobeom committed
42

Lee Soobeom's avatar
Lee Soobeom committed
43
  const titleHandleClick = async (event: MouseEvent<HTMLButtonElement>) => {
Lee Soobeom's avatar
Lee Soobeom committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    if (!user.isLoggedIn) {
      alert("로그인이 필요합니다.");
      navigate("/login", { replace: true });
    } else {
      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);
Kim, MinGyu's avatar
Kim, MinGyu committed
59
      }
Lee Soobeom's avatar
Lee Soobeom committed
60
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
61
  };
62

63
  const GoLogin = async (event: React.MouseEvent) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
64
65
66
67
68
69
    if (!user.isLoggedIn) {
      alert("로그인이 필요합니다.");
      navigate("/login", { replace: true });
    }
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
70
  return (
Lee Soobeom's avatar
Lee Soobeom committed
71
72
73
74
    <div className="flex flex-col shadow-lg bg-white rounded px-8 py-4">
      <div className="flex flex-col my-6 ">
        <div className="text-4xl font-semibold whitespace-nowrap">
          자유 게시판
Kim, MinGyu's avatar
Kim, MinGyu committed
75
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
76
77
78
        {/* <div className="text-sm mt-5 whitespace-nowrap">
          여행지 후기를 남겨주세요!
        </div> */}
Kim, MinGyu's avatar
Kim, MinGyu committed
79
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
80

Lee Soobeom's avatar
Lee Soobeom committed
81
      <div className="flex flex-col w-full">
Kim, MinGyu's avatar
Kim, MinGyu committed
82
        <div className="flex justify-end">
Kim, MinGyu's avatar
Kim, MinGyu committed
83
          <div className="border-2 border-blue-500 rounded mb-2 whitespace-nowrap">
84
            <button onClick={GoLogin}>
Kim, MinGyu's avatar
Kim, MinGyu committed
85
86
87
              <Link to="/posting">글쓰기</Link>
            </button>
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
88
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
89
        <div className="sm:overflow-y-auto">
Kim, MinGyu's avatar
Kim, MinGyu committed
90
          <div className="whitespace-nowrap flex place-items-center divide-x-2 border-2 border-solid border-y-2 h-10 bg-gradient-to-r from-cyan-500 to-blue-500 ">
Lee Soobeom's avatar
수정    
Lee Soobeom committed
91
92
93
            <div className="flex basis-full text-xs md:text-lg">제목</div>
            <div className="flex basis-3/12 text-xs md:text-lg">날짜</div>
            <div className="flex basis-3/12 text-xs md:text-lg">조회수</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
94
          </div>
Lee Soobeom's avatar
수정    
Lee Soobeom committed
95
          <div>
Lee Soobeom's avatar
Lee Soobeom committed
96
97
            {posts?.map((post, i) => (
              <Post key={i} post={post} handleClick={titleHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
98
99
100
101
102
103
104
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}