11import { getConfig } from '@edx/frontend-platform' ;
22import {
3- createContext , useContext , useEffect , useMemo , useState ,
3+ createContext , useCallback , useContext , useEffect , useMemo , useState ,
44} from 'react' ;
55import { getAuthenticatedUser } from '@edx/frontend-platform/auth' ;
6- import { useCreateCourseBlock , useDuplicateItem } from '@src/course-outline/data/apiHooks' ;
6+ import { useCreateCourseBlock , useDeleteCourseItem , useDuplicateItem } from '@src/course-outline/data/apiHooks' ;
77import { useDispatch , useSelector } from 'react-redux' ;
88import { useNavigate } from 'react-router' ;
99import { getOutlineIndexData , getSectionsList } from '@src/course-outline/data/selectors' ;
@@ -14,6 +14,10 @@ import { useCourseDetails, useWaffleFlags } from './data/apiHooks';
1414import { RequestStatusType } from './data/constants' ;
1515import { arrayMove } from '@dnd-kit/sortable' ;
1616import { fetchCourseOutlineIndexQuery , setSectionOrderListQuery , setSubsectionOrderListQuery , setUnitOrderListQuery } from './course-outline/data/thunk' ;
17+ import { useToggle } from '@openedx/paragon' ;
18+ import { getBlockType } from './generic/key-utils' ;
19+ import { COURSE_BLOCK_NAMES } from './constants' ;
20+ import { deleteSection , deleteSubsection , deleteUnit } from './course-outline/data/slice' ;
1721
1822type ModalState = {
1923 value ?: XBlock | UnitXBlock ;
@@ -46,6 +50,10 @@ export type CourseAuthoringContextData = {
4650 restoreSectionList : ( ) => void ;
4751 setSections : React . Dispatch < React . SetStateAction < XBlock [ ] > > ;
4852 isDuplicatingItem : boolean ;
53+ isDeleteModalOpen : boolean ;
54+ openDeleteModal : ( ) => void ;
55+ closeDeleteModal : ( ) => void ;
56+ getHandleDeleteItemSubmit : ( callback : ( ) => void ) => ( ) => Promise < void > ;
4957 handleDuplicateSectionSubmit : ( ) => void ;
5058 handleDuplicateSubsectionSubmit : ( ) => void ;
5159 handleDuplicateUnitSubmit : ( ) => void ;
@@ -96,6 +104,8 @@ export const CourseAuthoringProvider = ({
96104 ] = useToggleWithValue < ModalState > ( ) ;
97105 const sectionsList = useSelector ( getSectionsList ) ;
98106 const [ sections , setSections ] = useState < XBlock [ ] > ( sectionsList ) ;
107+ const [ isDeleteModalOpen , openDeleteModal , closeDeleteModal ] = useToggle ( false ) ;
108+
99109
100110 const restoreSectionList = ( ) => {
101111 setSections ( ( ) => [ ...sectionsList ] ) ;
@@ -272,6 +282,68 @@ export const CourseAuthoringProvider = ({
272282 }
273283 } ;
274284
285+ const deleteMutation = useDeleteCourseItem ( ) ;
286+
287+ const getHandleDeleteItemSubmit = useCallback ( ( callback : ( ) => void ) => {
288+ return async ( ) => {
289+ // istanbul ignore if
290+ if ( ! currentSelection ) {
291+ return ;
292+ }
293+ const category = getBlockType ( currentSelection . currentId ) ;
294+ switch ( category ) {
295+ case COURSE_BLOCK_NAMES . chapter . id :
296+ await deleteMutation . mutateAsync (
297+ { itemId : currentSelection . currentId } ,
298+ {
299+ onSettled : ( ) => dispatch ( deleteSection ( { itemId : currentSelection . currentId } ) ) ,
300+ } ,
301+ ) ;
302+ break ;
303+ case COURSE_BLOCK_NAMES . sequential . id :
304+ await deleteMutation . mutateAsync (
305+ { itemId : currentSelection . currentId , sectionId : currentSelection . sectionId } ,
306+ {
307+ onSettled : ( ) => dispatch ( deleteSubsection ( {
308+ itemId : currentSelection . currentId ,
309+ sectionId : currentSelection . sectionId ,
310+ } ) ) ,
311+ } ,
312+ ) ;
313+ break ;
314+ case COURSE_BLOCK_NAMES . vertical . id :
315+ await deleteMutation . mutateAsync (
316+ {
317+ itemId : currentSelection . currentId ,
318+ subsectionId : currentSelection . subsectionId ,
319+ sectionId : currentSelection . sectionId ,
320+ } ,
321+ {
322+ onSettled : ( ) => dispatch ( deleteUnit ( {
323+ itemId : currentSelection . currentId ,
324+ subsectionId : currentSelection . subsectionId ,
325+ sectionId : currentSelection . sectionId ,
326+ } ) ) ,
327+ } ,
328+ ) ;
329+ break ;
330+ default :
331+ // istanbul ignore next
332+ throw new Error ( `Unrecognized category ${ category } ` ) ;
333+ }
334+ closeDeleteModal ( ) ;
335+ callback ( ) ;
336+ } ;
337+ } , [
338+ deleteMutation ,
339+ closeDeleteModal ,
340+ currentSelection ,
341+ dispatch ,
342+ deleteSection ,
343+ deleteUnit ,
344+ deleteSubsection ,
345+ ] ) ;
346+
275347 const context = useMemo < CourseAuthoringContextData > ( ( ) => ( {
276348 courseId,
277349 courseUsageKey,
@@ -296,6 +368,10 @@ export const CourseAuthoringProvider = ({
296368 restoreSectionList,
297369 setSections,
298370 isDuplicatingItem,
371+ isDeleteModalOpen,
372+ openDeleteModal,
373+ closeDeleteModal,
374+ getHandleDeleteItemSubmit,
299375 handleDuplicateSectionSubmit,
300376 handleDuplicateSubsectionSubmit,
301377 handleDuplicateUnitSubmit,
@@ -329,6 +405,10 @@ export const CourseAuthoringProvider = ({
329405 restoreSectionList ,
330406 setSections ,
331407 isDuplicatingItem ,
408+ isDeleteModalOpen ,
409+ openDeleteModal ,
410+ closeDeleteModal ,
411+ getHandleDeleteItemSubmit ,
332412 handleDuplicateSectionSubmit ,
333413 handleDuplicateSubsectionSubmit ,
334414 handleSectionDragAndDrop ,
0 commit comments