'use client'; import { useTransition } from 'react'; import { DragDropContext, Droppable, Draggable, DropResult } from '@hello-pangea/dnd'; import type { YearReviewItem, YearReviewCategory } from '@prisma/client'; import { YearReviewSection } from './YearReviewSection'; import { YearReviewCard } from './YearReviewCard'; import { moveYearReviewItem, reorderYearReviewItems } from '@/actions/year-review'; import { YEAR_REVIEW_SECTIONS } from '@/lib/types'; interface YearReviewBoardProps { sessionId: string; items: YearReviewItem[]; } export function YearReviewBoard({ sessionId, items }: YearReviewBoardProps) { const [isPending, startTransition] = useTransition(); const itemsByCategory = YEAR_REVIEW_SECTIONS.reduce( (acc, section) => { acc[section.category] = items .filter((item) => item.category === section.category) .sort((a, b) => a.order - b.order); return acc; }, {} as Record ); function handleDragEnd(result: DropResult) { if (!result.destination) return; const { source, destination, draggableId } = result; const sourceCategory = source.droppableId as YearReviewCategory; const destCategory = destination.droppableId as YearReviewCategory; // If same position, do nothing if (sourceCategory === destCategory && source.index === destination.index) { return; } startTransition(async () => { if (sourceCategory === destCategory) { // Same category - just reorder const categoryItems = itemsByCategory[sourceCategory]; const itemIds = categoryItems.map((item) => item.id); const [removed] = itemIds.splice(source.index, 1); itemIds.splice(destination.index, 0, removed); await reorderYearReviewItems(sessionId, sourceCategory, itemIds); } else { // Different category - move item await moveYearReviewItem(draggableId, sessionId, destCategory, destination.index); } }); } return (
{/* Year Review Sections */}
{YEAR_REVIEW_SECTIONS.map((section) => ( {(provided, snapshot) => ( {itemsByCategory[section.category].map((item, index) => ( {(dragProvided, dragSnapshot) => ( )} ))} {provided.placeholder} )} ))}
); }