import React, { Component } from 'react'; import { Box, Button, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Dialog, DialogTitle, DialogContent, DialogActions, TextField, IconButton, Typography, Alert, CircularProgress, } from '@mui/material'; import { Add as AddIcon, Edit as EditIcon, Delete as DeleteIcon, } from '@mui/icons-material'; import AuthService from '../../services/AuthService'; class KontoTable extends Component { constructor(props) { super(props); this.state = { konten: [], loading: true, error: null, dialogOpen: false, editingKonto: null, confirmDialogOpen: false, itemToDelete: null, formData: { konto: '', name: '', }, }; this.authService = new AuthService(); // Focus management refs this.triggerRef = React.createRef(); this.dialogRef = React.createRef(); this.confirmDialogRef = React.createRef(); } componentDidMount() { this.loadKonten(); } loadKonten = async () => { try { const response = await this.authService.apiCall('/admin/konten'); if (response && response.ok) { const data = await response.json(); this.setState({ konten: data.konten, loading: false }); } else { this.setState({ error: 'Fehler beim Laden der Konten', loading: false }); } } catch (error) { console.error('Error loading konten:', error); this.setState({ error: 'Fehler beim Laden der Konten', loading: false }); } }; handleOpenDialog = (konto = null) => { // Store reference to the trigger element for focus restoration this.triggerRef.current = document.activeElement; this.setState({ dialogOpen: true, editingKonto: konto, formData: konto ? { konto: konto.konto, name: konto.name, } : { konto: '', name: '', }, }); }; handleCloseDialog = () => { this.setState({ dialogOpen: false, editingKonto: null, formData: { konto: '', name: '', }, }); // Restore focus to the trigger element after dialog closes setTimeout(() => { if (this.triggerRef.current && this.triggerRef.current.focus) { this.triggerRef.current.focus(); } }, 100); }; handleInputChange = (field) => (event) => { this.setState({ formData: { ...this.state.formData, [field]: event.target.value, }, }); }; isFormValid = () => { const { formData } = this.state; return formData.konto.trim() !== '' && formData.name.trim() !== ''; }; handleSave = async () => { const { editingKonto, formData } = this.state; try { const url = editingKonto ? `/admin/konten/${editingKonto.id}` : '/admin/konten'; const method = editingKonto ? 'PUT' : 'POST'; const response = await this.authService.apiCall(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); if (response && response.ok) { this.handleCloseDialog(); this.loadKonten(); } else { const errorData = await response.json(); this.setState({ error: errorData.error || 'Fehler beim Speichern' }); } } catch (error) { console.error('Error saving konto:', error); this.setState({ error: 'Fehler beim Speichern des Kontos' }); } }; handleDeleteClick = (konto) => { // Store reference to the trigger element for focus restoration this.triggerRef.current = document.activeElement; this.setState({ confirmDialogOpen: true, itemToDelete: konto, }); }; handleDeleteConfirm = async () => { const { itemToDelete } = this.state; if (!itemToDelete) return; this.setState({ confirmDialogOpen: false, itemToDelete: null }); // Restore focus to the trigger element after dialog closes setTimeout(() => { if (this.triggerRef.current && this.triggerRef.current.focus) { this.triggerRef.current.focus(); } }, 100); try { const response = await this.authService.apiCall(`/admin/konten/${konto.id}`, { method: 'DELETE', }); if (response && response.ok) { this.loadKonten(); } else { const errorData = await response.json(); this.setState({ error: errorData.error || 'Fehler beim Löschen' }); } } catch (error) { console.error('Error deleting konto:', error); this.setState({ error: 'Fehler beim Löschen des Kontos' }); } }; handleDeleteCancel = () => { this.setState({ confirmDialogOpen: false, itemToDelete: null, }); // Restore focus to the trigger element after dialog closes setTimeout(() => { if (this.triggerRef.current && this.triggerRef.current.focus) { this.triggerRef.current.focus(); } }, 100); }; render() { const { konten, loading, error, dialogOpen, editingKonto, formData } = this.state; if (loading) { return ( ); } return ( Konten {error && ( {error} )} Konto Name Aktionen {konten.map((konto) => ( {konto.konto} {konto.name} this.handleOpenDialog(konto)} > this.handleDeleteClick(konto)} color="error" > ))}
{editingKonto ? 'Konto bearbeiten' : 'Neues Konto'} {/* Confirmation Dialog */} Löschen bestätigen {this.state.itemToDelete && `Konto "${this.state.itemToDelete.konto} - ${this.state.itemToDelete.name}" wirklich löschen?` }
); } } export default KontoTable;