board.tsx 2.54 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { useState, MouseEvent, useEffect } from "react";
Kim, MinGyu's avatar
test    
Kim, MinGyu committed
2
3
4
5
6
7
8
9
import {
  Link,
  Outlet,
  useLocation,
  useNavigate,
  useOutletContext,
} from "react-router-dom";

10
11
import { PostType } from "../types";
import Post from "../post/post";
Lee Soobeom's avatar
Lee Soobeom committed
12
import { postApi } from "../apis";
Kim, MinGyu's avatar
Kim, MinGyu committed
13
import { useAuth } from "../auth/auth.context";
Lee Soobeom's avatar
Lee Soobeom committed
14

Kim, MinGyu's avatar
test    
Kim, MinGyu committed
15
interface Props {
Kim, MinGyu's avatar
Kim, MinGyu committed
16
  posts: PostType[];
17
18
}

Lee Soobeom's avatar
Lee Soobeom committed
19
20
21
22
interface Newpost {
  state: PostType;
}

Lee Soobeom's avatar
Lee Soobeom committed
23
export default function BoardPage() {
Kim, MinGyu's avatar
test    
Kim, MinGyu committed
24
  const posts = useOutletContext<PostType[]>();
Lee Soobeom's avatar
Lee Soobeom committed
25
26
  const location = useLocation() as Newpost;
  const newPost = location.state;
Kim, MinGyu's avatar
Kim, MinGyu committed
27
28
  const navigate = useNavigate();
  const { user } = useAuth();
Lee Soobeom's avatar
Lee Soobeom committed
29

Kim, MinGyu's avatar
test    
Kim, MinGyu committed
30
31
32
33
34
  const handleClick = async (event: MouseEvent<HTMLButtonElement>) => {
    const postId = event.currentTarget.id;
    const newpost = posts?.find((element) => {
      if (element._id === postId) {
        return element;
Kim, MinGyu's avatar
Kim, MinGyu committed
35
      }
Kim, MinGyu's avatar
test    
Kim, MinGyu committed
36
37
38
39
40
41
    });
    if (!(newpost?._id === undefined)) {
      const post = newpost;
      const res = await postApi.addCounts(post._id, post.counts);
      // console.log(res);
      // setPosts(res);
Lee Soobeom's avatar
Lee Soobeom committed
42
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
43
  };
44

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

Kim, MinGyu's avatar
Kim, MinGyu committed
52
  return (
Lee Soobeom's avatar
Lee Soobeom committed
53
54
55
56
    <div className="flex flex-col shadow-lg bg-white rounded px-8 py-4">
      <div className="flex flex-col my-6 ">
        <div className="text-4xl font-semibold whitespace-nowrap">
          자유 게시판
Kim, MinGyu's avatar
Kim, MinGyu committed
57
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
58
59
60
        {/* <div className="text-sm mt-5 whitespace-nowrap">
          여행지 후기를 남겨주세요!
        </div> */}
Kim, MinGyu's avatar
Kim, MinGyu committed
61
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
62

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