forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddSidebar.tsx
More file actions
369 lines (344 loc) · 12.5 KB
/
AddSidebar.tsx
File metadata and controls
369 lines (344 loc) · 12.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { useCallback, useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import {
Button,
Icon,
Stack, StandardModal, Tab, Tabs, useToggle,
} from '@openedx/paragon';
import { ChevronLeft, ChevronRight } from '@openedx/paragon/icons';
import { getConfig } from '@edx/frontend-platform';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import { getItemIcon } from '@src/generic/block-type-utils';
import { SidebarContent, SidebarSection, SidebarTitle } from '@src/generic/sidebar';
import { MultiLibraryProvider } from '@src/library-authoring/common/context/MultiLibraryContext';
import { ComponentPicker, SelectedComponent } from '@src/library-authoring';
import { ContentType } from '@src/library-authoring/routes';
import { SidebarFilters } from '@src/library-authoring/library-filters/SidebarFilters';
import { COMPONENT_TYPES } from '@src/generic/block-type-utils/constants';
import { BlockCardButton, BlockTemplate } from '@src/generic/sidebar/BlockCardButton';
import { useWaffleFlags } from '@src/data/apiHooks';
import { useCourseAuthoringContext } from '@src/CourseAuthoringContext';
import EditorPage from '@src/editors/EditorPage';
import VideoSelectorPage from '@src/editors/VideoSelectorPage';
import { useIframe } from '@src/generic/hooks/context/hooks';
import { ProblemTypeKeys } from '@src/editors/data/constants/problem';
import problemMessages from '@src/editors/containers/ProblemEditor/components/SelectTypeModal/content/messages';
import { getCourseSectionVertical, getCourseUnitData } from '../data/selectors';
import { useUnitSidebarContext } from './UnitSidebarContext';
import messages from './messages';
import { useHandleCreateNewCourseXBlock } from '../hooks';
import { messageTypes } from '../constants';
import { fetchCourseSectionVerticalData } from '../data/thunk';
/**
* Tab of the add sidebar to add new content to the unit
*/
const AddNewContent = () => {
const intl = useIntl();
const dispatch = useDispatch();
const { sendMessageToIframe } = useIframe();
const { blockId } = useParams();
const { courseId } = useCourseAuthoringContext();
const courseUnit = useSelector(getCourseUnitData);
const { componentTemplates = {} } = useSelector(getCourseSectionVertical);
const [blockType, setBlockType] = useState<string | null>(null);
const [newBlockId, setNewBlockId] = useState<string | null>(null);
const [editorExtraProps, setEditorExtraProps] = useState<Record<string, any> | null>(null);
const { useVideoGalleryFlow } = useWaffleFlags(courseId ?? undefined);
const [isXBlockEditorModalOpen, showXBlockEditorModal, closeXBlockEditorModal] = useToggle();
const [isVideoSelectorModalOpen, showVideoSelectorModal, closeVideoSelectorModal] = useToggle();
const [isAdvancedPageOpen, showAdvancedPage, closeAdvancedPage] = useToggle();
/** The ID of the subsection (`sequential`) that is the parent of the unit we're adding to */
const parentSubsectionId = courseUnit?.ancestorInfo?.ancestors?.[0]?.id;
// Build problem templates
const problemTemplates: BlockTemplate[] = [];
Object.values(ProblemTypeKeys).map((key) => (
problemTemplates.push({
displayName: intl.formatMessage(problemMessages[`problemType.${key}.title`]),
boilerplateName: key,
})
));
// Pre-process block templates
const templatesByType = componentTemplates.reduce((acc, item) => {
let result = item;
// (1) All types have at least one template of the same type.
// In that case, it's left empty to avoid rendering that single template.
// (2) Set the problem templates required for this component.
if (item.type === 'problem') {
result = {
...item,
templates: problemTemplates,
};
} else if (item.templates.length === 1) {
result = {
...item,
templates: [],
};
}
return {
...acc,
[item.type]: result,
};
}, {});
if (courseId === undefined) {
// istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker.
throw new Error('Error: route is missing courseId.');
}
if (blockId === undefined) {
// istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker.
throw new Error('Error: route is missing blockId.');
}
const handleCreateXBlock = useHandleCreateNewCourseXBlock({ blockId });
const onXBlockSave = useCallback(/* istanbul ignore next */ () => {
closeXBlockEditorModal();
closeVideoSelectorModal();
sendMessageToIframe(messageTypes.refreshXBlock, null);
dispatch(fetchCourseSectionVerticalData(blockId, parentSubsectionId));
}, [closeXBlockEditorModal, sendMessageToIframe]);
const onXBlockCancel = useCallback(/* istanbul ignore next */ () => {
closeXBlockEditorModal();
closeVideoSelectorModal();
dispatch(fetchCourseSectionVerticalData(blockId, parentSubsectionId));
}, [closeXBlockEditorModal, sendMessageToIframe, blockId, parentSubsectionId]);
/* eslint-disable no-void */
const handleSelection = useCallback((type: string, moduleName?: string) => {
switch (type) {
case COMPONENT_TYPES.dragAndDrop:
void handleCreateXBlock({ type, parentLocator: blockId });
break;
case COMPONENT_TYPES.problem:
void handleCreateXBlock({ type, parentLocator: blockId }, ({ locator }) => {
setEditorExtraProps({ problemType: moduleName });
setBlockType(type);
setNewBlockId(locator);
showXBlockEditorModal();
});
break;
case COMPONENT_TYPES.video:
void handleCreateXBlock(
{ type, parentLocator: blockId },
/* istanbul ignore next */ ({ locator }) => {
setBlockType(type);
setNewBlockId(locator);
if (useVideoGalleryFlow) {
showVideoSelectorModal();
} else {
showXBlockEditorModal();
}
},
);
break;
case COMPONENT_TYPES.openassessment:
void handleCreateXBlock({ boilerplate: moduleName, category: type, parentLocator: blockId });
break;
case COMPONENT_TYPES.html:
void handleCreateXBlock({
type,
boilerplate: moduleName,
parentLocator: blockId,
}, /* istanbul ignore next */ ({ locator }) => {
setBlockType(type);
setNewBlockId(locator);
showXBlockEditorModal();
});
break;
case COMPONENT_TYPES.advanced:
void handleCreateXBlock({ type: moduleName, category: moduleName, parentLocator: blockId });
break;
/* istanbul ignore next */
default:
break;
}
}, [blockId]);
const blockTypes = [
{
blockType: 'html',
name: intl.formatMessage(messages.sidebarAddTextButton),
},
{
blockType: 'video',
name: intl.formatMessage(messages.sidebarAddVideoButton),
},
{
blockType: 'problem',
name: intl.formatMessage(messages.sidebarAddProblemButton),
},
{
blockType: 'drag-and-drop-v2',
name: intl.formatMessage(messages.sidebarAddDragDropButton),
},
{
blockType: 'openassessment',
name: intl.formatMessage(messages.sidebarAddOpenResponseButton),
},
];
// Render add advanced blocks page
if (isAdvancedPageOpen) {
return (
<Stack>
<Stack className="mb-2 text-primary-500" direction="horizontal" gap={1}>
<Button
className="text-primary-500"
variant="tertiary"
iconBefore={ChevronLeft}
onClick={closeAdvancedPage}
>
<FormattedMessage {...messages.sidebarAddBackButton} />
</Button>
<Icon src={ChevronRight} />
<FormattedMessage {...messages.sidebarAddAdvancedBlocksTitle} />
</Stack>
<Stack gap={2}>
{templatesByType.advanced?.templates.map((advancedTypeObj) => (
<BlockCardButton
key={advancedTypeObj.category}
blockType={advancedTypeObj.category}
name={advancedTypeObj.displayName}
onClick={() => handleSelection('advanced', advancedTypeObj.category)}
/>
))}
</Stack>
</Stack>
);
}
// Render add default blocks page
return (
<>
<Stack gap={2}>
{blockTypes.map((blockTypeObj) => (
<BlockCardButton
{...blockTypeObj}
key={blockTypeObj.blockType}
templates={templatesByType[blockTypeObj.blockType].templates}
onClick={() => handleSelection(blockTypeObj.blockType)}
onClickTemplate={(boilerplateName: string) => handleSelection(blockTypeObj.blockType, boilerplateName)}
/>
))}
{templatesByType.advanced?.templates?.length > 0 && (
<BlockCardButton
blockType="advanced"
name={intl.formatMessage(messages.sidebarAddAdvancedButton)}
onClick={showAdvancedPage}
actionIcon={<Icon src={ChevronRight} />}
/>
)}
</Stack>
<StandardModal
title={intl.formatMessage(messages.videoPickerModalTitle)}
isOpen={isVideoSelectorModalOpen}
onClose={closeVideoSelectorModal}
isOverflowVisible={false}
size="xl"
>
<div className="selector-page">
<VideoSelectorPage
blockId={newBlockId}
courseId={courseId}
studioEndpointUrl={getConfig().STUDIO_BASE_URL}
lmsEndpointUrl={getConfig().LMS_BASE_URL}
onCancel={closeVideoSelectorModal}
returnFunction={/* istanbul ignore next */ () => onXBlockSave}
/>
</div>
</StandardModal>
{isXBlockEditorModalOpen && courseId && blockType && newBlockId && (
<div className="editor-page">
<EditorPage
courseId={courseId}
blockType={blockType}
blockId={newBlockId}
studioEndpointUrl={getConfig().STUDIO_BASE_URL}
lmsEndpointUrl={getConfig().LMS_BASE_URL}
onClose={onXBlockCancel}
returnFunction={/* istanbul ignore next */ () => onXBlockSave}
extraProps={editorExtraProps}
/>
</div>
)}
</>
);
};
/**
* Tab of the add sidebar to add a content library in the unit
*
* Uses `ComponentPicker`
*/
const AddLibraryContent = () => {
const { blockId } = useParams();
if (blockId === undefined) {
// istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker.
throw new Error('Error: route is missing blockId.');
}
const handleCreateXBlock = useHandleCreateNewCourseXBlock({ blockId });
const handleSelection = useCallback(async (selection: SelectedComponent) => {
await handleCreateXBlock({
type: COMPONENT_TYPES.libraryV2,
category: selection.blockType,
parentLocator: blockId,
libraryContentKey: selection.usageKey,
});
}, [blockId]);
return (
<MultiLibraryProvider>
<ComponentPicker
showOnlyPublished
extraFilter={['type = "library_block"']}
visibleTabs={[ContentType.home]}
FiltersComponent={SidebarFilters}
onComponentSelected={handleSelection}
/>
</MultiLibraryProvider>
);
};
/**
* Main component of the Add Sidebar for the unit page
*/
export const AddSidebar = () => {
const intl = useIntl();
const unitData = useSelector(getCourseUnitData);
const {
currentTabKey,
setCurrentTabKey,
} = useUnitSidebarContext();
useEffect(() => {
if (currentTabKey === undefined) {
// Set default Tab key
setCurrentTabKey('add-new');
}
}, []);
return (
<div>
<SidebarTitle
title={unitData.displayName}
icon={getItemIcon('unit')}
/>
<SidebarContent>
<SidebarSection>
<Tabs
id="unit-add-sidebar"
className="mb-2 mx-n4.5 mx-n3.5"
activeKey={currentTabKey}
onSelect={setCurrentTabKey}
>
<Tab
eventKey="add-new"
title={intl.formatMessage(messages.sidebarAddNewTab)}
>
<div className="mt-4">
<AddNewContent />
</div>
</Tab>
<Tab
eventKey="add-existing"
title={intl.formatMessage(messages.sidebarAddExistingTab)}
>
<div className="mt-4">
<AddLibraryContent />
</div>
</Tab>
</Tabs>
</SidebarSection>
</SidebarContent>
</div>
);
};