"use client"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useParams, useRouter } from "next/navigation"; import { api } from "@/lib/api"; import type { DocumentTemplate } from "@/lib/types"; import { TEMPLATE_CATEGORY_LABELS } from "@/lib/types"; import { Breadcrumb } from "@/components/layout/Breadcrumb"; import { TemplateEditor } from "@/components/templates/TemplateEditor"; import Link from "next/link"; import { Loader2, Lock, Trash2, FileDown, ArrowRight, } from "lucide-react"; import { toast } from "sonner"; import { useState } from "react"; export default function TemplateDetailPage() { const { id } = useParams<{ id: string }>(); const router = useRouter(); const queryClient = useQueryClient(); const [isEditing, setIsEditing] = useState(false); const { data: template, isLoading } = useQuery({ queryKey: ["template", id], queryFn: () => api.get(`/templates/${id}`), }); const deleteMutation = useMutation({ mutationFn: () => api.delete(`/templates/${id}`), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["templates"] }); toast.success("Vorlage gelöscht"); router.push("/vorlagen"); }, onError: () => toast.error("Fehler beim Löschen"), }); const updateMutation = useMutation({ mutationFn: (data: Partial) => api.put(`/templates/${id}`, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["template", id] }); queryClient.invalidateQueries({ queryKey: ["templates"] }); toast.success("Vorlage gespeichert"); setIsEditing(false); }, onError: () => toast.error("Fehler beim Speichern"), }); if (isLoading) { return (
); } if (!template) { return (
Vorlage nicht gefunden
); } return (

{template.name}

{template.is_system && ( )}
{TEMPLATE_CATEGORY_LABELS[template.category] ?? template.category} {template.description && ( {template.description} )}
Dokument erstellen {!template.is_system && ( <> )}
{isEditing ? ( updateMutation.mutate(data)} isSaving={updateMutation.isPending} /> ) : (
{/* Variables */} {template.variables && template.variables.length > 0 && (

Variablen

{template.variables.map((v: string) => ( {`{{${v}}}`} ))}
)} {/* Content preview */}

Vorschau

{template.content}
)}
); }