Commit 1574f869 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

admin 권한 추가

parent ed7b303c
import { get } from "mongoose";
import React, { FormEvent, useEffect, useState, MouseEvent } from "react"; import React, { FormEvent, useEffect, useState, MouseEvent } from "react";
import { Link } from "react-router-dom"; import { Link, Navigate } from "react-router-dom";
import { mainimgApi } from "../apis"; import { mainimgApi } from "../apis";
// import { profileUpload } from "../apis/profile.api";
import { catchErrors } from "../helpers"; import { catchErrors } from "../helpers";
import { MainimgType } from "../types"; import { MainimgType } from "../types";
import { MySlide } from "./adminslide"; import { MySlide } from "./adminslide";
import { useAuth } from "./auth.context";
// export interface ImgState {
// state: MainimgType;
// }
export default function Admin() { export default function Admin() {
// 이미지 전체 불러오기 // 이미지 전체 불러오기
const [getimgs, setGetimgs] = useState<MainimgType[]>([]); const [getimgs, setGetimgs] = useState<MainimgType[]>([]);
async function imgsData() {
const imgs = await mainimgApi.getmainimg();
setGetimgs(imgs);
}
useEffect(() => {
imgsData();
}, []);
// 이미지 추가하기 // 이미지 추가하기
const [addimg, setAddimg] = useState<MainimgType>({ const [addimg, setAddimg] = useState<MainimgType>({
_id: "", _id: "",
...@@ -30,12 +17,25 @@ export default function Admin() { ...@@ -30,12 +17,25 @@ export default function Admin() {
title: "", title: "",
fileInfo: { originalfilename: "", newfilename: "" }, fileInfo: { originalfilename: "", newfilename: "" },
}); });
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(""); const [error, setError] = useState("");
const [addSuccess, setAddSuccess] = useState(false); const [addSuccess, setAddSuccess] = useState(false);
const [delSuccess, setDelSuccess] = useState(false); const [delSuccess, setDelSuccess] = useState(false);
const [file, setFile] = useState<File>(); const [file, setFile] = useState<File>();
const { user } = useAuth();
if (user.role !== "admin") {
alert("이용 권한이 없습니다.");
return <Navigate to={"/"} replace />;
}
async function imgsData() {
const imgs = await mainimgApi.getmainimg();
setGetimgs(imgs);
}
useEffect(() => {
imgsData();
}, []);
function handleSelectChange(event: React.ChangeEvent<HTMLSelectElement>) { function handleSelectChange(event: React.ChangeEvent<HTMLSelectElement>) {
const { name, value } = event.currentTarget; const { name, value } = event.currentTarget;
...@@ -68,11 +68,8 @@ export default function Admin() { ...@@ -68,11 +68,8 @@ export default function Admin() {
const res = await mainimgApi.mainimg(formdata); const res = await mainimgApi.mainimg(formdata);
console.log("확인 중 ", res); console.log("확인 중 ", res);
alert("img 추가되었습니다"); alert("img 추가되었습니다");
setGetimgs([...getimgs, res]) setGetimgs([...getimgs, res]);
} } else console.log("file === undefined");
else (
console.log("file === undefined")
)
} catch (error) { } catch (error) {
console.log("에러발생"); console.log("에러발생");
catchErrors(error, setError); catchErrors(error, setError);
...@@ -81,7 +78,6 @@ export default function Admin() { ...@@ -81,7 +78,6 @@ export default function Admin() {
} }
} }
// 이미지 삭제하기 // 이미지 삭제하기
async function handleDeleteClick(event: MouseEvent<HTMLButtonElement>) { async function handleDeleteClick(event: MouseEvent<HTMLButtonElement>) {
try { try {
...@@ -94,8 +90,8 @@ export default function Admin() { ...@@ -94,8 +90,8 @@ export default function Admin() {
setDelSuccess(true); setDelSuccess(true);
setError(""); setError("");
alert("img 삭제되었습니다"); alert("img 삭제되었습니다");
const deleteimg = getimgs.filter(pic=>picId!==pic._id) const deleteimg = getimgs.filter((pic) => picId !== pic._id);
setGetimgs(deleteimg) setGetimgs(deleteimg);
} else { } else {
return false; return false;
} }
...@@ -110,9 +106,6 @@ export default function Admin() { ...@@ -110,9 +106,6 @@ export default function Admin() {
let limit = 15; let limit = 15;
const numPages = Math.ceil(getimgs.length / 15); const numPages = Math.ceil(getimgs.length / 15);
// const location = useLocation() as ImgState;
// const img = location.state;
const slides = []; const slides = [];
for (let i = 0; i < numPages; i++) { for (let i = 0; i < numPages; i++) {
const k = [ const k = [
...@@ -122,7 +115,9 @@ export default function Admin() { ...@@ -122,7 +115,9 @@ export default function Admin() {
<div key={index}> <div key={index}>
<div className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}> <div className={`m-1 shrink-0 bg-gray-200 rounded shadow-md `}>
<img <img
src={"http://localhost:3000/images/" + picture.fileInfo.newfilename} src={
"http://localhost:3000/images/" + picture.fileInfo.newfilename
}
className="w-full h-10 md:h-20 object-center" className="w-full h-10 md:h-20 object-center"
/> />
<p className="text-center text-xs">{picture.title}</p> <p className="text-center text-xs">{picture.title}</p>
......
...@@ -39,7 +39,7 @@ export default function Header() { ...@@ -39,7 +39,7 @@ export default function Header() {
프로필 프로필
</Link> </Link>
<div className="border-0 border-r-2 border-black "></div> <div className="border-0 border-r-2 border-black "></div>
<div></div> {user.role === "admin" ? <Link to={"/admin"}>어드민</Link> : null}
<button <button
className="ml-2 text-xs" className="ml-2 text-xs"
onClick={() => { onClick={() => {
......
...@@ -4,6 +4,7 @@ export interface IUser { ...@@ -4,6 +4,7 @@ export interface IUser {
email?: string; email?: string;
isLoggedIn: boolean; isLoggedIn: boolean;
_id?: string; _id?: string;
role?: string;
} }
export interface LoginUser { export interface LoginUser {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment