board.tsx 2.45 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { useState, MouseEvent, useEffect } from "react";
Kim, MinGyu's avatar
Kim, MinGyu committed
2
import { Link, 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
export default function BoardPage() {
Lee Soobeom's avatar
Lee Soobeom committed
13
  const [posts, setPosts] = useState<PostType[]>();
Kim, MinGyu's avatar
Kim, MinGyu committed
14
15
  const navigate = useNavigate();
  const { user } = useAuth();
Lee Soobeom's avatar
Lee Soobeom committed
16
17
18

  useEffect(() => {
    getDataList();
Lee Soobeom's avatar
Lee Soobeom committed
19
  }, []);
20

Lee Soobeom's avatar
Lee Soobeom committed
21
  // posts
Lee Soobeom's avatar
Lee Soobeom committed
22
23
24
25
  const getDataList = async () => {
    const res = await postApi.getData();
    setPosts(res);
  };
Lee Soobeom's avatar
Lee Soobeom committed
26

Lee Soobeom's avatar
Lee Soobeom committed
27
  const titleHandleClick = async (event: MouseEvent<HTMLButtonElement>) => {
Kim, MinGyu's avatar
Kim, MinGyu committed
28
    const postId = event.currentTarget.id;
Lee Soobeom's avatar
Lee Soobeom committed
29
30
31
    const newpost = posts?.find((element) => {
      if (element._id === postId) {
        return element;
Kim, MinGyu's avatar
Kim, MinGyu committed
32
33
      }
    });
Lee Soobeom's avatar
Lee Soobeom committed
34
35
36
37
38
39
    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
40
  };
41

Kim, MinGyu's avatar
Kim, MinGyu committed
42
43
44
45
46
47
48
  const goLogin = async (event: React.MouseEvent) => {
    if (!user.isLoggedIn) {
      alert("로그인이 필요합니다.");
      navigate("/login", { replace: true });
    }
  };

Kim, MinGyu's avatar
Kim, MinGyu committed
49
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
50
51
    <div className="flex flex-col ">
      <div className="flex flex-col  mt-6">
Kim, MinGyu's avatar
Kim, MinGyu committed
52
53
54
55
        <div className="text-2xl whitespace-nowrap">자유 게시판</div>
        <div className="text-sm mt-5 whitespace-nowrap">
          여행지 후기를 남겨주세요!
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
56
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
57

Kim, MinGyu's avatar
Kim, MinGyu committed
58
      <div className="flex flex-col w-full mt-16">
Kim, MinGyu's avatar
Kim, MinGyu committed
59
        <div className="flex justify-end">
Kim, MinGyu's avatar
Kim, MinGyu committed
60
61
62
63
64
          <div className="border-2 border-blue-500 rounded mb-2 whitespace-nowrap">
            <button onClick={goLogin}>
              <Link to="/posting">글쓰기</Link>
            </button>
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
65
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
66
        <div className="sm:overflow-y-auto">
Kim, MinGyu's avatar
Kim, MinGyu committed
67
          <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 ">
Kim, MinGyu's avatar
Kim, MinGyu committed
68
69
70
            <div className="basis-full">제목</div>
            <div className="basis-3/12">게시 날짜</div>
            <div className="basis-2/12">조회수</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
71
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
72
          <div className="whitespace-nowrap">
Lee Soobeom's avatar
Lee Soobeom committed
73
74
            {posts?.map((post, i) => (
              <Post key={i} post={post} handleClick={titleHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
75
76
77
78
79
80
81
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}