board.tsx 2.02 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
  }, []);
백승민's avatar
백승민 committed
17
  
Lee Soobeom's avatar
Lee Soobeom committed
18
  // posts
Lee Soobeom's avatar
Lee Soobeom committed
19
20
21
22
  const getDataList = async () => {
    const res = await postApi.getData();
    setPosts(res);
  };
Lee Soobeom's avatar
Lee Soobeom committed
23

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

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

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