/** * AdminUsersPage * * Admin page for managing Users using FormGeneratorTable. */ import React, { useState, useMemo } from 'react'; import { useOrgUsers, useUserOperations } from '../../hooks/useUsers'; import { FormGeneratorTable } from '../../components/FormGenerator/FormGeneratorTable'; import { FormGeneratorForm } from '../../components/FormGenerator/FormGeneratorForm'; import { FaPlus, FaSync, FaUsers, FaKey } from 'react-icons/fa'; import styles from './Admin.module.css'; interface User { id: string; username: string; email: string; fullName: string; enabled: boolean; isSysAdmin?: boolean; [key: string]: any; } export const AdminUsersPage: React.FC = () => { // Use two hooks: one for data, one for operations const { data: users, attributes, permissions, pagination, loading, error, refetch, fetchUserById, updateOptimistically, } = useOrgUsers(); const { handleUserCreate: createUser, handleUserUpdate: updateUser, handleUserDelete: deleteUser, handleSendPasswordLink, handleInlineUpdate, sendingPasswordLink: sendingPasswordLinkState, } = useUserOperations(); const [showCreateModal, setShowCreateModal] = useState(false); const [editingUser, setEditingUser] = useState(null); // Generate columns from attributes const columns = useMemo(() => { return (attributes || []).map(attr => ({ key: attr.name, label: attr.label || attr.name, type: attr.type as any, sortable: attr.sortable !== false, filterable: attr.filterable !== false, searchable: attr.searchable !== false, width: attr.width || 150, minWidth: attr.minWidth || 100, maxWidth: attr.maxWidth || 400, fkSource: (attr as any).fkSource, fkDisplayField: (attr as any).fkDisplayField, })); }, [attributes]); // Check permissions const canCreate = permissions?.create !== 'n'; const canUpdate = permissions?.update !== 'n'; const canDelete = permissions?.delete !== 'n'; // Handle edit click const handleEditClick = async (user: User) => { const fullUser = await fetchUserById(user.id); if (fullUser) { setEditingUser(fullUser as User); } }; // Handle create submit const handleCreateSubmit = async (data: Partial) => { const result = await createUser(data as Omit); if (result.success) { setShowCreateModal(false); refetch(); // Refresh the list } }; // Handle edit submit const handleEditSubmit = async (data: Partial) => { if (!editingUser) return; const result = await updateUser(editingUser.id, data); if (result.success) { setEditingUser(null); refetch(); // Refresh the list } }; // Handle delete (confirmation handled by DeleteActionButton) const handleDeleteUser = async (user: User) => { const success = await deleteUser(user.id); if (success) { refetch(); // Refresh the list } }; // Handle send password link const handleSendPassword = async (user: User) => { await handleSendPasswordLink(user.id); }; // Form attributes from backend - filter for create/edit forms const formAttributes = useMemo(() => { const excludedFields = ['id', 'hashedPassword', 'authenticationAuthority']; return (attributes || []) .filter(attr => !excludedFields.includes(attr.name)) .map(attr => ({ ...attr, // Mark username as readonly for edit mode (will be handled by FormGeneratorForm) editable: attr.name === 'username' ? false : attr.editable, })); }, [attributes]); if (error) { return (
⚠️

Fehler beim Laden der Benutzer: {error}

); } return (

Benutzer

Verwalten Sie alle Benutzer im System

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

Keine Benutzer vorhanden

Erstellen Sie einen neuen Benutzer, um loszulegen.

{canCreate && ( )}
) : ( , onClick: handleSendPassword, title: 'Passwort-Link senden', loading: (row: User) => sendingPasswordLinkState.has(row.id), } ] : []} onDelete={handleDeleteUser} hookData={{ refetch, permissions, pagination, handleDelete: deleteUser, handleInlineUpdate, updateOptimistically, }} emptyMessage="Keine Benutzer gefunden" /> )}
{/* Create Modal */} {showCreateModal && (
setShowCreateModal(false)}>
e.stopPropagation()}>

Neuer Benutzer

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

Benutzer bearbeiten

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