MCPcopy Create free account
hub / github.com/WebDevSimplified/course-platform / handleDragEnd

Function handleDragEnd

src/components/SortableList.tsx:31–51  ·  view source on GitHub ↗
(event: DragEndEvent)

Source from the content-addressed store, hash-verified

29 const [, startTransition] = useTransition()
30
31 function handleDragEnd(event: DragEndEvent) {
32 const { active, over } = event
33 const activeId = active.id.toString()
34 const overId = over?.id.toString()
35 if (overId == null || activeId == null) return
36
37 function getNewArray(array: T[], activeId: string, overId: string) {
38 const oldIndex = array.findIndex(section => section.id === activeId)
39 const newIndex = array.findIndex(section => section.id === overId)
40 return arrayMove(array, oldIndex, newIndex)
41 }
42
43 startTransition(async () => {
44 setOptimisticItems(items => getNewArray(items, activeId, overId))
45 const actionData = await onOrderChange(
46 getNewArray(optimisticItems, activeId, overId).map(s => s.id)
47 )
48
49 actionToast({ actionData })
50 })
51 }
52
53 return (
54 <DndContext id={dndContextId} onDragEnd={handleDragEnd}>

Callers

nothing calls this directly

Calls 2

actionToastFunction · 0.90
getNewArrayFunction · 0.85

Tested by

no test coverage detected