intopost.tsx 4.15 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
}

Lee Soobeom's avatar
Lee Soobeom committed
11
12
13
14
15
16
17
18
19
20
21
22
export interface FileType {
  id: string;
  post: string;
  originalfilename: string;
  newfilename: string;
  picturepath: string;
}

export interface FilesList {
  filesList: FileType[];
}

Lee Soobeom's avatar
Lee Soobeom committed
23
24
25
export function IntoPost() {
  const location = useLocation() as PostState;
  const post = location.state;
Lee Soobeom's avatar
Lee Soobeom committed
26
  const navigate = useNavigate();
Lee Soobeom's avatar
Lee Soobeom committed
27
  const [filesList, setFilesList] = useState<FileType[]>();
Lee Soobeom's avatar
Lee Soobeom committed
28
29
  // console.log(post);

Lee Soobeom's avatar
Lee Soobeom committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [addSuccess, setAddSuccess] = useState(false);
  const [delSuccess, setDelSuccess] = useState(false);

  useEffect(() => {
    getFilesList(post._id);
  }, []);

  const getFilesList = async (postId: string) => {
    const res = await postApi.getFileByPostId(postId);
    setFilesList(res);
  };

Lee Soobeom's avatar
Lee Soobeom committed
44
  const handleDeleteClick = async (event: MouseEvent<HTMLButtonElement>) => {
Lee Soobeom's avatar
Lee Soobeom committed
45
46
47
48
49
50
51
52
53
54
55
56
57
    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
58
59
  };

Lee Soobeom's avatar
Lee Soobeom committed
60
61
62
  return (
    <div>
      <div>
Lee Soobeom's avatar
Lee Soobeom committed
63
        <div>
Lee Soobeom's avatar
Lee Soobeom committed
64
65
66
67
68
          <div className="flex flex-row h-8 gap-x-1 place-content-end">
            <div className="w-8">
              <button
                id={post._id}
                onClick={handleDeleteClick}
Lee Soobeom's avatar
Lee Soobeom committed
69
                className="border-2 border-sky-100 rounded-full h-8 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"
Lee Soobeom's avatar
Lee Soobeom committed
70
              >
Lee Soobeom's avatar
Lee Soobeom committed
71
                삭제
Lee Soobeom's avatar
Lee Soobeom committed
72
73
              </button>
            </div>
Lee Soobeom's avatar
Lee Soobeom committed
74
            <div className="w-8">
Lee Soobeom's avatar
Lee Soobeom committed
75
              <Link to="/edit" state={post}>
Lee Soobeom's avatar
Lee Soobeom committed
76
                <button className="border-2 border-sky-100 rounded-full h-8 w-8 text-xs transition delay-150 bg-white-400 hover:-translate-y-1 hover:scale-110 hover:bg-sky-300 duration-300">
Lee Soobeom's avatar
Lee Soobeom committed
77
78
                  수정
                </button>
Lee Soobeom's avatar
Lee Soobeom committed
79
80
              </Link>
            </div>
Lee Soobeom's avatar
Lee Soobeom committed
81
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
82
83
84
85
86
87
88
89
          <div className="flex flex-col h-16 md:h-8 md:flex-row">
            <div className="flex basis-1/2 place-content-between h-8">
              <div className="flex basis-1/2 border-2 border-sky-200 rounded h-8">
                작성자: {post.user.slice(0, 8)}
              </div>
              <div className="flex basis-1/2 border-2 border-sky-200 rounded h-8">
                작성일: {post.date.slice(0, 10)}
              </div>
Lee Soobeom's avatar
Lee Soobeom committed
90
            </div>
Lee Soobeom's avatar
Lee Soobeom committed
91
92
93
94
95
96
97
98
99
100
            <div className="flex basis-1/2 place-content-between h-8">
              <div className="flex basis-1/3 border-2 border-sky-300 rounded">
                도시: {post.city}
              </div>
              <div className="flex basis-1/3 border-2 border-sky-300 rounded">
                테마: {post.theme}
              </div>
              <div className="flex basis-1/3 border-2 border-sky-300 rounded">
                조회수: {post.counts}
              </div>
Lee Soobeom's avatar
Lee Soobeom committed
101
            </div>
Lee Soobeom's avatar
Lee Soobeom committed
102
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
103
104
105
          <div className="flex flex-row h-8 gap-x-1 ">
            <div className="flex w-full border-2 border-sky-200 rounded">
              제목: {post.title}
Lee Soobeom's avatar
Lee Soobeom committed
106
            </div>
Lee Soobeom's avatar
Lee Soobeom committed
107
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
108
        </div>
Lee Soobeom's avatar
Lee Soobeom committed
109
110
111
112
113
114
115
116
117
118
119
120
121
        <div className="flex-row border-2 border-sky-400 rounded h-48 gap-x-2 ">
          <div className="flex gap-x-2 h-48 overflow-x-scroll">
            {filesList?.map((file, i) => (
              <img
                key={i}
                src={"http://localhost:3000/images/" + file.newfilename}
                width={200}
                height={200}
              />
            ))}
          </div>
        </div>
        <div className="border-2 border-sky-500 rounded h-96">{post.text}</div>
Lee Soobeom's avatar
Lee Soobeom committed
122
123
124
125
      </div>
    </div>
  );
}