intopost.tsx 3.09 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

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

export function IntoPost() {
  const location = useLocation() as PostState;
  const post = location.state;
Lee Soobeom's avatar
Lee Soobeom committed
14
  const navigate = useNavigate();
Lee Soobeom's avatar
Lee Soobeom committed
15

Lee Soobeom's avatar
Lee Soobeom committed
16
17
18
19
20
  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
21
  const handleDeleteClick = async (event: MouseEvent<HTMLButtonElement>) => {
Lee Soobeom's avatar
Lee Soobeom committed
22
23
24
25
26
27
28
29
30
31
32
33
34
    try {
      if (confirm("삭제하시겠습니까?") == true) {
        const postId = event.currentTarget.id;
        const res = await postApi.deletePost(postId);
        navigate("/board", { replace: true });
        console.log("delete post", res);
      } else {
        return false;
      }
    } catch (error) {
      console.log("에러발생");
      catchErrors(error, setError);
    }
Lee Soobeom's avatar
Lee Soobeom committed
35
36
  };

Lee Soobeom's avatar
Lee Soobeom committed
37
  return (
Kim, MinGyu's avatar
Kim, MinGyu committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    <div className="flex flex-col">
      <div className="flex h-8 gap-x-1 place-content-end place-items-center">
        <button
          id={post._id}
          onClick={handleDeleteClick}
          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:scale-110 hover:bg-red-300 duration-300"
        >
          삭제
        </button>
        <Link to="/edit" state={post}>
          <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:scale-110 hover:bg-sky-300 duration-300">
            수정
          </button>
        </Link>
      </div>
      <div className="flex h-10 border-t-2 border-sky-500 items-center font-semibold">
        {post.title}
      </div>

      <div className="flex h-10 items-center border-t-2 border-sky-200 md:flex-row justify-between bg-slate-50 text-sm">
        <div className="flex whitespace-nowrap pr-5 ">
          작성자: {post.user.slice(0, 8)}
Lee Soobeom's avatar
Lee Soobeom committed
60
        </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
61
62
63
64
65
66
67
68
69

        <div className="flex divide-x divide-slate-300 ">
          <div className="flex basis-1/2 whitespace-nowrap px-2">
            작성일 : {post.date.slice(0, 10)}
          </div>
          <div className="flex whitespace-nowrap px-2"> {post.city}</div>
          <div className="flex whitespace-nowrap px-2"> {post.theme}</div>
          <div className="flex whitespace-nowrap px-2">
            조회수 : {post.counts}
Lee Soobeom's avatar
Lee Soobeom committed
70
71
          </div>
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
72
      </div>
Kim, MinGyu's avatar
Kim, MinGyu committed
73
74

      <div className="flex border-t-2 border-sky-200 h-44 p-2 overflow-auto mb-5 ">
Lee Soobeom's avatar
Lee Soobeom committed
75
        {post.file?.map((file, i) => (
Kim, MinGyu's avatar
Kim, MinGyu committed
76
77
78
79
80
81
82
83
84
          <img
            key={i}
            src={"http://localhost:3000/images/" + file.newfilename}
            width={200}
            height={200}
          />
        ))}
      </div>
      <div className="border-b-2 border-sky-500 h-44 mb-10">{post.text}</div>
Lee Soobeom's avatar
Lee Soobeom committed
85
86
87
    </div>
  );
}