강좌 수정 삭제 작업중1
This commit is contained in:
@@ -1,21 +1,103 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useMemo } from "react";
|
||||
import { useState, useMemo, useRef, ChangeEvent } from "react";
|
||||
import AdminSidebar from "@/app/components/AdminSidebar";
|
||||
import ChevronDownSvg from "@/app/svgs/chevrondownsvg";
|
||||
import BackArrowSvg from "@/app/svgs/backarrow";
|
||||
import { MOCK_RESOURCES, type Resource } from "@/app/admin/resources/mockData";
|
||||
import apiService from "@/app/lib/apiService";
|
||||
|
||||
export default function AdminResourcesPage() {
|
||||
// TODO: 나중에 실제 데이터로 교체
|
||||
const items: any[] = [];
|
||||
const [resources, setResources] = useState<Resource[]>(MOCK_RESOURCES);
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const [isWritingMode, setIsWritingMode] = useState(false);
|
||||
const [title, setTitle] = useState('');
|
||||
const [content, setContent] = useState('');
|
||||
const [attachedFile, setAttachedFile] = useState<File | null>(null);
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const totalCount = useMemo(() => resources.length, [resources]);
|
||||
|
||||
const characterCount = useMemo(() => content.length, [content]);
|
||||
|
||||
const handleBack = () => {
|
||||
setIsWritingMode(false);
|
||||
setTitle('');
|
||||
setContent('');
|
||||
setAttachedFile(null);
|
||||
};
|
||||
|
||||
const handleFileAttach = () => {
|
||||
fileInputRef.current?.click();
|
||||
};
|
||||
|
||||
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
if (file.size > 30 * 1024 * 1024) {
|
||||
alert('30MB 미만의 파일만 첨부할 수 있습니다.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 단일 파일 업로드
|
||||
await apiService.uploadFile(file);
|
||||
setAttachedFile(file);
|
||||
} catch (error) {
|
||||
console.error('파일 업로드 실패:', error);
|
||||
alert('파일 업로드에 실패했습니다. 다시 시도해주세요.');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
if (!title.trim()) {
|
||||
alert('제목을 입력해주세요.');
|
||||
return;
|
||||
}
|
||||
if (!content.trim()) {
|
||||
alert('내용을 입력해주세요.');
|
||||
return;
|
||||
}
|
||||
|
||||
// 새 학습 자료 추가
|
||||
const newResource: Resource = {
|
||||
id: resources.length > 0 ? Math.max(...resources.map(r => r.id)) + 1 : 1,
|
||||
title: title.trim(),
|
||||
date: new Date().toISOString().split('T')[0],
|
||||
views: 0,
|
||||
writer: '관리자', // TODO: 실제 작성자 정보 사용
|
||||
content: content.split('\n'),
|
||||
hasAttachment: attachedFile !== null,
|
||||
};
|
||||
|
||||
setResources([newResource, ...resources]);
|
||||
handleBack();
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
if (title.trim() || content.trim() || attachedFile) {
|
||||
if (confirm('작성 중인 내용이 있습니다. 정말 취소하시겠습니까?')) {
|
||||
handleBack();
|
||||
}
|
||||
} else {
|
||||
handleBack();
|
||||
}
|
||||
};
|
||||
|
||||
const ITEMS_PER_PAGE = 10;
|
||||
const totalPages = Math.ceil(items.length / ITEMS_PER_PAGE);
|
||||
const paginatedItems = useMemo(() => {
|
||||
const sortedResources = useMemo(() => {
|
||||
return [...resources].sort((a, b) => {
|
||||
// 생성일 내림차순 정렬 (최신 날짜가 먼저)
|
||||
return b.date.localeCompare(a.date);
|
||||
});
|
||||
}, [resources]);
|
||||
|
||||
const totalPages = Math.ceil(sortedResources.length / ITEMS_PER_PAGE);
|
||||
const paginatedResources = useMemo(() => {
|
||||
const startIndex = (currentPage - 1) * ITEMS_PER_PAGE;
|
||||
const endIndex = startIndex + ITEMS_PER_PAGE;
|
||||
return items.slice(startIndex, endIndex);
|
||||
}, [items, currentPage]);
|
||||
return sortedResources.slice(startIndex, endIndex);
|
||||
}, [sortedResources, currentPage]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex flex-col bg-white">
|
||||
@@ -30,27 +112,219 @@ export default function AdminResourcesPage() {
|
||||
{/* 메인 콘텐츠 */}
|
||||
<main className="w-[1120px] bg-white">
|
||||
<div className="h-full flex flex-col px-8">
|
||||
{/* 제목 영역 */}
|
||||
<div className="h-[100px] flex items-center">
|
||||
<h1 className="text-[24px] font-bold leading-[1.5] text-[#1b2027]">
|
||||
학습 자료실
|
||||
</h1>
|
||||
</div>
|
||||
{isWritingMode ? (
|
||||
<>
|
||||
{/* 작성 모드 헤더 */}
|
||||
<div className="h-[100px] flex items-center">
|
||||
<div className="flex gap-3 items-center">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleBack}
|
||||
className="flex items-center justify-center w-8 h-8 cursor-pointer"
|
||||
aria-label="뒤로가기"
|
||||
>
|
||||
<BackArrowSvg width={32} height={32} />
|
||||
</button>
|
||||
<h1 className="text-[24px] font-bold leading-[1.5] text-[#1b2027]">
|
||||
학습 자료 작성
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 콘텐츠 영역 */}
|
||||
<div className="flex-1 pt-8 flex flex-col">
|
||||
{items.length === 0 ? (
|
||||
{/* 작성 폼 */}
|
||||
<div className="flex-1 flex flex-col gap-10 pb-20 pt-8 w-full">
|
||||
<div className="flex flex-col gap-6 w-full">
|
||||
{/* 제목 입력 */}
|
||||
<div className="flex flex-col gap-2 items-start justify-center w-full">
|
||||
<label className="text-[15px] font-semibold leading-[1.5] text-[#6c7682] w-[100px]">
|
||||
제목
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
placeholder="제목을 입력해 주세요."
|
||||
className="w-full h-[40px] px-3 py-2 rounded-[8px] border border-[#dee1e6] bg-white text-[16px] font-normal leading-[1.5] text-[#1b2027] placeholder:text-[#b1b8c0] focus:outline-none focus:ring-2 focus:ring-[#1f2b91] focus:border-transparent"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 내용 입력 */}
|
||||
<div className="flex flex-col gap-2 items-start justify-center w-full">
|
||||
<label className="text-[15px] font-semibold leading-[1.5] text-[#6c7682] w-[100px]">
|
||||
내용
|
||||
</label>
|
||||
<div className="relative w-full">
|
||||
<textarea
|
||||
value={content}
|
||||
onChange={(e) => {
|
||||
const newContent = e.target.value;
|
||||
if (newContent.length <= 1000) {
|
||||
setContent(newContent);
|
||||
}
|
||||
}}
|
||||
placeholder="내용을 입력해 주세요. (최대 1,000자 이내)"
|
||||
className="w-full h-[320px] px-3 py-2 rounded-[8px] border border-[#dee1e6] bg-white text-[16px] font-normal leading-[1.5] text-[#1b2027] placeholder:text-[#b1b8c0] resize-none focus:outline-none focus:ring-2 focus:ring-[#1f2b91] focus:border-transparent"
|
||||
/>
|
||||
<div className="absolute bottom-3 right-3">
|
||||
<p className="text-[13px] font-normal leading-[1.4] text-[#6c7682] text-right">
|
||||
{characterCount}/1000
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 첨부 파일 */}
|
||||
<div className="flex flex-col gap-2 items-start justify-center w-full">
|
||||
<div className="flex items-center justify-between h-8 w-full">
|
||||
<div className="flex items-center gap-3 flex-1 min-w-0">
|
||||
<label className="text-[15px] font-semibold leading-[1.5] text-[#6c7682] whitespace-nowrap">
|
||||
첨부 파일{' '}
|
||||
<span className="font-normal">
|
||||
({attachedFile ? 1 : 0}/1)
|
||||
</span>
|
||||
</label>
|
||||
<p className="text-[13px] font-normal leading-[1.4] text-[#8c95a1] whitespace-nowrap">
|
||||
30MB 미만 파일
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleFileAttach}
|
||||
className="h-[32px] w-[62px] px-[4px] py-[3px] rounded-[6px] border border-[#8c95a1] bg-white flex items-center justify-center cursor-pointer hover:bg-gray-50 transition-colors shrink-0"
|
||||
>
|
||||
<span className="text-[13px] font-medium leading-[1.4] text-[#4c5561] whitespace-nowrap">
|
||||
첨부
|
||||
</span>
|
||||
</button>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
onChange={handleFileChange}
|
||||
className="hidden"
|
||||
accept="*/*"
|
||||
/>
|
||||
</div>
|
||||
<div className="h-16 w-full rounded-[8px] border border-[#dee1e6] bg-gray-50 flex items-center justify-center">
|
||||
{attachedFile ? (
|
||||
<p className="text-[14px] font-normal leading-[1.5] text-[#1b2027]">
|
||||
{attachedFile.name}
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-[14px] font-normal leading-[1.5] text-[#8c95a1] text-center">
|
||||
파일을 첨부해주세요.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 액션 버튼 */}
|
||||
<div className="flex gap-3 items-center justify-end shrink-0 w-full">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCancel}
|
||||
className="h-12 px-8 rounded-[10px] bg-[#f1f3f5] text-[16px] font-semibold leading-[1.5] text-[#4c5561] whitespace-nowrap hover:bg-[#e5e8eb] transition-colors cursor-pointer"
|
||||
>
|
||||
취소
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSave}
|
||||
className="h-12 px-4 rounded-[10px] bg-[#1f2b91] text-[16px] font-semibold leading-[1.5] text-white whitespace-nowrap hover:bg-[#1a2478] transition-colors cursor-pointer"
|
||||
>
|
||||
저장하기
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
{/* 제목 영역 */}
|
||||
<div className="h-[100px] flex items-center">
|
||||
<h1 className="text-[24px] font-bold leading-[1.5] text-[#1b2027]">
|
||||
학습 자료실
|
||||
</h1>
|
||||
</div>
|
||||
|
||||
{/* 헤더 영역 (제목과 콘텐츠 사이) */}
|
||||
<div className="pt-2 pb-4 flex items-center justify-between">
|
||||
<p className="text-[15px] font-medium leading-[1.5] text-[#333c47] whitespace-nowrap">
|
||||
총 {totalCount}건
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setIsWritingMode(true)}
|
||||
className="h-[40px] px-4 rounded-[8px] bg-[#1f2b91] text-[16px] font-semibold leading-[1.5] text-white whitespace-nowrap hover:bg-[#1a2478] transition-colors cursor-pointer"
|
||||
>
|
||||
등록하기
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 콘텐츠 영역 */}
|
||||
<div className="flex-1 pt-2 flex flex-col">
|
||||
{resources.length === 0 ? (
|
||||
<div className="rounded-lg border border-[#dee1e6] bg-white min-h-[400px] flex items-center justify-center">
|
||||
<p className="text-[16px] font-medium leading-[1.5] text-[#333c47]">
|
||||
현재 관리할 수 있는 항목이 없습니다.
|
||||
등록된 학습 자료가 없습니다.
|
||||
<br />
|
||||
학습 자료를 등록해주세요.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{/* TODO: 테이블 또는 리스트를 여기에 추가 */}
|
||||
<div className="rounded-[8px]">
|
||||
<div className="w-full rounded-[8px] border border-[#dee1e6] overflow-visible">
|
||||
<table className="min-w-full border-collapse">
|
||||
<colgroup>
|
||||
<col style={{ width: 80 }} />
|
||||
<col />
|
||||
<col style={{ width: 140 }} />
|
||||
<col style={{ width: 120 }} />
|
||||
<col style={{ width: 120 }} />
|
||||
</colgroup>
|
||||
<thead>
|
||||
<tr className="h-12 bg-gray-50 text-left">
|
||||
<th className="border-r border-[#dee1e6] px-4 text-[14px] font-semibold leading-[1.5] text-[#4c5561]">번호</th>
|
||||
<th className="border-r border-[#dee1e6] px-4 text-[14px] font-semibold leading-[1.5] text-[#4c5561]">제목</th>
|
||||
<th className="border-r border-[#dee1e6] px-4 text-[14px] font-semibold leading-[1.5] text-[#4c5561]">게시일</th>
|
||||
<th className="border-r border-[#dee1e6] px-4 text-[14px] font-semibold leading-[1.5] text-[#4c5561]">조회수</th>
|
||||
<th className="px-4 text-[14px] font-semibold leading-[1.5] text-[#4c5561]">작성자</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{paginatedResources.map((resource, index) => {
|
||||
// 번호는 전체 목록에서의 순서 (정렬된 목록 기준)
|
||||
const resourceNumber = sortedResources.length - (currentPage - 1) * ITEMS_PER_PAGE - index;
|
||||
return (
|
||||
<tr
|
||||
key={resource.id}
|
||||
className="h-12 hover:bg-[#F5F7FF] transition-colors"
|
||||
>
|
||||
<td className="border-t border-r border-[#dee1e6] px-4 text-[13px] leading-[1.5] text-[#1b2027] whitespace-nowrap text-center">
|
||||
{resourceNumber}
|
||||
</td>
|
||||
<td className="border-t border-r border-[#dee1e6] px-4 text-[13px] leading-[1.5] text-[#1b2027]">
|
||||
{resource.title}
|
||||
</td>
|
||||
<td className="border-t border-r border-[#dee1e6] px-4 text-[13px] leading-[1.5] text-[#1b2027] whitespace-nowrap">
|
||||
{resource.date}
|
||||
</td>
|
||||
<td className="border-t border-r border-[#dee1e6] px-4 text-[13px] leading-[1.5] text-[#1b2027] whitespace-nowrap">
|
||||
{resource.views.toLocaleString()}
|
||||
</td>
|
||||
<td className="border-t border-[#dee1e6] px-4 text-[13px] leading-[1.5] text-[#1b2027] whitespace-nowrap">
|
||||
{resource.writer}
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 페이지네이션 - 10개 초과일 때만 표시 */}
|
||||
{items.length > ITEMS_PER_PAGE && (() => {
|
||||
{resources.length > ITEMS_PER_PAGE && (() => {
|
||||
// 페이지 번호를 10단위로 표시
|
||||
const pageGroup = Math.floor((currentPage - 1) / 10);
|
||||
const startPage = pageGroup * 10 + 1;
|
||||
@@ -134,7 +408,9 @@ export default function AdminResourcesPage() {
|
||||
})()}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user