editpost.tsx 7 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
2
3
4
5
6
7
8
9
10
import React, { FormEvent, useState } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import isLength from "validator/lib/isLength";
import equals from "validator/lib/equals";
import { catchErrors } from "../helpers";
import { PostType } from "../types";
import { postApi } from "../apis";
import { PostState } from "./intopost";

export function EditPost() {
Lee Soobeom's avatar
Lee Soobeom committed
11
12
  const [city, setCity] = useState<string>("city");
  const [theme, setTheme] = useState<string>("theme");
Lee Soobeom's avatar
Lee Soobeom committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  const [title, setTitle] = useState<string>("");
  const [text, setText] = useState<string>("");
  const navigate = useNavigate();
  const location = useLocation() as PostState;

  const post = location.state;

  const [user, setUser] = useState<PostType>({
    title: post.title,
    text: post.text,
    theme: post.theme,
    city: post.city,
    date: post.date,
    user: post.user,
    counts: 0,
    _id: post._id,
  });

  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
  const [disabled, setDisabled] = useState(false);
  const [success, setSuccess] = useState(false);

Lee Soobeom's avatar
Lee Soobeom committed
36
  async function reWriteSubmit(event: FormEvent) {
Lee Soobeom's avatar
Lee Soobeom committed
37
38
39
40
41
42
    event.preventDefault();
    try {
      setError("");

      console.log("user data", user);

Lee Soobeom's avatar
Lee Soobeom committed
43
      if (postingFormMatch(user) === true) {
Lee Soobeom's avatar
Lee Soobeom committed
44
45
        setLoading(true);
        const res = await postApi.updating(user);
Lee Soobeom's avatar
Lee Soobeom committed
46
47

        console.log("clear", res);
Lee Soobeom's avatar
Lee Soobeom committed
48
49
50
51
52
53
54
55
56
57
58
59
        navigate("/board", { replace: true });
        setSuccess(true);
        setError("");
      }
    } catch (error) {
      console.log("에러발생");
      catchErrors(error, setError);
    } finally {
      setLoading(false);
    }
  }

Lee Soobeom's avatar
Lee Soobeom committed
60
  function postingFormMatch(user: PostType) {
Lee Soobeom's avatar
Lee Soobeom committed
61
62
63
64
65
66
    if (!isLength(user.title ?? "", { min: 1 })) {
      setError("제목을 입력해 주세요.");
      return false;
    } else if (!isLength(user.text ?? "", { min: 1 })) {
      setError("내용을 입력해 주세요.");
      return false;
Lee Soobeom's avatar
Lee Soobeom committed
67
    } else if (equals(city, "city")) {
Lee Soobeom's avatar
Lee Soobeom committed
68
      setError("도시를 선택해 주세요.");
Lee Soobeom's avatar
Lee Soobeom committed
69
      return false;
Lee Soobeom's avatar
Lee Soobeom committed
70
    } else if (equals(theme, "theme")) {
Lee Soobeom's avatar
Lee Soobeom committed
71
      setError("테마를 선택해 주세요.");
Lee Soobeom's avatar
Lee Soobeom committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      return false;
    } else {
      return true;
    }
  }

  const titleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
    const title = event.currentTarget.value;
    const newUser = { ...user, title: title };
    console.log(event.currentTarget.value);
    setTitle(event.currentTarget.value);
    setUser(newUser);
  };

  const textChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
    const text = event.currentTarget.value;
    const newUser = { ...user, text: text };
    console.log(event.currentTarget.value);
    setText(event.currentTarget.value);
    setUser(newUser);
  };

  const cityChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    const city = event.currentTarget.value;
    const newUser = { ...user, city: city };
    console.log(event.currentTarget.value);
    setCity(event.currentTarget.value);
    setUser(newUser);
  };

  const themeChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
    const theme = event.currentTarget.value;
    const newUser = { ...user, theme: theme };
    console.log(event.currentTarget.value);
    setTheme(event.currentTarget.value);
    setUser(newUser);
  };

  return (
Lee Soobeom's avatar
Lee Soobeom committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
    <div className="flex place-content-center">
      <form
        onSubmit={reWriteSubmit}
        className="flex flex-col w-96 items-center"
      >
        <div className="flex flex-row h-8 gap-x-1">
          <div className="flex border-2 border-sky-400 rounded-full w-20 place-content-center transition ease-in-out delay-150 bg-white-400 hover:-translate-y-1 hover:scale-110 hover:bg-gray-300 duration-300">
            <input
              id="files"
              type="file"
              multiple
              // onChange={handleInputPic}
              className="hidden"
            />
            <label htmlFor="files" className="text-xs mt-1.5 ml-1 ">
              파일 선택
            </label>
          </div>

          <div className="flex w-20">
            <select
              name="city"
              className="border-2 border-sky-400 rounded-full w-20 text-xs"
              onChange={cityChange}
              defaultValue="질문종류"
            >
              <option value="질문종류">도시</option>
              <option value="Seoul">서울</option>
              <option value="Busan">부산</option>
              <option value="Incheon">인천</option>
              <option value="Daegoo">대구</option>
              <option value="Kwangjoo">광주</option>
              <option value="Daejeon">대전</option>
              <option value="Woolsan">울산</option>
              <option value="Sejong">세종</option>
              <option value="Dokdo">독도</option>
              <option value="Jeju">제주</option>
            </select>
          </div>

          <div className="flex w-20">
            <select
              name="theme"
              className="border-2 border-sky-400 rounded-full w-20 text-xs"
              onChange={themeChange}
              defaultValue="질문종류"
            >
              <option value="질문종류">테마</option>
              <option value="cycling">사이클링</option>
              <option value="surfing">서핑</option>
              <option value="activity">액티비티</option>
              <option value="camping">캠핑</option>
              <option value="sking">스키</option>
              <option value="boat">보트</option>
              <option value="desert">사막</option>
              <option value="golf">골프</option>
              <option value="cave">동굴</option>
              <option value="history">문화재</option>
              <option value="zoo">동물원</option>
              <option value="cycling">사이클링</option>
            </select>
          </div>

          <div className="flex w-20">
            <button
              type="submit"
              className="border-2 border-sky-400 rounded-full w-20 text-xs text-center transition ease-in-out delay-150 bg-white-400 hover:-translate-y-1 hover:scale-110 hover:bg-sky-300 duration-300"
            >
              글쓰기
            </button>
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
182
183
        </div>

Lee Soobeom's avatar
Lee Soobeom committed
184
        <div className="flex flex-col">
Lee Soobeom's avatar
Lee Soobeom committed
185
186
187
          <textarea
            name="title"
            onChange={titleChange}
Lee Soobeom's avatar
Lee Soobeom committed
188
189
190
191
192
193
194
195
196
197
            placeholder="제목을 입력해 주세요!"
            className="flex w-96 border-2 border-sky-500 rounded"
          />
          <div className="flex flex-col mt-1 mb-1">
            <div className="flex gap-x-2 h-48 overflow-x-scroll">
              {/* {imgSrc?.map((img, i) => (
                <img key={i} src={img} width={200} height={200} />
              ))} */}
            </div>
          </div>
Lee Soobeom's avatar
Lee Soobeom committed
198
199
200
          <textarea
            onChange={textChange}
            name="text"
Lee Soobeom's avatar
Lee Soobeom committed
201
202
203
            placeholder="여행 후기를 알려주세요!"
            className="flex w-96 h-96 border-2 border-sky-500 rounded"
          />
Lee Soobeom's avatar
Lee Soobeom committed
204
205
206
207
208
        </div>
      </form>
    </div>
  );
}