board.tsx 2.05 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { useState, MouseEvent, useEffect } from "react";
Lee Soobeom's avatar
Lee Soobeom committed
2
import { Link } 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";
Lee Soobeom's avatar
Lee Soobeom committed
6

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

Lee Soobeom's avatar
Lee Soobeom committed
11
export default function BoardPage() {
Lee Soobeom's avatar
Lee Soobeom committed
12
13
14
15
  const [posts, setPosts] = useState<PostType[]>();

  useEffect(() => {
    getDataList();
Lee Soobeom's avatar
Lee Soobeom committed
16
17
  }, []);
  // posts
Lee Soobeom's avatar
Lee Soobeom committed
18
19
20
21
  const getDataList = async () => {
    const res = await postApi.getData();
    setPosts(res);
  };
Lee Soobeom's avatar
Lee Soobeom committed
22

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

Kim, MinGyu's avatar
Kim, MinGyu committed
38
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
39
40
41
42
    <div className="flex flex-col ">
      <div className="flex flex-col  mt-6">
        <div className="text-2xl">자유 게시판</div>
        <div className="text-sm mt-5">여행지 후기를 남겨주세요!</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
43
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
44

Kim, MinGyu's avatar
Kim, MinGyu committed
45
      <div className="flex flex-col w-full mt-16">
Kim, MinGyu's avatar
Kim, MinGyu committed
46
        <div className="flex justify-end">
Lee Soobeom's avatar
Lee Soobeom committed
47
          <div className="border-2 border-blue-500 rounded mb-2">
Kim, MinGyu's avatar
Kim, MinGyu committed
48
            <Link to="/posting">
Lee Soobeom's avatar
Lee Soobeom committed
49
              <button>글쓰기</button>
Kim, MinGyu's avatar
Kim, MinGyu committed
50
51
52
            </Link>
          </div>{" "}
          {/* Link */}
Lee Soobeom's avatar
Lee Soobeom committed
53
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
54
55
56
57
58
        <div className="sm:overflow-y-auto">
          <div className="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 ">
            <div className="basis-full">제목</div>
            <div className="basis-3/12">게시 날짜</div>
            <div className="basis-2/12">조회수</div>
Kim, MinGyu's avatar
Kim, MinGyu committed
59
          </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
60
          <div className="">
Lee Soobeom's avatar
Lee Soobeom committed
61
62
            {posts?.map((post, i) => (
              <Post key={i} post={post} handleClick={titleHandleClick} />
Kim, MinGyu's avatar
Kim, MinGyu committed
63
64
65
66
67
68
69
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}