11import { getConfig } from '@edx/frontend-platform' ;
22import {
3- createContext , useContext , useMemo , useState ,
3+ createContext , useContext , useEffect , useMemo , useState ,
44} from 'react' ;
55import { getAuthenticatedUser } from '@edx/frontend-platform/auth' ;
66import { useCreateCourseBlock , useDuplicateItem } from '@src/course-outline/data/apiHooks' ;
@@ -13,7 +13,7 @@ import { CourseDetailsData } from './data/api';
1313import { useCourseDetails , useWaffleFlags } from './data/apiHooks' ;
1414import { RequestStatusType } from './data/constants' ;
1515import { arrayMove } from '@dnd-kit/sortable' ;
16- import { setSectionOrderListQuery } from './course-outline/data/thunk' ;
16+ import { fetchCourseOutlineIndexQuery , setSectionOrderListQuery , setSubsectionOrderListQuery } from './course-outline/data/thunk' ;
1717
1818type ModalState = {
1919 value ?: XBlock | UnitXBlock ;
@@ -50,7 +50,9 @@ export type CourseAuthoringContextData = {
5050 handleDuplicateSubsectionSubmit : ( ) => void ;
5151 handleDuplicateUnitSubmit : ( ) => void ;
5252 handleSectionDragAndDrop : ( sectionListIds : string [ ] ) => void ;
53+ handleSubsectionDragAndDrop : ( sectionId : string , prevSectionId : string , subsectionListIds : string [ ] ) => void ;
5354 updateSectionOrderByIndex : ( currentIndex : number , newIndex : number ) => void ;
55+ updateSubsectionOrderByIndex : ( section : XBlock , moveDetails : any ) => void ;
5456} ;
5557
5658/**
@@ -97,6 +99,14 @@ export const CourseAuthoringProvider = ({
9799 setSections ( ( ) => [ ...sectionsList ] ) ;
98100 } ;
99101
102+ useEffect ( ( ) => {
103+ dispatch ( fetchCourseOutlineIndexQuery ( courseId ) ) ;
104+ } , [ courseId ] ) ;
105+
106+ useEffect ( ( ) => {
107+ setSections ( sectionsList ) ;
108+ } , [ sectionsList ] ) ;
109+
100110 /**
101111 * This will hold the state of current item that is being operated on,
102112 * For example:
@@ -179,6 +189,19 @@ export const CourseAuthoringProvider = ({
179189 ) ) ;
180190 } ;
181191
192+ const handleSubsectionDragAndDrop = (
193+ sectionId : string ,
194+ prevSectionId : string ,
195+ subsectionListIds : string [ ] ,
196+ ) => {
197+ dispatch ( setSubsectionOrderListQuery (
198+ sectionId ,
199+ prevSectionId ,
200+ subsectionListIds ,
201+ restoreSectionList ,
202+ ) ) ;
203+ } ;
204+
182205 /**
183206 * Move section to new index
184207 */
@@ -193,6 +216,25 @@ export const CourseAuthoringProvider = ({
193216 } ) ;
194217 } ;
195218
219+ /**
220+ * Uses details from move information and moves subsection
221+ */
222+ const updateSubsectionOrderByIndex = ( section : XBlock , moveDetails ) => {
223+ const { fn, args, sectionId } = moveDetails ;
224+ if ( ! args ) {
225+ return ;
226+ }
227+ const [ sectionsCopy , newSubsections ] = fn ( ...args ) ;
228+ if ( newSubsections && sectionId ) {
229+ setSections ( sectionsCopy ) ;
230+ handleSubsectionDragAndDrop (
231+ sectionId ,
232+ section . id ,
233+ newSubsections . map ( subsection => subsection . id ) ,
234+ ) ;
235+ }
236+ } ;
237+
196238 const context = useMemo < CourseAuthoringContextData > ( ( ) => ( {
197239 courseId,
198240 courseUsageKey,
@@ -221,7 +263,9 @@ export const CourseAuthoringProvider = ({
221263 handleDuplicateSubsectionSubmit,
222264 handleDuplicateUnitSubmit,
223265 handleSectionDragAndDrop,
266+ handleSubsectionDragAndDrop,
224267 updateSectionOrderByIndex,
268+ updateSubsectionOrderByIndex,
225269 } ) , [
226270 courseId ,
227271 courseUsageKey ,
@@ -249,7 +293,9 @@ export const CourseAuthoringProvider = ({
249293 handleDuplicateSectionSubmit ,
250294 handleDuplicateSubsectionSubmit ,
251295 handleSectionDragAndDrop ,
296+ handleSubsectionDragAndDrop ,
252297 updateSectionOrderByIndex ,
298+ updateSubsectionOrderByIndex ,
253299 ] ) ;
254300
255301 return (
0 commit comments