import React, { FormEvent, useEffect, useState, MouseEvent } from "react"; import { Link } from "react-router-dom"; import { mainimgApi } from "../apis"; import { MainimgType } from "../types"; import { picture } from "../apis/profile.api"; import { catchErrors } from "../helpers"; import { MySlide } from "./adminslide"; // export interface ImgState { // state: MainimgType; // } export default function Admin() { // 이미지 전체 불러오기 const [getimgs, setGetimgs] = useState([]); async function imgsData() { const imgs = await mainimgApi.getmainimg(); setGetimgs(imgs) }; useEffect(() => { imgsData(); }, []); // 이미지 추가하기 const [addimg, setAddimg] = useState({ _id: "", theme: "", city: "", title: "", pic: { originalfilename: "", newfilename: "" }, }); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [addSuccess, setAddSuccess] = useState(false); const [delSuccess, setDelSuccess] = useState(false); const [file, setFile] = useState(); function handleSelectChange(event: React.ChangeEvent) { const { name, value } = event.currentTarget; console.log(value) setAddimg({ ...addimg, [name]: value }); } function handleInputeChange(event: React.ChangeEvent) { const { name, value } = event.currentTarget; setAddimg({ ...addimg, [name]: value }); }; function handleFileChange(e: React.ChangeEvent) { const file = e.target.files?.[0]; if (!(file === undefined)) { setFile(file); } } async function handleSubmit(event: FormEvent) { event.preventDefault(); const formdata = new FormData(); console.log(addimg); formdata.append("city", addimg.city); formdata.append("theme", addimg.theme); formdata.append("title", addimg.title); if (!(file === undefined)) { formdata.append("mainimg", file); console.log(formdata); const res = await mainimgApi.mainimg(formdata); console.log("확인 중 ", res); alert("img 추가되었습니다"); }; }; // 이미지 삭제하기 async function handleDeleteClick(event: MouseEvent) { try { if (confirm("삭제하시겠습니까?") == true) { const picId = event.currentTarget.id; console.log("picId : ", picId) const res = await mainimgApi.delmainimg(picId); console.log("delete img", res); // setGetimgs(getimgs) setDelSuccess(true); setError(""); alert("img 삭제되었습니다"); } else { return false; } } catch (error) { console.log("에러발생"); catchErrors(error, setError); } finally { setLoading(false); }; }; let limit = 15; const numPages = Math.ceil(getimgs.length / 15); // const location = useLocation() as ImgState; // const img = location.state; const slides = [] for (let i = 0; i < numPages; i++) { const k = [ getimgs.slice(i * limit, i * limit + limit).map((picture, index: number) => (

{picture.title}

))] slides.push(k); } return (

title :

); };