/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { useState } from 'react'; import { Repeat, X, Trash2, Pencil, Check } from 'lucide-react'; interface RecurringTasksManagerProps { isOpen: boolean; onClose: () => void; tasks: any[]; onStopRecurring: (task: any) => void; onSeriesUpdated?: () => void; } export default function RecurringTasksManager({ isOpen, onClose, tasks, onStopRecurring, onSeriesUpdated }: RecurringTasksManagerProps) { const [editingSignature, setEditingSignature] = useState(null); const [editTitle, setEditTitle] = useState(''); const [editInterval, setEditInterval] = useState(1); const [editUnit, setEditUnit] = useState('weeks'); const [editTime, setEditTime] = useState(''); const [isSaving, setIsSaving] = useState(false); if (!isOpen) return null; // Filter and group recurring tasks by series signature const seriesGroups = new Map(); tasks.filter(t => t.isRecurring).forEach(t => { if (t.recurrenceEndDate && new Date(t.recurrenceEndDate) < new Date()) { return; } const signature = `${t.title}-${t.recurrenceInterval || 1}-${t.recurrenceUnit || 'weeks'}-${t.recurrenceTime || ''}`; const existing = seriesGroups.get(signature); if (!existing || (t.scheduledDate && new Date(t.scheduledDate) > new Date(existing.task.scheduledDate || 0))) { seriesGroups.set(signature, { task: t, signature }); } }); const groupedTasks = Array.from(seriesGroups.values()); const startEditing = (task: any, signature: string) => { setEditingSignature(signature); setEditTitle(task.title); setEditInterval(task.recurrenceInterval || 1); setEditUnit(task.recurrenceUnit || 'weeks'); setEditTime(task.recurrenceTime || ''); }; const cancelEditing = () => { setEditingSignature(null); }; const saveEditing = async (task: any) => { setIsSaving(true); try { const res = await fetch('/api/tasks/recurring', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ oldTitle: task.title, oldRecurrenceInterval: task.recurrenceInterval || 1, oldRecurrenceUnit: task.recurrenceUnit || 'weeks', oldRecurrenceTime: task.recurrenceTime || '', newTitle: editTitle, newRecurrenceInterval: editInterval, newRecurrenceUnit: editUnit, newRecurrenceTime: editTime || null, }), }); if (res.ok) { setEditingSignature(null); onSeriesUpdated?.(); } else { const err = await res.json(); alert(err.error || 'Failed to update series'); } } catch (e) { console.error('Failed to update recurring series:', e); } finally { setIsSaving(false); } }; const unitLabel = (unit: string, interval: number) => { if (unit === 'days') return interval === 1 ? 'Tag' : 'Tage'; if (unit === 'weeks') return interval === 1 ? 'Woche' : 'Wochen'; if (unit === 'months') return interval === 1 ? 'Monat' : 'Monate'; return interval === 1 ? 'Jahr' : 'Jahre'; }; return (
e.stopPropagation()}>

Wiederkehrende Aufgaben

{groupedTasks.length === 0 ? (

Du hast momentan keine aktiven wiederkehrenden Aufgaben.

) : (
{groupedTasks.map(({ task, signature }) => (
{editingSignature === signature ? ( /* Edit mode */
setEditTitle(e.target.value)} className="w-full px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-900 dark:text-white text-sm font-semibold outline-none focus:border-teal-400" placeholder="Aufgabentitel" />
Alle setEditInterval(parseInt(e.target.value) || 1)} className="w-14 px-2 py-1 rounded border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-900 dark:text-white text-xs text-center outline-none" /> um setEditTime(e.target.value)} className="px-2 py-1 rounded border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-900 dark:text-white text-xs outline-none" />
) : ( /* View mode */
{task.title}
Alle {task.recurrenceInterval === 1 ? '' : `${task.recurrenceInterval} `}{unitLabel(task.recurrenceUnit || 'weeks', task.recurrenceInterval || 1)} {task.recurrenceTime && ( um {task.recurrenceTime} Uhr )}
)}
))}
)}

Tipp: Du kannst Aufgaben jederzeit beim Bearbeiten wiederkehrend machen.

); }