/** * AdminMandatesPage * * Admin page for managing Mandates (tenants) using FormGeneratorTable. */ import React, { useState, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAdminMandates, type Mandate } from '../../hooks/useMandates'; import { FormGeneratorTable } from '../../components/FormGenerator/FormGeneratorTable'; import { FormGeneratorForm, type AttributeDefinition } from '../../components/FormGenerator/FormGeneratorForm'; import { FaPlus, FaSync, FaBuilding, FaUsers } from 'react-icons/fa'; import styles from './Admin.module.css'; export const AdminMandatesPage: React.FC = () => { const navigate = useNavigate(); const { mandates, attributes, columns, permissions, pagination, loading, error, refetch, fetchMandateById, handleCreate, handleUpdate, handleDelete, handleInlineUpdate, updateOptimistically, } = useAdminMandates(); // Form attributes from backend - filter for create/edit forms const formAttributes: AttributeDefinition[] = useMemo(() => { const excludedFields = ['id']; return attributes .filter(attr => !excludedFields.includes(attr.name)) .map(attr => ({ ...attr, type: attr.type, })) as AttributeDefinition[]; }, [attributes]); const [showCreateModal, setShowCreateModal] = useState(false); const [editingMandate, setEditingMandate] = useState(null); // Check if user can create const canCreate = permissions?.create !== 'n'; const canUpdate = permissions?.update !== 'n'; const canDelete = permissions?.delete !== 'n'; // Handle edit click const handleEditClick = async (mandate: Mandate) => { const fullMandate = await fetchMandateById(mandate.id); if (fullMandate) { setEditingMandate(fullMandate); } }; // Handle create submit const handleCreateSubmit = async (data: Partial) => { const success = await handleCreate(data); if (success) { setShowCreateModal(false); } }; // Handle edit submit const handleEditSubmit = async (data: Partial) => { if (!editingMandate) return; const success = await handleUpdate(editingMandate.id, data); if (success) { setEditingMandate(null); } }; // Handle delete (confirmation handled by DeleteActionButton) const handleDeleteMandate = async (mandate: Mandate) => { await handleDelete(mandate.id); }; if (error) { return (
⚠️

Fehler beim Laden der Mandanten: {error}

); } return (

Mandanten

Verwalten Sie alle Mandanten im System

{canCreate && ( )}
{loading && mandates.length === 0 ? (
Lade Mandanten...
) : mandates.length === 0 ? (

Keine Mandanten vorhanden

Erstellen Sie einen neuen Mandanten, um loszulegen.

{canCreate && ( )}
) : ( )}
{/* Create Modal */} {showCreateModal && (
setShowCreateModal(false)}>
e.stopPropagation()}>

Neuer Mandant

{formAttributes.length === 0 ? (
Lade Formular...
) : ( setShowCreateModal(false)} submitButtonText="Erstellen" cancelButtonText="Abbrechen" /> )}
)} {/* Edit Modal */} {editingMandate && (
setEditingMandate(null)}>
e.stopPropagation()}>

Mandant bearbeiten

{formAttributes.length === 0 ? (
Lade Formular...
) : ( setEditingMandate(null)} submitButtonText="Speichern" cancelButtonText="Abbrechen" /> )}
)}
); }; export default AdminMandatesPage;