intopost.tsx 3.56 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
import React, { MouseEvent, useEffect, useState } from "react";
Lee Soobeom's avatar
Lee Soobeom committed
2
import { useLocation, useNavigate, Link, Outlet } from "react-router-dom";
Lee Soobeom's avatar
Lee Soobeom committed
3
import { catchErrors } from "../helpers";
Lee Soobeom's avatar
Lee Soobeom committed
4
import { postApi } from "../apis";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
import { PostType } from "../types";
Lee Soobeom's avatar
Lee Soobeom committed
6
import { Props } from "./post";
Lee Soobeom's avatar
Lee Soobeom committed
7

Lee Soobeom's avatar
Lee Soobeom committed
8
export interface PostState {
Yoon, Daeki's avatar
Yoon, Daeki committed
9
  state: PostType;
Lee Soobeom's avatar
Lee Soobeom committed
10
11
12
}

export function IntoPost() {
Lee Soobeom's avatar
Lee Soobeom committed
13
  const [posts, setPosts] = useState<PostType>();
Lee Soobeom's avatar
Lee Soobeom committed
14
15
  const location = useLocation() as PostState;
  const post = location.state;
Lee Soobeom's avatar
Lee Soobeom committed
16
  const navigate = useNavigate();
Lee Soobeom's avatar
Lee Soobeom committed
17

Lee Soobeom's avatar
Lee Soobeom committed
18
19
20
21
22
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [addSuccess, setAddSuccess] = useState(false);
  const [delSuccess, setDelSuccess] = useState(false);

Lee Soobeom's avatar
Lee Soobeom committed
23
24
25
26
27
28
  // console.log("post.file", post.file);

  useEffect(() => {
    setPosts(post);
  }, []);

Lee Soobeom's avatar
Lee Soobeom committed
29
  const handleDeleteClick = async (event: MouseEvent<HTMLButtonElement>) => {
Lee Soobeom's avatar
Lee Soobeom committed
30
31
32
33
    try {
      if (confirm("삭제하시겠습니까?") == true) {
        const postId = event.currentTarget.id;
        const res = await postApi.deletePost(postId);
Lee Soobeom's avatar
Lee Soobeom committed
34
        navigate("/board");
Lee Soobeom's avatar
Lee Soobeom committed
35
36
37
38
39
40
41
42
        console.log("delete post", res);
      } else {
        return false;
      }
    } catch (error) {
      console.log("에러발생");
      catchErrors(error, setError);
    }
Lee Soobeom's avatar
Lee Soobeom committed
43
44
  };

Lee Soobeom's avatar
Lee Soobeom committed
45
  return (
Lee Soobeom's avatar
Lee Soobeom committed
46
47
48
49
    <div className="flex flex-col shadow-lg bg-white rounded px-8 py-4">
      <div className="text-3xl font-semibold md:text-4xl">
        [{post.city}] / [{post.theme}] Travel Report
      </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
50
51
      <div className="flex h-8 gap-x-1 place-content-end place-items-center">
        <button
Lee Soobeom's avatar
Lee Soobeom committed
52
          id={posts?._id}
Kim, MinGyu's avatar
Kim, MinGyu committed
53
          onClick={handleDeleteClick}
Lee Soobeom's avatar
Lee Soobeom committed
54
          className=" whitespace-nowrap flex border-2 border-sky-100 place-self-center h-6 w-8 text-xs text-center transition delay-150 bg-white-400 hover:-translate-y-1 hover:bg-red-300 duration-300"
Kim, MinGyu's avatar
Kim, MinGyu committed
55
56
57
        >
          삭제
        </button>
Lee Soobeom's avatar
Lee Soobeom committed
58
59
        <button className="whitespace-nowrap flex border-2 border-sky-100 place-self-center h-6 w-8 text-xs transition delay-150 bg-white-400 hover:-translate-y-1 hover:bg-sky-300 duration-300">
          <Link to="edit" state={posts}>
Kim, MinGyu's avatar
Kim, MinGyu committed
60
            수정
Lee Soobeom's avatar
Lee Soobeom committed
61
62
          </Link>
        </button>
Kim, MinGyu's avatar
Kim, MinGyu committed
63
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
64
      <div className="flex h-10 border-t-2 border-sky-500 items-center text-2xl font-medium md:text-2xl">
Lee Soobeom's avatar
Lee Soobeom committed
65
        {posts?.title}
Kim, MinGyu's avatar
Kim, MinGyu committed
66
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
67
68
      <div className="flex h-10 items-center border-t-2 border-sky-200 md:flex-row justify-between bg-slate-50 text-sm divide-x divide-slate-300">
        <div className="flex whitespace-nowrap px-0.5">
Lee Soobeom's avatar
Lee Soobeom committed
69
          작성자: {posts?.user.name}
Lee Soobeom's avatar
Lee Soobeom committed
70
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
71
72

        <div className="flex divide-x divide-slate-300 ">
Lee Soobeom's avatar
Lee Soobeom committed
73
          <div className="flex basis-1/2 whitespace-nowrap px-0.5 md:px-2">
Lee Soobeom's avatar
Lee Soobeom committed
74
            작성일 : {posts?.date.slice(0, 10)}
Kim, MinGyu's avatar
Kim, MinGyu committed
75
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
76
          <div className="md:flex hidden md:whitespace-nowrap md:px-2 ">
Lee Soobeom's avatar
Lee Soobeom committed
77
78
79
            {" "}
            {posts?.city}
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
80
          <div className="md:flex hidden md:whitespace-nowrap md:px-2">
Lee Soobeom's avatar
Lee Soobeom committed
81
82
83
84
            {" "}
            {posts?.theme}
          </div>
          <div className="flex whitespace-nowrap px-0.5 md:px-2">
Lee Soobeom's avatar
Lee Soobeom committed
85
            조회수 : {posts?.counts}
Lee Soobeom's avatar
Lee Soobeom committed
86
87
          </div>
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
88
      </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
89
      <div className="flex border-t-2 border-sky-200 h-44 p-2 overflow-auto mb-5 ">
Lee Soobeom's avatar
Lee Soobeom committed
90
        {posts?.file?.map((file, i) => (
Kim, MinGyu's avatar
Kim, MinGyu committed
91
92
93
94
95
96
97
98
          <img
            key={i}
            src={"http://localhost:3000/images/" + file.newfilename}
            width={200}
            height={200}
          />
        ))}
      </div>
Lee Soobeom's avatar
Lee Soobeom committed
99
      <div className="border-b-2 border-sky-500 h-44 mb-10">{posts?.text}</div>
Lee Soobeom's avatar
Lee Soobeom committed
100
101
102
    </div>
  );
}