Skip to content

Commit add4f2d

Browse files
committed
fix back button and delete options
1 parent 9aaa6dc commit add4f2d

9 files changed

Lines changed: 343 additions & 248 deletions

File tree

‎src/Pages/Admin/UserManagement.jsx‎

Lines changed: 132 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Switch from '@mui/material/Switch';
2222
import Select from '@mui/material/Select';
2323
import MenuItem from '@mui/material/MenuItem';
2424
import Stack from '@mui/material/Stack';
25-
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
25+
import BackButton from '../../components/Navigation/BackButton';
2626
import SaveIcon from '@mui/icons-material/Save';
2727
import RefreshIcon from '@mui/icons-material/Refresh';
2828
import DeleteIcon from '@mui/icons-material/Delete';
@@ -57,7 +57,10 @@ export default function UserManagement() {
5757
// Confirmation dialog state
5858
const [confirmOpen, setConfirmOpen] = useState(false);
5959
const [userToDelete, setUserToDelete] = useState(null);
60-
const [deleteForumPosts, setDeleteForumPosts] = useState(false);
60+
const [deleteAccount, setDeleteAccount] = useState(true);
61+
const [deleteEvents, setDeleteEvents] = useState(true);
62+
const [deleteForumPosts, setDeleteForumPosts] = useState(true);
63+
const [deleteGallery, setDeleteGallery] = useState(true);
6164

6265
const isAuthorized = auth?.JWT && (auth.roles === 'Admin' || auth.roles === 'Vorstand');
6366

@@ -127,6 +130,7 @@ export default function UserManagement() {
127130
lastName: user.lastName,
128131
role: user.role,
129132
isActive: activeVal,
133+
active: activeVal,
130134
isBlocked: !!user.isBlocked,
131135
yearlyFeePaid: user.yearlyFeePaid,
132136
canCreateCategory: !!user.canCreateCategory
@@ -149,55 +153,53 @@ export default function UserManagement() {
149153

150154
const handleDeleteClick = (user) => {
151155
setUserToDelete(user);
152-
setDeleteForumPosts(false);
156+
setDeleteAccount(true);
157+
setDeleteEvents(true);
158+
setDeleteForumPosts(true);
159+
setDeleteGallery(true);
153160
setConfirmOpen(true);
154161
};
155162

156163
const handleDeleteCancel = () => {
157164
setConfirmOpen(false);
158165
setUserToDelete(null);
159-
setDeleteForumPosts(false);
166+
setDeleteAccount(true);
167+
setDeleteEvents(true);
168+
setDeleteForumPosts(true);
169+
setDeleteGallery(true);
160170
};
161171

162172
const handleDeleteConfirm = () => {
163173
if (!userToDelete) return;
164174
const user = userToDelete;
165-
const shouldDeletePosts = deleteForumPosts;
166175
setConfirmOpen(false);
167176
setUserToDelete(null);
168-
setDeleteForumPosts(false);
169177
setDeletingId(user.id);
170178

171-
const doDelete = () =>
172-
api.delete(`/user/${user.id}`)
173-
.then(() => {
174-
setUsers(prev => prev.filter(u => u.id !== user.id));
175-
alertsManagerRef.current.showAlert('success', `Benutzer ${user.userName} erfolgreich gelöscht.`);
176-
})
177-
.catch(err => {
178-
console.error('Error deleting user', err);
179-
const msg = err.response?.data || 'Fehler beim Löschen.';
180-
alertsManagerRef.current.showAlert('error', `Löschen fehlgeschlagen: ${msg}`);
181-
})
182-
.finally(() => {
183-
setDeletingId(null);
184-
});
185-
186-
if (shouldDeletePosts) {
187-
api.delete(`/user/${user.id}/posts`)
188-
.then(() => {
189-
alertsManagerRef.current.showAlert('info', `Forumsbeiträge von ${user.userName} gelöscht.`);
190-
})
191-
.catch(err => {
192-
console.error('Error deleting posts', err);
193-
alertsManagerRef.current.showAlert('error', 'Forumsbeiträge konnten nicht gelöscht werden.');
194-
})
195-
.finally(() => {
196-
doDelete();
197-
});
198-
} else {
199-
doDelete();
200-
}
179+
api.delete(`/user/${user.id}`, {
180+
params: {
181+
deleteAccount: !!deleteAccount,
182+
deleteEvents: !!deleteEvents,
183+
deletePosts: !!deleteForumPosts,
184+
deleteGallery: !!deleteGallery
185+
}
186+
})
187+
.then(() => {
188+
if (deleteAccount) {
189+
alertsManagerRef.current.showAlert('success', `Benutzer ${user.userName} wurde gesperrt und ausgewählte Daten gelöscht.`);
190+
} else {
191+
alertsManagerRef.current.showAlert('success', `Ausgewählte Daten von ${user.userName} erfolgreich gelöscht.`);
192+
}
193+
fetchUsers();
194+
})
195+
.catch(err => {
196+
console.error('Error deleting user data', err);
197+
const msg = err.response?.data || 'Fehler beim Löschen.';
198+
alertsManagerRef.current.showAlert('error', `Löschen fehlgeschlagen: ${msg}`);
199+
})
200+
.finally(() => {
201+
setDeletingId(null);
202+
});
201203
};
202204

203205
const handleSignupToggle = () => {
@@ -248,13 +250,10 @@ export default function UserManagement() {
248250
<Container maxWidth="xl" sx={{ mt: 4, mb: 6, px: { xs: 1, md: 3 } }}>
249251
<Stack direction="row" sx={{ justifyContent: 'space-between', alignItems: 'center', mb: 4 }}>
250252
<Stack direction="row" spacing={2} sx={{ alignItems: 'center' }}>
251-
<Button
252-
startIcon={<ArrowBackIcon />}
253-
onClick={() => navigate(-1)}
253+
<BackButton
254254
variant="text"
255-
>
256-
Zurück
257-
</Button>
255+
fallbackPath="/"
256+
/>
258257
<Typography variant="h4" component="h1" sx={{ fontWeight: 'bold', color: 'primary.main' }}>
259258
Benutzerverwaltung
260259
</Typography>
@@ -444,43 +443,108 @@ export default function UserManagement() {
444443
bgcolor: 'background.paper',
445444
border: '1px solid rgba(255,255,255,0.1)',
446445
borderRadius: 2,
447-
minWidth: 400,
446+
minWidth: { xs: '90%', sm: 450 },
448447
}
449448
}
450449
}}
451450
>
452451
<DialogTitle sx={{ fontWeight: 'bold', color: 'error.main' }}>
453-
Benutzer löschen
452+
Daten löschen / Benutzer entfernen
454453
</DialogTitle>
455454
<DialogContent>
456455
<DialogContentText sx={{ color: 'text.secondary', mb: 2 }}>
457-
Bist du sicher, dass du den Benutzer{' '}
456+
Wähle aus, welche Daten für den Benutzer{' '}
458457
<Box component="span" sx={{ fontWeight: 'bold', color: 'text.primary' }}>
459458
{userToDelete?.userName}
460459
</Box>{' '}
461-
unwiderruflich löschen möchtest? Alle zugehörigen Daten werden entfernt.
460+
gelöscht werden sollen:
462461
</DialogContentText>
463462
<Divider sx={{ mb: 2, borderColor: 'rgba(255,255,255,0.08)' }} />
464-
<FormControlLabel
465-
control={
466-
<Checkbox
467-
checked={deleteForumPosts}
468-
onChange={e => setDeleteForumPosts(e.target.checked)}
469-
color="error"
470-
id="delete-forum-posts-checkbox"
471-
/>
472-
}
473-
label={
474-
<Box>
475-
<Typography variant="body2" sx={{ fontWeight: 'bold', color: deleteForumPosts ? 'error.main' : 'text.primary' }}>
476-
Alle Forumsbeiträge löschen
477-
</Typography>
478-
<Typography variant="caption" color="text.secondary">
479-
Entfernt sämtliche Posts dieses Benutzers im Forum (z.B. bei Spam-Accounts).
480-
</Typography>
481-
</Box>
482-
}
483-
/>
463+
<Stack spacing={2}>
464+
<FormControlLabel
465+
control={
466+
<Checkbox
467+
checked={deleteAccount}
468+
onChange={e => setDeleteAccount(e.target.checked)}
469+
color="error"
470+
id="delete-account-checkbox"
471+
/>
472+
}
473+
label={
474+
<Box>
475+
<Typography variant="body2" sx={{ fontWeight: 'bold', color: deleteAccount ? 'error.main' : 'text.primary' }}>
476+
Benutzerkonto sperren & Profil anonymisieren
477+
</Typography>
478+
<Typography variant="caption" color="text.secondary">
479+
Sperrt den Account dauerhaft und anonymisiert die Profildaten. Der Benutzer kann sich nicht erneut anmelden.
480+
</Typography>
481+
</Box>
482+
}
483+
/>
484+
485+
<FormControlLabel
486+
control={
487+
<Checkbox
488+
checked={deleteEvents}
489+
onChange={e => setDeleteEvents(e.target.checked)}
490+
color="error"
491+
id="delete-events-checkbox"
492+
/>
493+
}
494+
label={
495+
<Box>
496+
<Typography variant="body2" sx={{ fontWeight: 'bold', color: deleteEvents ? 'error.main' : 'text.primary' }}>
497+
Veranstaltungen & Anmeldungen löschen
498+
</Typography>
499+
<Typography variant="caption" color="text.secondary">
500+
Löscht alle vom Benutzer erstellten Events sowie seine Event-Teilnahmen.
501+
</Typography>
502+
</Box>
503+
}
504+
/>
505+
506+
<FormControlLabel
507+
control={
508+
<Checkbox
509+
checked={deleteForumPosts}
510+
onChange={e => setDeleteForumPosts(e.target.checked)}
511+
color="error"
512+
id="delete-forum-posts-checkbox"
513+
/>
514+
}
515+
label={
516+
<Box>
517+
<Typography variant="body2" sx={{ fontWeight: 'bold', color: deleteForumPosts ? 'error.main' : 'text.primary' }}>
518+
Forumsbeiträge löschen
519+
</Typography>
520+
<Typography variant="caption" color="text.secondary">
521+
Löscht Posts, Antworten und leere Themen. Beiträge mit Antworten anderer Benutzer werden beibehalten (Autor anonymisiert).
522+
</Typography>
523+
</Box>
524+
}
525+
/>
526+
527+
<FormControlLabel
528+
control={
529+
<Checkbox
530+
checked={deleteGallery}
531+
onChange={e => setDeleteGallery(e.target.checked)}
532+
color="error"
533+
id="delete-gallery-checkbox"
534+
/>
535+
}
536+
label={
537+
<Box>
538+
<Typography variant="body2" sx={{ fontWeight: 'bold', color: deleteGallery ? 'error.main' : 'text.primary' }}>
539+
Galerieeinträge löschen
540+
</Typography>
541+
<Typography variant="caption" color="text.secondary">
542+
Löscht alle vom Benutzer erstellten Galerieeinträge.
543+
</Typography>
544+
</Box>
545+
}
546+
/>
547+
</Stack>
484548
</DialogContent>
485549
<DialogActions sx={{ px: 3, pb: 2, gap: 1 }}>
486550
<Button onClick={handleDeleteCancel} variant="outlined" color="inherit">
@@ -491,9 +555,10 @@ export default function UserManagement() {
491555
variant="contained"
492556
color="error"
493557
startIcon={<DeleteIcon />}
558+
disabled={!deleteAccount && !deleteEvents && !deleteForumPosts && !deleteGallery}
494559
autoFocus
495560
>
496-
{deleteForumPosts ? 'Posts + Account löschen' : 'Endgültig löschen'}
561+
Ausgewählte Daten löschen
497562
</Button>
498563
</DialogActions>
499564
</Dialog>

0 commit comments

Comments
 (0)