board.tsx 2.55 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { useState, MouseEvent, useEffect } from "react";
Lee Soobeom's avatar
0816    
Lee Soobeom committed
2
3
4
5
6
7
8
import {
  Link,
  Outlet,
  useLocation,
  useNavigate,
  useOutletContext,
} from "react-router-dom";
9
10
import { PostType } from "../types";
import Post from "../post/post";
Lee Soobeom's avatar
Lee Soobeom committed
11
import { postApi } from "../apis";
Kim, MinGyu's avatar
Kim, MinGyu committed
12
import { useAuth } from "../auth/auth.context";
Lee Soobeom's avatar
Lee Soobeom committed
13

Lee Soobeom's avatar
0816    
Lee Soobeom committed
14
interface Props {
Kim, MinGyu's avatar
Kim, MinGyu committed
15
  posts: PostType[];
16
17
}

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

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

Lee Soobeom's avatar
0816    
Lee Soobeom committed
29
  console.log("posts", posts);
Lee Soobeom's avatar
board    
Lee Soobeom committed
30
31
32
33
34
35

  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
36
      }
Lee Soobeom's avatar
board    
Lee Soobeom committed
37
38
39
40
    });
    if (!(newpost?._id === undefined)) {
      const post = newpost;
      const res = await postApi.addCounts(post._id, post.counts);
Lee Soobeom's avatar
Lee Soobeom committed
41
    }
Kim, MinGyu's avatar
Kim, MinGyu committed
42
  };
43

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

Kim, MinGyu's avatar
Kim, MinGyu committed
51
  return (
Lee Soobeom's avatar
Lee Soobeom committed
52
53
54
55
    <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
56
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
57
58
59
        {/* <div className="text-sm mt-5 whitespace-nowrap">
          여행지 후기를 남겨주세요!
        </div> */}
Kim, MinGyu's avatar
Kim, MinGyu committed
60
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
61

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

      <Outlet />
Kim, MinGyu's avatar
Kim, MinGyu committed
85
86
87
    </div>
  );
}