import React, { FormEvent, useState, useEffect } 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"; import { FileType } from "./intopost"; export function EditPost() { const [city, setCity] = useState("city"); const [theme, setTheme] = useState("theme"); const [title, setTitle] = useState(""); const [text, setText] = useState(""); const [file, setFile] = useState(); const [imgSrc, setImgSrc] = useState(); const [filesList, setFilesList] = useState(); const navigate = useNavigate(); const location = useLocation() as PostState; const post = location.state; const [user, setUser] = useState({ title: post.title, text: post.text, theme: post.theme, city: post.city, date: post.date, user: post.user, counts: post.counts, _id: post._id, }); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [disabled, setDisabled] = useState(false); const [success, setSuccess] = useState(false); useEffect(() => { getFilesList(post._id); }, []); const imgArr = new Array(); const getFilesList = async (postId: string) => { const res = await postApi.getFileByPostId(postId); //_id는 req.params에 항상 같이 보낸다 setFilesList(res); }; const updateImg2Db = async (filelist: FileList) => { const formdata = new FormData(); formdata.append("title", user.title); formdata.append("text", user.text); formdata.append("theme", user.theme); formdata.append("city", user.city); if (filelist === undefined || filelist === null) { const res = await postApi.updateImgAndPost(user._id, formdata); } else { for (var i = 0; i < filelist.length; i++) { formdata.append("picture", filelist?.[i]); } const res = await postApi.updateImgAndPost(user._id, formdata); } }; async function reWriteSubmit(event: FormEvent) { event.preventDefault(); try { if (confirm("게시물을 수정하시겠습니까?") == true) { setError(""); // console.log("user data", user); if (postingFormMatch(user) === true) { setLoading(true); if (file) { const res = updateImg2Db(file); // console.log(res); } navigate("/board", { replace: true }); setSuccess(true); setError(""); } } else { return false; } } catch (error) { console.log("에러발생"); catchErrors(error, setError); } finally { setLoading(false); } } const handleInputPic = async (event: React.ChangeEvent) => { const maxImg = 10; const { files } = event.target; if (!(files === null)) { setFile(files); } if (!(files?.length === undefined)) { if (files?.length <= maxImg) { for (var i = 0; i < files.length; i++) { const reader = new FileReader(); reader.readAsDataURL(files?.[i]); reader.onload = (e) => { imgArr.push(e.target?.result); setImgSrc(imgArr); }; } } else { alert(`사진은 최대 ${maxImg}장까지 업로드 가능합니다!`); } } }; function postingFormMatch(user: PostType) { if (!isLength(user.title ?? "", { min: 1 })) { setError("제목을 입력해 주세요."); alert("제목을 입력해 주세요."); return false; } else if (!isLength(user.text ?? "", { min: 1 })) { alert("내용을 입력해 주세요."); setError("내용을 입력해 주세요."); return false; } else if (equals(user.city, "city")) { alert("도시를 선택해 주세요."); setError("도시를 선택해 주세요."); return false; } else if (equals(user.theme, "theme")) { alert("테마를 선택해 주세요."); setError("테마를 선택해 주세요."); return false; } else { return true; } } const titleChange = (event: React.ChangeEvent) => { 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) => { 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) => { 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) => { const theme = event.currentTarget.value; const newUser = { ...user, theme: theme }; console.log(event.currentTarget.value); setTheme(event.currentTarget.value); setUser(newUser); }; return (