board.tsx 2.01 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
16
17
18
19
20
21
  const [posts, setPosts] = useState<PostType[]>();

  useEffect(() => {
    getDataList();
  }, [posts]);

  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
39
  return (
    <div className="flex flex-col items-center">
Lee Soobeom's avatar
Lee Soobeom committed
40
      <div className="flex flex-col w-10/12 items-center mt-6">
Kim, MinGyu's avatar
Kim, MinGyu committed
41
42
43
        <div>`Travel Report's Board`</div>
        <div>`여행지 후기를 남겨주세요!`</div>
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
44

Kim, MinGyu's avatar
Kim, MinGyu committed
45
46
      <div className="flex flex-col w-10/12 mt-16">
        <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
        <div className="sm:overflow-y-scroll">
Lee Soobeom's avatar
Lee Soobeom committed
55
          <div className="flex flex-row 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
56
57
58
59
60
            <div className="basis-full">Title</div>
            <div className="basis-3/12">Date</div>
            <div className="basis-2/12">Clicks</div>
          </div>
          <div>
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>
  );
}