/** * AutomationTemplatesPage * * Page for managing automation templates (CRUD). * System templates (isSystem=true) are read-only for non-SysAdmin, with duplicate option. * Instance templates can be managed by instance admins/editors. */ import React, { useState, useMemo, useEffect } from 'react'; import { useAutomationTemplates, type AutomationTemplate } from '../../hooks/useAutomations'; import { FormGeneratorTable } from '../../components/FormGenerator/FormGeneratorTable'; import { AutomationEditor } from '../../components/AutomationEditor'; import { FaSync, FaPlus, FaFileAlt, FaCopy, FaLock } from 'react-icons/fa'; import { useToast } from '../../contexts/ToastContext'; import { useCurrentUser } from '../../hooks/useUsers'; import styles from '../admin/Admin.module.css'; export const AutomationTemplatesPage: React.FC = () => { const { templates, attributes, loading, error, permissions, refetch, createTemplate, updateTemplate, deleteTemplate, duplicateTemplate, getTemplate, } = useAutomationTemplates(); const { user: currentUser } = useCurrentUser(); const isSysAdmin = currentUser?.isSysAdmin || false; const { showSuccess, showError } = useToast(); // Editor states const [showEditor, setShowEditor] = useState(false); const [editingTemplate, setEditingTemplate] = useState(null); const [saving, setSaving] = useState(false); // Initial fetch useEffect(() => { refetch(); }, []); // Check permissions const canCreate = permissions?.create !== 'n'; const canUpdate = permissions?.update !== 'n'; const canDelete = permissions?.delete !== 'n'; // Table columns - FormGeneratorTable auto-renders TextMultilingual in user language const columns = useMemo(() => [ { key: 'label', label: 'Label', type: 'string' as const, sortable: true, searchable: true, width: 200 }, { key: 'overview', label: 'Beschreibung', type: 'string' as const, width: 300 }, { key: 'isSystem', label: 'Typ', type: 'custom' as const, width: 100, render: (value: boolean) => value ? System : Instanz }, { key: '_createdByUserName', label: 'Erstellt von', type: 'string' as const, width: 150 }, ], []); // Handle edit click - open editor with template data const handleEditClick = async (template: AutomationTemplate) => { // Fetch full template data const fullTemplate = await getTemplate(template.id); setEditingTemplate(fullTemplate || template); setShowEditor(true); }; // Handle create click - open editor for new template const handleCreateClick = () => { setEditingTemplate(null); setShowEditor(true); }; // Handle editor save const handleEditorSave = async (data: Partial) => { setSaving(true); try { if (editingTemplate) { await updateTemplate(editingTemplate.id, data); showSuccess('Vorlage aktualisiert'); } else { await createTemplate(data as any); showSuccess('Vorlage erstellt'); } setShowEditor(false); setEditingTemplate(null); await refetch(); } catch (err: any) { showError(`Fehler: ${err.message}`); } finally { setSaving(false); } }; // Handle editor cancel const handleEditorCancel = () => { setShowEditor(false); setEditingTemplate(null); }; // Handle delete by ID (used by DeleteActionButton via hookData) const handleDelete = async (templateId: string): Promise => { try { await deleteTemplate(templateId); showSuccess('Vorlage gelöscht'); return true; } catch (err: any) { showError(`Fehler: ${err.message}`); return false; } }; // Handle duplicate const handleDuplicate = async (template: AutomationTemplate) => { try { await duplicateTemplate(template.id); showSuccess('Vorlage dupliziert'); await refetch(); } catch (err: any) { showError(`Fehler beim Duplizieren: ${err.message}`); } }; // Check if template is editable (system templates only by SysAdmin) const _canEditTemplate = (template: AutomationTemplate) => { if ((template as any).isSystem) return isSysAdmin; return canUpdate; }; const _canDeleteTemplate = (template: AutomationTemplate) => { if ((template as any).isSystem) return isSysAdmin; return canDelete; }; if (error) { return (
⚠️

Fehler beim Laden der Vorlagen: {error}

); } return (

Automation-Vorlagen

Verwalten Sie Ihre Workflow-Vorlagen

{canCreate && ( )}
{loading && (!templates || templates.length === 0) ? (
Lade Vorlagen...
) : !templates || templates.length === 0 ? (

Keine Vorlagen vorhanden

Erstellen Sie eine neue Vorlage für Ihre Workflows.

{canCreate && ( )}
) : ( , title: 'Duplizieren', onAction: handleDuplicate, }, { type: 'edit' as const, onAction: handleEditClick, title: 'Bearbeiten', disabled: (row: any) => row.isSystem && !isSysAdmin ? { disabled: true, message: 'System-Vorlagen können nur vom SysAdmin bearbeitet werden' } : !canUpdate ? { disabled: true, message: 'Keine Berechtigung' } : false, }, { type: 'delete' as const, title: 'Löschen', disabled: (row: any) => row.isSystem && !isSysAdmin ? { disabled: true, message: 'System-Vorlagen können nur vom SysAdmin gelöscht werden' } : !canDelete ? { disabled: true, message: 'Keine Berechtigung' } : false, }, ]} onDelete={(template) => handleDelete(template.id)} hookData={{ refetch, handleDelete, attributes, }} emptyMessage="Keine Vorlagen gefunden" /> )}
{/* Automation Editor */} {showEditor && ( )}
); }; export default AutomationTemplatesPage;