"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Trash2, Loader2, RefreshCw } from "lucide-react"; 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 { 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 handleDeleteTemplate = async (templateId: string, templateName: string) => { if (!confirm(`"${templateName}" 템플릿을 삭제하시겠습니까?`)) { return; } setDeletingId(templateId); try { const response = await reportApi.deleteTemplate(templateId); 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) => (
)) )}
); }