'use client'; import { DailyCheckbox, DailyCheckboxType } from '@/lib/types'; import { Card } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { DailyCheckboxSortable } from './DailyCheckboxSortable'; import { DailyCheckboxItem } from './DailyCheckboxItem'; import { DailyAddForm } from './DailyAddForm'; import { DndContext, closestCenter, DragEndEvent, DragOverlay, DragStartEvent } from '@dnd-kit/core'; import { SortableContext, verticalListSortingStrategy, arrayMove } from '@dnd-kit/sortable'; import { useState } from 'react'; import React from 'react'; interface DailySectionProps { title: string; date: Date; checkboxes: DailyCheckbox[]; onAddCheckbox: (text: string, type: DailyCheckboxType) => Promise; onToggleCheckbox: (checkboxId: string) => Promise; onUpdateCheckbox: (checkboxId: string, text: string, type: DailyCheckboxType, taskId?: string) => Promise; onDeleteCheckbox: (checkboxId: string) => Promise; onReorderCheckboxes: (date: Date, checkboxIds: string[]) => Promise; onToggleAll?: () => Promise; saving: boolean; refreshing?: boolean; } export function DailySection({ title, date, checkboxes, onAddCheckbox, onToggleCheckbox, onUpdateCheckbox, onDeleteCheckbox, onReorderCheckboxes, onToggleAll, saving, refreshing = false }: DailySectionProps) { const [activeId, setActiveId] = useState(null); const [items, setItems] = useState(checkboxes); const formatShortDate = (date: Date) => { return date.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' }); }; // Mettre à jour les items quand les checkboxes changent React.useEffect(() => { setItems(checkboxes); }, [checkboxes]); const handleDragStart = (event: DragStartEvent) => { setActiveId(event.active.id as string); }; const handleDragEnd = async (event: DragEndEvent) => { const { active, over } = event; setActiveId(null); if (!over || active.id === over.id) { return; } const oldIndex = items.findIndex((item) => item.id === active.id); const newIndex = items.findIndex((item) => item.id === over.id); if (oldIndex !== -1 && newIndex !== -1) { // Mise à jour optimiste const newItems = arrayMove(items, oldIndex, newIndex); setItems(newItems); // Envoyer l'ordre au serveur const checkboxIds = newItems.map(item => item.id); try { await onReorderCheckboxes(date, checkboxIds); } catch (error) { // Rollback en cas d'erreur setItems(checkboxes); console.error('Erreur lors du réordonnancement:', error); } } }; const activeCheckbox = activeId ? items.find(item => item.id === activeId) : null; return ( {/* Header */}

{title} ({formatShortDate(date)}) {refreshing && (
)}

{checkboxes.filter(cb => cb.isChecked).length}/{checkboxes.length} {onToggleAll && checkboxes.length > 0 && ( )}
{/* Liste des checkboxes - zone scrollable avec drag & drop */}
item.id)} strategy={verticalListSortingStrategy}>
{items.map((checkbox) => ( ))} {items.length === 0 && (
Aucune tâche pour cette période
)}
{/* Footer - Formulaire d'ajout toujours en bas */}
{activeCheckbox ? (
Promise.resolve()} onUpdate={() => Promise.resolve()} onDelete={() => Promise.resolve()} saving={false} />
) : null}
); }