"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Trash2, Loader2, RefreshCw } from "lucide-react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { useReportDesigner } from "@/contexts/ReportDesignerContext"; import { reportApi } from "@/lib/api/reportApi"; import { useToast } from "@/hooks/use-toast"; interface Template { template_id: string; template_name_kor: string; template_name_eng: string | null; is_system: string; } export function TemplatePalette() { const { applyTemplate } = useReportDesigner(); const [customTemplates, setCustomTemplates] = useState([]); const [isLoading, setIsLoading] = useState(false); const [deletingId, setDeletingId] = useState(null); const [deleteTarget, setDeleteTarget] = useState<{ id: string; name: string } | null>(null); const { toast } = useToast(); const fetchTemplates = async () => { setIsLoading(true); try { const response = await reportApi.getTemplates(); if (response.success && response.data) { setCustomTemplates(Array.isArray(response.data.custom) ? response.data.custom : []); } } catch (error) { console.error("템플릿 조회 실패:", error); toast({ title: "오류", description: "템플릿 목록을 불러올 수 없습니다.", variant: "destructive", }); } finally { setIsLoading(false); } }; useEffect(() => { fetchTemplates(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleApplyTemplate = async (templateId: string) => { await applyTemplate(templateId); }; const handleDeleteClick = (templateId: string, templateName: string) => { setDeleteTarget({ id: templateId, name: templateName }); }; const handleDeleteConfirm = async () => { if (!deleteTarget) return; setDeletingId(deleteTarget.id); setDeleteTarget(null); try { const response = await reportApi.deleteTemplate(deleteTarget.id); if (response.success) { toast({ title: "성공", description: "템플릿이 삭제되었습니다.", }); fetchTemplates(); } } catch (error: any) { toast({ title: "오류", description: error.response?.data?.message || "템플릿 삭제에 실패했습니다.", variant: "destructive", }); } finally { setDeletingId(null); } }; return (
{/* 사용자 정의 템플릿 */}
{isLoading ? (
) : customTemplates.length === 0 ? (

저장된 템플릿이 없습니다

) : ( customTemplates.map((template) => (
)) )}
{/* 삭제 확인 모달 */} !open && setDeleteTarget(null)}> 템플릿 삭제 "{deleteTarget?.name}" 템플릿을 삭제하시겠습니까?
삭제된 템플릿은 복구할 수 없습니다.
취소 삭제
); }