Skip to content

Commit 218f2d2

Browse files
committed
add poll delte
1 parent 404e60e commit 218f2d2

4 files changed

Lines changed: 152 additions & 61 deletions

File tree

‎src/Pages/Forum/Forum-Post.jsx‎

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import BarChartIcon from '@mui/icons-material/BarChart';
3333
import Dialog from '@mui/material/Dialog';
3434
import DialogTitle from '@mui/material/DialogTitle';
3535
import DialogContent from '@mui/material/DialogContent';
36+
import DialogContentText from '@mui/material/DialogContentText';
3637
import DialogActions from '@mui/material/DialogActions';
3738
import TextField from '@mui/material/TextField';
3839
import Checkbox from '@mui/material/Checkbox';
@@ -146,6 +147,7 @@ export default function Forum_Post() {
146147
const [pollOptions, setPollOptions] = useState(['', '']);
147148
const [pollAllowMultiple, setPollAllowMultiple] = useState(false);
148149
const [submittingPoll, setSubmittingPoll] = useState(false);
150+
const [deletePollId, setDeletePollId] = useState(null);
149151

150152
const isCreatorOrAdmin = post && auth.user && (post.creator === auth.user || auth.roles === 'Admin');
151153

@@ -213,6 +215,31 @@ export default function Forum_Post() {
213215
});
214216
};
215217

218+
const handleDeletePoll = (pollId) => {
219+
setDeletePollId(pollId);
220+
};
221+
222+
const handleConfirmDeletePoll = () => {
223+
if (!deletePollId) return;
224+
const pollId = deletePollId;
225+
setDeletePollId(null);
226+
227+
api.delete(`/forum/poll/delete?poll=${pollId}`)
228+
.then(() => {
229+
alertsManagerRef.current.showAlert('success', 'Umfrage erfolgreich gelöscht');
230+
fetchData(true);
231+
})
232+
.catch(error => {
233+
console.error("Failed to delete poll", error);
234+
const status = error.response?.status || 500;
235+
const rawData = error.response?.data;
236+
const msg = typeof rawData === 'object'
237+
? (rawData.message || rawData.details || JSON.stringify(rawData))
238+
: (rawData || 'Fehler beim Löschen der Umfrage');
239+
alertsManagerRef.current.showAlert('error', `${status}: ${msg}`);
240+
});
241+
};
242+
216243
// Fetch post, answers, and associated event
217244
const fetchData = useCallback((silent = false) => {
218245
if (!silent) {
@@ -382,7 +409,7 @@ export default function Forum_Post() {
382409
</AccordionSummary>
383410
<Divider />
384411
<AccordionDetails sx={{ p: 0 }}>
385-
<Post post={post} onUpdate={() => fetchData(true)} onDelete={() => navigate("/Forum/Topic/" + post.topicId)} />
412+
<Post post={post} onUpdate={() => fetchData(true)} onDelete={() => navigate("/Forum/Topic/" + post.topicId)} onAddPoll={() => setCreatePollOpen(true)} />
386413
</AccordionDetails>
387414
</Accordion>
388415

@@ -392,27 +419,13 @@ export default function Forum_Post() {
392419
<Grid container spacing={3}>
393420
{post.polls.map(poll => (
394421
<Grid key={poll.id} size={post.polls.length === 1 ? { xs: 12 } : { xs: 12, md: 6 }}>
395-
<PollWidget pollData={poll} />
422+
<PollWidget pollData={poll} canDelete={isCreatorOrAdmin} onDelete={() => handleDeletePoll(poll.id)} />
396423
</Grid>
397424
))}
398425
</Grid>
399426
</Box>
400427
)}
401428

402-
{/* Button to add a new poll */}
403-
{isCreatorOrAdmin && (
404-
<Box sx={{ mb: 4, display: 'flex', justifyContent: 'flex-start' }}>
405-
<Button
406-
variant="outlined"
407-
color="primary"
408-
startIcon={<BarChartIcon />}
409-
onClick={() => setCreatePollOpen(true)}
410-
>
411-
Umfrage hinzufügen
412-
</Button>
413-
</Box>
414-
)}
415-
416429
{/* Associated Event Details & Attendance */}
417430
{associatedEvent && (
418431
<Card sx={{ mb: 4, border: '1px solid rgba(255, 255, 255, 0.08)', bgcolor: 'rgba(255, 255, 255, 0.02)', borderRadius: 2 }}>
@@ -422,7 +435,6 @@ export default function Forum_Post() {
422435
<Typography variant="h6" sx={{ fontWeight: 'bold' }}>
423436
Zugehöriges Event: {associatedEvent.title}
424437
</Typography>
425-
</Stack>
426438
<Typography variant="body2" color="text.secondary" sx={{ mb: 2.5 }}>
427439
Ort: <strong>{associatedEvent.location}</strong> | Datum: <strong>{(() => {
428440
if (!associatedEvent.eventDate) return '';
@@ -455,6 +467,7 @@ export default function Forum_Post() {
455467
}
456468
})()}</strong>
457469
</Typography>
470+
</Stack>
458471

459472
<Grid container spacing={2} sx={{ mt: 2.5 }}>
460473
{/* YES / Zusagen */}
@@ -465,7 +478,8 @@ export default function Forum_Post() {
465478
p: 2,
466479
borderRadius: 2,
467480
bgcolor: userStatus === 'YES' ? 'rgba(76, 175, 80, 0.12)' : 'rgba(76, 175, 80, 0.03)',
468-
border: userStatus === 'YES' ? '2px solid rgba(76, 175, 80, 0.5)' : '1px solid rgba(76, 175, 80, 0.12)',
481+
border: '2px solid',
482+
borderColor: userStatus === 'YES' ? 'rgba(76, 175, 80, 0.5)' : 'rgba(76, 175, 80, 0.12)',
469483
height: '100%',
470484
cursor: isLoggedIn ? 'pointer' : 'default',
471485
transition: 'all 0.2s ease-in-out',
@@ -495,7 +509,8 @@ export default function Forum_Post() {
495509
p: 2,
496510
borderRadius: 2,
497511
bgcolor: userStatus === 'NO' ? 'rgba(244, 67, 54, 0.12)' : 'rgba(244, 67, 54, 0.03)',
498-
border: userStatus === 'NO' ? '2px solid rgba(244, 67, 54, 0.5)' : '1px solid rgba(244, 67, 54, 0.12)',
512+
border: '2px solid',
513+
borderColor: userStatus === 'NO' ? 'rgba(244, 67, 54, 0.5)' : 'rgba(244, 67, 54, 0.12)',
499514
height: '100%',
500515
cursor: isLoggedIn ? 'pointer' : 'default',
501516
transition: 'all 0.2s ease-in-out',
@@ -525,7 +540,8 @@ export default function Forum_Post() {
525540
p: 2,
526541
borderRadius: 2,
527542
bgcolor: userStatus === 'MAYBE' ? 'rgba(255, 152, 0, 0.12)' : 'rgba(255, 152, 0, 0.03)',
528-
border: userStatus === 'MAYBE' ? '2px solid rgba(255, 152, 0, 0.5)' : '1px solid rgba(255, 152, 0, 0.12)',
543+
border: '2px solid',
544+
borderColor: userStatus === 'MAYBE' ? 'rgba(255, 152, 0, 0.5)' : 'rgba(255, 152, 0, 0.12)',
529545
height: '100%',
530546
cursor: isLoggedIn ? 'pointer' : 'default',
531547
transition: 'all 0.2s ease-in-out',
@@ -549,7 +565,7 @@ export default function Forum_Post() {
549565

550566
{/* NO RESPONSE / Ausstehend */}
551567
<Grid size={{ xs: 12, sm: 3 }}>
552-
<Box sx={{ p: 2, borderRadius: 2, bgcolor: 'rgba(255, 255, 255, 0.02)', border: '1px solid rgba(255, 255, 255, 0.1)', height: '100%' }}>
568+
<Box sx={{ p: 2, borderRadius: 2, bgcolor: 'rgba(255, 255, 255, 0.02)', border: '2px solid rgba(255, 255, 255, 0.1)', height: '100%' }}>
553569
<Typography variant="subtitle2" sx={{ fontWeight: 'bold', mb: 1, color: 'text.primary' }}>
554570
⚪ Rückmeldung ausstehend ({associatedEvent.nonRespondents?.length || 0})
555571
</Typography>
@@ -625,6 +641,42 @@ export default function Forum_Post() {
625641
)}
626642
</Grid>
627643

644+
{/* Dialog for confirming poll deletion */}
645+
<Dialog
646+
open={deletePollId !== null}
647+
onClose={() => setDeletePollId(null)}
648+
PaperProps={{
649+
sx: {
650+
bgcolor: 'background.paper',
651+
backgroundImage: 'none',
652+
border: '1px solid rgba(255,255,255,0.08)',
653+
borderRadius: 2,
654+
minWidth: 320
655+
}
656+
}}
657+
>
658+
<DialogTitle sx={{ fontWeight: 'bold', color: 'error.main' }}>
659+
Umfrage löschen
660+
</DialogTitle>
661+
<DialogContent>
662+
<DialogContentText>
663+
Möchtest du diese Umfrage wirklich unwiderruflich löschen? Alle Stimmen gehen dabei verloren.
664+
</DialogContentText>
665+
</DialogContent>
666+
<DialogActions sx={{ p: 2 }}>
667+
<Button onClick={() => setDeletePollId(null)} color="inherit">
668+
Abbrechen
669+
</Button>
670+
<Button
671+
onClick={handleConfirmDeletePoll}
672+
variant="contained"
673+
color="error"
674+
>
675+
Löschen
676+
</Button>
677+
</DialogActions>
678+
</Dialog>
679+
628680
{/* Dialog for creating a poll */}
629681
<Dialog
630682
open={createPollOpen}

‎src/components/Forum/Answer.jsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ export default function Answer(props) {
150150
</Stack>
151151
</Grid>
152152

153-
<Divider sx={{ width: '100%', my: 1, opacity: 0.5 }} />
153+
<Divider sx={{ width: '100%', opacity: 0.5 }} />
154154

155155
<Grid size={{ xs: 12 }}>
156156
{isEditing ? (
157157
<Box sx={{ mb: 1 }}>
158+
<style>{`.reply-editor .ql-container.ql-snow, .reply-editor .ql-toolbar.ql-snow { border-color: rgba(255,255,255,0.12) !important; }`}</style>
158159
<style>{`.reply-editor .ql-editor.ql-blank::before { color: rgba(255,255,255,0.55) !important; font-style: italic; }`}</style>
159160
<Box className="reply-editor">
160161
<ReactQuill
@@ -167,6 +168,7 @@ export default function Answer(props) {
167168
</Box>
168169
) : (
169170
<Box className="reply-body-content" sx={{ pl: 0.5 }}>
171+
<style>{`.reply-body-content .ql-container.ql-snow { border: none !important; } .reply-body-content .ql-editor { padding: 0; }`}</style>
170172
<ReactQuill
171173
theme="snow"
172174
modules={noModules}

‎src/components/Forum/PollWidget.jsx‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import Button from '@mui/material/Button';
1515
import Box from '@mui/material/Box';
1616
import LinearProgress from '@mui/material/LinearProgress';
1717
import Stack from '@mui/material/Stack';
18+
import IconButton from '@mui/material/IconButton';
19+
import Tooltip from '@mui/material/Tooltip';
1820
import CheckCircleIcon from '@mui/icons-material/CheckCircle';
1921
import BarChartIcon from '@mui/icons-material/BarChart';
22+
import DeleteForeverIcon from '@mui/icons-material/DeleteForever';
2023

2124
// Eigene
2225
import useAuth from '../../context/useAuth';
2326
import { AlertsContext } from '../../components/utils/AlertsManager';
2427

2528
export default function PollWidget(props) {
26-
const { pollData } = props;
29+
const { pollData, canDelete, onDelete } = props;
2730
const { auth } = useAuth();
2831
const alertsManagerRef = use(AlertsContext);
2932

@@ -97,7 +100,7 @@ export default function PollWidget(props) {
97100
const totalVotes = poll.options.reduce((sum, opt) => sum + (opt.votes || 0), 0);
98101

99102
return (
100-
<Card sx={{ border: '1px solid rgba(255, 152, 0, 0.25)', bgcolor: 'background.paper', mb: 3 }}>
103+
<Card sx={{ border: '1px solid rgba(255, 152, 0, 0.25)', bgcolor: 'background.paper' }}>
101104
<CardContent sx={{ p: 3 }}>
102105
<Stack direction="row" spacing={2} sx={{ alignItems: 'center', justifyContent: 'space-between', mb: 2 }}>
103106
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
@@ -106,16 +109,29 @@ export default function PollWidget(props) {
106109
Umfrage: {poll.question}
107110
</Typography>
108111
</Stack>
109-
{hasVoted && auth.user && (
110-
<Button
111-
variant="outlined"
112-
color="secondary"
113-
size="small"
114-
onClick={() => setHasVoted(false)}
115-
>
116-
Stimme ändern
117-
</Button>
118-
)}
112+
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
113+
{hasVoted && auth.user && (
114+
<Button
115+
variant="outlined"
116+
color="secondary"
117+
size="small"
118+
onClick={() => setHasVoted(false)}
119+
>
120+
Stimme ändern
121+
</Button>
122+
)}
123+
{canDelete && (
124+
<Tooltip title="Umfrage löschen">
125+
<IconButton
126+
size="small"
127+
color="error"
128+
onClick={onDelete}
129+
>
130+
<DeleteForeverIcon fontSize="small" />
131+
</IconButton>
132+
</Tooltip>
133+
)}
134+
</Stack>
119135
</Stack>
120136

121137
{!hasVoted && auth.user ? (
@@ -211,4 +227,6 @@ export default function PollWidget(props) {
211227

212228
PollWidget.propTypes = {
213229
pollData: PropTypes.object.isRequired,
230+
canDelete: PropTypes.bool,
231+
onDelete: PropTypes.func,
214232
};

0 commit comments

Comments
 (0)