board.tsx 1.95 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
40
41
42
43
  return (
    <div className="flex flex-col items-center">
      <div className="flex flex-col items-center mt-6">
        <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
47
48
49
50
51
52
      <div className="flex flex-col w-10/12 mt-16">
        <div className="flex justify-end">
          <div className="border-2 mb-2">
            <Link to="/posting">
              <button>글쓰기+</button>
            </Link>
          </div>{" "}
          {/* Link */}
Lee Soobeom's avatar
Lee Soobeom committed
53
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
54
55
56
57
58
59
60
        <div className="sm:overflow-y-scroll">
          <div className="flex flex-row divide-x-2 border-2 border-solid bg-gray-500 border-y-2 h-10 ">
            <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>
  );
}