Open seunghee1108 opened 9 months ago
수정본 입니다 "use client";
import React, { useState, useEffect, useCallback, ChangeEvent } from "react";
interface BoardInfo { titleKey: string; adddate: string; username: string; password: string; title: string; content: string; reply: string; }
export default function Page() { const [boards, setBoards] = useState<BoardInfo[]>([]); const [pageInfo, setPageInfo] = useState({ currentPage: 1, pageSize: 10, totalPages: 1, });
const pageSize = 10;
const [showForm, setShowForm] = useState(false);
const [boardInfo, setBoardInfo] = useState
const [selectedBoard, setSelectedBoard] = useState<BoardInfo | null>(null);
const [editedReply, setEditedReply] = useState<{
}>({});
// 서버에서 게시판 데이터를 가져오는 함수
const fetchData = useCallback(async (page: number) => {
try {
let apiUrl = /api/qna?page=${page}&pageSize=${pageSize}
;
const response = await fetch(apiUrl);
const data = await response.json();
setBoards(data.boards);
setPageInfo({
currentPage: data.pageInfo.currentPage,
pageSize: data.pageInfo.pageSize,
totalPages: data.pageInfo.totalPages,
});
} catch (error) {
console.error("사용자 정보를 가져오는 중 오류 발생:", error);
}
}, []);
const handleRowClick = (board: BoardInfo) => { setSelectedBoard(board); };
const handleModalClose = () => { setSelectedBoard(null); };
const handlePageChange = (newPage: number) => { setPageInfo({ ...pageInfo, currentPage: newPage, }); };
const handleReplyEdit = async (username: string) => {
try {
await fetch(/api/updateReply/${username}
, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ reply: editedReply[username] }),
});
// 수정 후 데이터 다시 불러오기
fetchData(pageInfo.currentPage);
setEditedReply((prev) => ({ ...prev, [username]: "" }));
} catch (error) {
console.error("Error updating reply:", error);
}
};
const formatDateTime = (datetime: string) => { const dateTime = new Date(datetime); const options = { year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", hour12: false, }; const dateTimeString = dateTime.toLocaleString(); return dateTimeString; };
useEffect(() => { fetchData(pageInfo.currentPage); }, [fetchData, pageInfo.currentPage]);
return (
titleKey | adddate | username | title | reply | Actions |
---|---|---|---|---|---|
{board.titleKey} | {formatDateTime(board.adddate)} | {board.username} | {board.title} | {board.reply} |
); }
admin/app/qna/page.tsx