import React, { FormEvent, useState, useEffect } from "react"; import { Link, Outlet, useNavigate, useLocation } from "react-router-dom"; import { mainimgApi } from "../apis"; import isLength from "validator/lib/isLength"; import equals from "validator/lib/equals"; import { catchErrors } from "../helpers"; import { MainimgType } from "../types"; export interface ImgState { state: MainimgType; } export default function ImgRewrite() { // 이미지 수정 const [theme, setTheme] = useState("질문종류"); const [city, setCity] = useState("질문종류"); const [url, setUrl] = useState(""); const [title, setTitle] = useState(""); const navigate = useNavigate(); const location = useLocation() as ImgState; const img = location.state; const [imgdata, setImgData] = useState({ _id: img._id, theme: img.theme, city: img.city, title: img.title, fileInfo: { originalfilename: "", newfilename: "" }, }); useEffect(() => { console.log("수정 전 : ",imgdata); }, []); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [success, setSuccess] = useState(false); const [file, setFile] = useState(); const [imageSrc, setImageSrc] = useState(""); const PictureSrc = (fileBlob: Blob) => { const reader = new FileReader(); reader.readAsDataURL(fileBlob); reader.onload = (data) => { if (typeof data.target?.result === "string") { console.log(data.target?.result); setImageSrc(data.target?.result); } }; }; const onUploadFile = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!(file === undefined)) { setFile(file); PictureSrc(file); } }; async function reWriteSubmit(event: FormEvent) { event.preventDefault(); if (confirm("수정하시겠습니까?") == true) { // try { setError(""); console.log("user data", imgdata); // if (infoFormMatch(imgdata) === true) { // setLoading(true); // const res = await mainimgApi.updating(imgdata); // console.log("clear", res); // navigate("/admin", { replace: true }); // setSuccess(true); // setError(""); const formdata = new FormData(); formdata.append("id", imgdata._id) console.log(imgdata._id) formdata.append("city", imgdata.city); formdata.append("theme", imgdata.theme); formdata.append("title", imgdata.title); console.log(formdata) if (!(file === undefined)) { formdata.append("updatemainimg", file); console.log("formdata", formdata); const res = await mainimgApi.updateimg(formdata,imgdata._id); navigate("/admin", { replace: true }); console.log("확인 중 ", res); } else { formdata.append("updatemainimg",""); console.log("formdata",formdata); const res = await mainimgApi.updateimg(formdata,imgdata._id); navigate("/admin", { replace: true }); console.log("확인 중 ", res); } // } // } catch (error) { // console.log("에러발생"); // catchErrors(error, setError); // } finally { // setLoading(false); // } } else { return false; } } // console.log(user._id) function infoFormMatch(pic: MainimgType) { if (!isLength(pic.title ?? "", { min: 1 })) { setError("제목을 입력해 주세요."); return false; } else if (!isLength(pic.fileInfo.newfilename ?? "", { min: 1 })) { setError("파일을 선택해 주세요."); return false; } else if (equals(pic.city, "city")) { setError("도시를 선택해 주세요."); return false; } else if (equals(pic.theme, "theme")) { setError("테마를 선택해 주세요."); return false; } else { return true; } } const cityChange = (event: React.ChangeEvent) => { const city = event.currentTarget.value; const newUser = { ...imgdata, city: city }; console.log(event.currentTarget.value); setCity(event.currentTarget.value); setImgData(newUser); }; const themeChange = (event: React.ChangeEvent) => { const theme = event.currentTarget.value; const newUser = { ...imgdata, theme: theme }; console.log(event.currentTarget.value); setTheme(event.currentTarget.value); setImgData(newUser); }; const titleChange = (event: React.ChangeEvent) => { const title = event.currentTarget.value; const newUser = { ...imgdata, title: title }; setTitle(event.currentTarget.value); setImgData(newUser); }; const fileChange = (event: React.ChangeEvent) => { const url = event.currentTarget.value; const newUser = { ...imgdata, pic: { originalfilename: "", newfilename: url } }; setUrl(event.currentTarget.value); setImgData(newUser); }; return (
도시이름
테마이름
url {imageSrc ? ( ) : ( )}
title
); };