import React, { FormEvent, useState} 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, url: img.url, title: img.title, }); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [disabled, setDisabled] = useState(false); const [success, setSuccess] = useState(false); async function reWriteSubmit(event: FormEvent) { event.preventDefault(); try { if (confirm("수정하시겠습니까?") == true) { 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(""); } } else { return false; } } catch (error) { console.log("에러발생"); catchErrors(error, setError); } finally { setLoading(false); } } // console.log(user._id) function infoFormMatch(pic: MainimgType) { if (!isLength(pic.title ?? "", { min: 1 })) { setError("제목을 입력해 주세요."); return false; } else if (!isLength(pic.url ?? "", { min: 1 })) { setError("url을 입력해 주세요."); 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 urlChange = (event: React.ChangeEvent) => { const url = event.currentTarget.value; const newUser = { ...imgdata, url: url }; setUrl(event.currentTarget.value); setImgData(newUser); }; return (
도시이름
테마이름
url
title
); };