forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseOutlineContext.tsx
More file actions
304 lines (280 loc) · 10.3 KB
/
CourseOutlineContext.tsx
File metadata and controls
304 lines (280 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import {
createContext, useCallback, useContext, useEffect, useMemo, useState,
} from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useToggle } from '@openedx/paragon';
import { arrayMove } from '@dnd-kit/sortable';
import { SelectionState, type XBlock } from '@src/data/types';
import { useToggleWithValue } from '@src/hooks';
import { getBlockType } from '@src/generic/key-utils';
import { COURSE_BLOCK_NAMES } from '@src/constants';
import { useCourseAuthoringContext, type ModalState } from '@src/CourseAuthoringContext';
import {
useCreateCourseBlock,
useDeleteCourseItem,
useDuplicateItem,
} from './data/apiHooks';
import { getOutlineIndexData, getSectionsList } from './data/selectors';
import {
fetchCourseOutlineIndexQuery,
setSectionOrderListQuery,
setSubsectionOrderListQuery,
setUnitOrderListQuery,
} from './data/thunk';
import { deleteSection, deleteSubsection, deleteUnit } from './data/slice';
export type CourseOutlineContextData = {
handleAddAndOpenUnit: ReturnType<typeof useCreateCourseBlock>;
handleAddBlock: ReturnType<typeof useCreateCourseBlock>;
currentSelection?: SelectionState;
setCurrentSelection: React.Dispatch<React.SetStateAction<SelectionState | undefined>>;
sections: XBlock[];
restoreSectionList: () => void;
setSections: React.Dispatch<React.SetStateAction<XBlock[]>>;
isDuplicatingItem: boolean;
isDeleteModalOpen: boolean;
openDeleteModal: () => void;
closeDeleteModal: () => void;
getHandleDeleteItemSubmit: (callback: () => void) => () => Promise<void>;
handleDuplicateSectionSubmit: () => void;
handleDuplicateSubsectionSubmit: () => void;
handleDuplicateUnitSubmit: () => void;
isPublishModalOpen: boolean;
currentPublishModalData?: ModalState;
openPublishModal: (value: ModalState) => void;
closePublishModal: () => void;
handleSectionDragAndDrop: (sectionListIds: string[]) => void;
handleSubsectionDragAndDrop: (sectionId: string, prevSectionId: string, subsectionListIds: string[]) => void;
handleUnitDragAndDrop: (sectionId: string, prevSectionId: string, subsectionId: string, unitListIds: string[]) => void;
updateSectionOrderByIndex: (currentIndex: number, newIndex: number) => void;
updateSubsectionOrderByIndex: (section: XBlock, moveDetails: any) => void;
updateUnitOrderByIndex: (section: XBlock, moveDetails: any) => void;
};
/**
* Course Outline Context.
* Only available within the course outline page.
*
* Get this using `useCourseOutlineContext()`
*/
const CourseOutlineContext = createContext<CourseOutlineContextData | undefined>(undefined);
type CourseOutlineProviderProps = {
children?: React.ReactNode;
};
export const CourseOutlineProvider = ({ children }: CourseOutlineProviderProps) => {
const { courseId, openUnitPage } = useCourseAuthoringContext();
const dispatch = useDispatch();
const { courseStructure } = useSelector(getOutlineIndexData);
const sectionsList = useSelector(getSectionsList);
const [sections, setSections] = useState<XBlock[]>(sectionsList);
const [isDeleteModalOpen, openDeleteModal, closeDeleteModal] = useToggle(false);
const [
isPublishModalOpen,
currentPublishModalData,
openPublishModal,
closePublishModal,
] = useToggleWithValue<ModalState>();
/**
* This will hold the state of current item that is being operated on,
* For example:
* - the details of container that is being edited.
* - the details of container of which see more dropdown is open.
* It is mostly used in modals which should be soon be replaced with its equivalent in sidebar.
*/
const [currentSelection, setCurrentSelection] = useState<SelectionState | undefined>();
const restoreSectionList = () => {
setSections(() => [...sectionsList]);
};
useEffect(() => {
dispatch(fetchCourseOutlineIndexQuery(courseId));
}, [courseId]);
useEffect(() => {
setSections(sectionsList);
}, [sectionsList]);
const handleAddAndOpenUnit = useCreateCourseBlock(courseId, openUnitPage);
const handleAddBlock = useCreateCourseBlock(courseId);
const {
mutate: duplicateItem,
isPending: isDuplicatingItem,
} = useDuplicateItem(courseId);
// parentId is required by the API to know where to insert the duplicate.
// sectionId/subsectionId are required to invalidate the correct React Query caches after duplication.
const handleDuplicateSubmit = (parentId: string | undefined) => {
if (currentSelection?.currentId && parentId) {
duplicateItem({
itemId: currentSelection.currentId,
parentId,
sectionId: currentSelection.sectionId,
subsectionId: currentSelection.subsectionId,
});
}
};
const handleDuplicateSectionSubmit = () => handleDuplicateSubmit(courseStructure.id);
const handleDuplicateSubsectionSubmit = () => handleDuplicateSubmit(currentSelection?.sectionId);
const handleDuplicateUnitSubmit = () => handleDuplicateSubmit(currentSelection?.subsectionId);
const handleSectionDragAndDrop = (sectionListIds: string[]) => {
dispatch(setSectionOrderListQuery(courseId, sectionListIds, restoreSectionList));
};
const handleSubsectionDragAndDrop = (
sectionId: string,
prevSectionId: string,
subsectionListIds: string[],
) => {
dispatch(setSubsectionOrderListQuery(sectionId, prevSectionId, subsectionListIds, restoreSectionList));
};
const handleUnitDragAndDrop = (
sectionId: string,
prevSectionId: string,
subsectionId: string,
unitListIds: string[],
) => {
dispatch(setUnitOrderListQuery(sectionId, subsectionId, prevSectionId, unitListIds, restoreSectionList));
};
/** Move section to new index */
const updateSectionOrderByIndex = (currentIndex: number, newIndex: number) => {
if (currentIndex === newIndex) {
return;
}
setSections((prevSections) => {
const newSections = arrayMove(prevSections, currentIndex, newIndex);
handleSectionDragAndDrop(newSections.map((section) => section.id));
return newSections;
});
};
/** Uses details from move information and moves subsection */
const updateSubsectionOrderByIndex = (section: XBlock, moveDetails) => {
const { fn, args, sectionId } = moveDetails;
if (!args) {
return;
}
const [sectionsCopy, newSubsections] = fn(...args);
if (newSubsections && sectionId) {
setSections(sectionsCopy);
handleSubsectionDragAndDrop(sectionId, section.id, newSubsections.map((subsection) => subsection.id));
}
};
/** Uses details from move information and moves unit */
const updateUnitOrderByIndex = (section: XBlock, moveDetails) => {
const { fn, args, sectionId, subsectionId } = moveDetails;
if (!args) {
return;
}
const [sectionsCopy, newUnits] = fn(...args);
if (newUnits && subsectionId) {
setSections(sectionsCopy);
handleUnitDragAndDrop(sectionId, section.id, subsectionId, newUnits.map((unit) => unit.id));
}
};
const deleteMutation = useDeleteCourseItem();
const getHandleDeleteItemSubmit = useCallback((callback: () => void) => async () => {
// istanbul ignore if
if (!currentSelection) {
return;
}
const category = getBlockType(currentSelection.currentId);
switch (category) {
case COURSE_BLOCK_NAMES.chapter.id:
await deleteMutation.mutateAsync(
{ itemId: currentSelection.currentId },
{ onSettled: () => dispatch(deleteSection({ itemId: currentSelection.currentId })) },
);
break;
case COURSE_BLOCK_NAMES.sequential.id:
await deleteMutation.mutateAsync(
{ itemId: currentSelection.currentId, sectionId: currentSelection.sectionId },
{
onSettled: () => dispatch(deleteSubsection({
itemId: currentSelection.currentId,
sectionId: currentSelection.sectionId,
})),
},
);
break;
case COURSE_BLOCK_NAMES.vertical.id:
await deleteMutation.mutateAsync(
{
itemId: currentSelection.currentId,
subsectionId: currentSelection.subsectionId,
sectionId: currentSelection.sectionId,
},
{
onSettled: () => dispatch(deleteUnit({
itemId: currentSelection.currentId,
subsectionId: currentSelection.subsectionId,
sectionId: currentSelection.sectionId,
})),
},
);
break;
default:
// istanbul ignore next
throw new Error(`Unrecognized category ${category}`);
}
closeDeleteModal();
callback();
}, [deleteMutation, closeDeleteModal, currentSelection, dispatch]);
const context = useMemo<CourseOutlineContextData>(() => ({
handleAddBlock,
handleAddAndOpenUnit,
currentSelection,
setCurrentSelection,
sections,
restoreSectionList,
setSections,
isDuplicatingItem,
isDeleteModalOpen,
openDeleteModal,
closeDeleteModal,
getHandleDeleteItemSubmit,
handleDuplicateSectionSubmit,
handleDuplicateSubsectionSubmit,
handleDuplicateUnitSubmit,
isPublishModalOpen,
currentPublishModalData,
openPublishModal,
closePublishModal,
handleSectionDragAndDrop,
handleSubsectionDragAndDrop,
handleUnitDragAndDrop,
updateSectionOrderByIndex,
updateSubsectionOrderByIndex,
updateUnitOrderByIndex,
}), [
handleAddBlock,
handleAddAndOpenUnit,
currentSelection,
setCurrentSelection,
sections,
restoreSectionList,
setSections,
isDuplicatingItem,
isDeleteModalOpen,
openDeleteModal,
closeDeleteModal,
getHandleDeleteItemSubmit,
handleDuplicateSectionSubmit,
handleDuplicateSubsectionSubmit,
handleDuplicateUnitSubmit,
isPublishModalOpen,
currentPublishModalData,
openPublishModal,
closePublishModal,
handleSectionDragAndDrop,
handleSubsectionDragAndDrop,
handleUnitDragAndDrop,
updateSectionOrderByIndex,
updateSubsectionOrderByIndex,
updateUnitOrderByIndex,
]);
return (
<CourseOutlineContext.Provider value={context}>
{children}
</CourseOutlineContext.Provider>
);
};
export function useCourseOutlineContext(): CourseOutlineContextData {
const ctx = useContext(CourseOutlineContext);
if (ctx === undefined) {
/* istanbul ignore next */
throw new Error('useCourseOutlineContext() was used in a component without a <CourseOutlineProvider> ancestor.');
}
return ctx;
}