import React, { FormEvent, useState } from "react"; import { useNavigate } 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 { File } from "formidable"; export default function Posting() { const [city, setCity] = useState("질문종류"); const [theme, setTheme] = useState("질문종류"); const [title, setTitle] = useState(""); const [text, setText] = useState(""); const [file, setFile] = useState(); const [imgSrc, setImgSrc] = useState(); const navigate = useNavigate(); const [user, setUser] = useState({ title: "", text: "", theme: "", city: "", date: "", user: "", counts: 0, _id: "", }); const imgArr = new Array(); const img2Url = (fileBlob: Blob) => { const reader = new FileReader(); reader.readAsDataURL(fileBlob); reader.onload = (data) => { if (typeof data.target?.result === "string") { console.log(data.target?.result); imgArr.push(data.target?.result); // setImgSrc(data.target?.result); } }; }; const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [disabled, setDisabled] = useState(false); const [success, setSuccess] = useState(false); async function handlePostSubmit(event: FormEvent) { event.preventDefault(); try { setError(""); console.log("user data", user); if (postingFormMatch()) { setLoading(true); const res = await postApi.posting(user); console.log("서버연결됬나요", res); // console.log("user save"); navigate("/board", { replace: true }); setSuccess(true); setError(""); } } catch (error) { console.log("에러발생"); catchErrors(error, setError); } finally { setLoading(false); } } function postingFormMatch() { if (!isLength(user.title ?? "", { min: 1 })) { setError("제목을 입력해 주세요."); return false; } else if (!isLength(user.text ?? "", { min: 1 })) { setError("내용을 입력해 주세요."); return false; } else if (equals(city, "질문종류")) { setError("테마를 선택해 주세요."); return false; } else if (equals(theme, "질문종류")) { 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); }; const handleInputPic = (event: React.ChangeEvent) => { // const fileArr = new FileList; const maxImg = 5; const fileArr = event.target.files; if (!(fileArr?.length === undefined)) { if (!(fileArr?.length > maxImg)) { for (var i = 0; i < maxImg; i++) { // setFile(file); console.log(fileArr[i]); img2Url(fileArr[i]); } setImgSrc(imgArr); } else { alert("사진은 최대 5장까지 업로드 가능합니다!"); } } }; return (

Id

empty

{/* {imgSrc?.map((img, i) => (
))} */} {/* */}
); }