1+ import { useCallback } from 'react' ;
2+ import { arrayMove } from '@dnd-kit/sortable' ;
13import { SortableContext , verticalListSortingStrategy } from '@dnd-kit/sortable' ;
24import { ContainerType } from '@src/generic/key-utils' ;
35import type { OutlineActionSelection , XBlock , XBlockActions } from '@src/data/types' ;
@@ -10,6 +12,8 @@ import OutlineAddChildButtons from './OutlineAddChildButtons';
1012import DraggableList from './drag-helper/DraggableList' ;
1113import {
1214 canMoveSection ,
15+ moveSubsection ,
16+ moveUnit ,
1317 possibleUnitMoves ,
1418 possibleSubsectionMoves ,
1519} from './drag-helper/utils' ;
@@ -37,9 +41,6 @@ export interface OutlineTreeProps {
3741 subsectionId : string ,
3842 unitListIds : string [ ] ,
3943 ) => Promise < void > ;
40- updateSectionOrderByIndex : ( currentIndex : number , newIndex : number ) => Promise < void > ;
41- updateSubsectionOrderByIndex : ( section : XBlock , moveDetails : any ) => Promise < void > ;
42- updateUnitOrderByIndex : ( section : XBlock , moveDetails : any ) => Promise < void > ;
4344 handleOpenHighlightsModal : ( section : XBlock ) => void ;
4445 openConfigureModal : ( selection : OutlineActionSelection ) => void ;
4546 openDeleteModal : ( selection : OutlineActionSelection ) => void ;
@@ -60,15 +61,50 @@ const OutlineTree = ({
6061 commitSectionReorder,
6162 commitSubsectionReorder,
6263 commitUnitReorder,
63- updateSectionOrderByIndex,
64- updateSubsectionOrderByIndex,
65- updateUnitOrderByIndex,
6664 handleOpenHighlightsModal,
6765 openConfigureModal,
6866 openDeleteModal,
6967 handlePasteClipboardClick,
70- } : OutlineTreeProps ) => (
71- < section >
68+ } : OutlineTreeProps ) => {
69+ // ─── Card order change handlers (preview + commit) ────────────────────
70+ const handleSectionOrderChange = useCallback ( async ( oldIndex : number , newIndex : number ) => {
71+ if ( oldIndex === newIndex ) { return ; }
72+ const nextSections = arrayMove ( sections , oldIndex , newIndex ) as XBlock [ ] ;
73+ const sectionListIds = nextSections . map ( ( s ) => s . id ) ;
74+ previewSections ( nextSections ) ;
75+ await commitSectionReorder ( sectionListIds ) ;
76+ } , [ sections , previewSections , commitSectionReorder ] ) ;
77+
78+ const handleSubsectionOrderChange = useCallback ( async ( section : XBlock , moveDetails : any ) => {
79+ const { fn, args, sectionId } = moveDetails ;
80+ if ( ! args ) { return ; }
81+ const [ sectionsCopy , newSubsections ] = fn ( ...args ) ;
82+ if ( newSubsections && sectionId ) {
83+ previewSections ( sectionsCopy ) ;
84+ await commitSubsectionReorder (
85+ sectionId ,
86+ section . id ,
87+ newSubsections . map ( ( s : XBlock ) => s . id ) ,
88+ ) ;
89+ }
90+ } , [ previewSections , commitSubsectionReorder ] ) ;
91+
92+ const handleUnitOrderChange = useCallback ( async ( section : XBlock , moveDetails : any ) => {
93+ const { fn, args, sectionId, subsectionId } = moveDetails ;
94+ if ( ! args ) { return ; }
95+ const [ sectionsCopy , newUnits ] = fn ( ...args ) ;
96+ if ( newUnits && subsectionId ) {
97+ previewSections ( sectionsCopy ) ;
98+ await commitUnitReorder (
99+ sectionId ,
100+ section . id ,
101+ subsectionId ,
102+ newUnits . map ( ( u : XBlock ) => u . id ) ,
103+ ) ;
104+ }
105+ } , [ previewSections , commitUnitReorder ] ) ;
106+
107+ return ( < section >
72108 { ! hasOutlineIndexError && (
73109 < div className = "pt-4" >
74110 { sections . length ? (
@@ -98,7 +134,7 @@ const OutlineTree = ({
98134 onOpenConfigureModal = { openConfigureModal }
99135 onOpenDeleteModal = { openDeleteModal }
100136 isSectionsExpanded = { isSectionsExpanded }
101- onOrderChange = { updateSectionOrderByIndex }
137+ onOrderChange = { handleSectionOrderChange }
102138 >
103139 < SortableContext
104140 id = { section . id }
@@ -122,7 +158,7 @@ const OutlineTree = ({
122158 isCustomRelativeDatesActive = { isCustomRelativeDatesActive }
123159 onOpenDeleteModal = { openDeleteModal }
124160 onOpenConfigureModal = { openConfigureModal }
125- onOrderChange = { updateSubsectionOrderByIndex }
161+ onOrderChange = { handleSubsectionOrderChange }
126162 onPasteClick = { handlePasteClipboardClick }
127163 >
128164 < SortableContext
@@ -149,7 +185,7 @@ const OutlineTree = ({
149185 ) }
150186 onOpenConfigureModal = { openConfigureModal }
151187 onOpenDeleteModal = { openDeleteModal }
152- onOrderChange = { updateUnitOrderByIndex }
188+ onOrderChange = { handleUnitOrderChange }
153189 discussionsSettings = { discussionsSettings }
154190 />
155191 ) ) }
@@ -185,6 +221,7 @@ const OutlineTree = ({
185221 </ div >
186222 ) }
187223 </ section >
188- ) ;
224+ ) ;
225+ } ;
189226
190227export default OutlineTree ;
0 commit comments