forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutlineAddChildButtons.tsx
More file actions
389 lines (373 loc) · 12.6 KB
/
OutlineAddChildButtons.tsx
File metadata and controls
389 lines (373 loc) · 12.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import {
Button, Col, IconButton, Row, Stack, StandardModal, useToggle,
} from '@openedx/paragon';
import { Add as IconAdd, Close, Newsstand } from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
import { useSelector } from 'react-redux';
import { getStudioHomeData } from '@src/studio-home/data/selectors';
import { ContainerType } from '@src/generic/key-utils';
import { type OutlineFlowType, useOutlineSidebarContext } from '@src/course-outline/outline-sidebar/OutlineSidebarContext';
import { useCourseAuthoringContext } from '@src/CourseAuthoringContext';
import { LoadingSpinner } from '@src/generic/Loading';
import { useCallback } from 'react';
import { COURSE_BLOCK_NAMES } from '@src/constants';
import { COMPONENT_TYPES } from '@src/generic/block-type-utils/constants';
import { LibraryAndComponentPicker, type SelectedComponent } from '@src/library-authoring';
import { ContentType } from '@src/library-authoring/routes';
import { isOutlineNewDesignEnabled } from '@src/course-outline/utils';
import messages from './messages';
/**
* Placeholder component that is displayed when a user clicks the "Use content from library" button.
* Shows a loading spinner when the component is selected and being added to the course.
* Finally it is hidden once the add component operation is complete and the content is successfully
* added to the course.
* @param props.parentLocator The locator of the parent flow item to which the content will be added.
*/
const AddPlaceholder = ({ parentLocator }: { parentLocator?: string }) => {
const intl = useIntl();
const { currentFlow, stopCurrentFlow } = useOutlineSidebarContext();
const {
handleAddSection,
handleAddSubsection,
handleAddUnit,
} = useCourseAuthoringContext();
if (!currentFlow || currentFlow.parentLocator !== parentLocator) {
return null;
}
const getTitle = () => {
switch (currentFlow?.flowType) {
case 'use-section':
return intl.formatMessage(messages.placeholderSectionText);
case 'use-subsection':
return intl.formatMessage(messages.placeholderSubsectionText);
case 'use-unit':
return intl.formatMessage(messages.placeholderUnitText);
default:
// istanbul ignore next: this should never happen
throw new Error('Unknown flow type');
}
};
return (
<Row
className="mx-0 py-3 px-4 border-dashed border-gray-500 shadow-lg rounded bg-white w-100"
>
<Col className="py-3">
<Stack direction="horizontal" gap={3}>
{(handleAddSection.isPending
|| handleAddSubsection.isPending
|| handleAddUnit.isPending) && (
<LoadingSpinner />
)}
<h3 className="mb-0">{getTitle()}</h3>
<IconButton
src={Close}
alt="Close"
onClick={stopCurrentFlow}
variant="dark"
className="ml-auto"
/>
</Stack>
</Col>
</Row>
);
};
interface BaseProps {
handleNewButtonClick?: () => void;
onClickCard?: (e: React.MouseEvent) => void;
childType: ContainerType;
btnVariant?: string;
btnClasses?: string;
btnSize?: 'sm' | 'md' | 'lg' | 'inline';
parentLocator: string;
}
interface NewChildButtonsProps extends BaseProps {
handleUseFromLibraryClick?: () => void;
parentTitle: string;
}
const NewOutlineAddChildButtons = ({
handleNewButtonClick,
handleUseFromLibraryClick,
onClickCard,
childType,
btnVariant = 'outline-primary',
btnClasses = 'mt-4 border-gray-500 rounded-0',
btnSize,
parentLocator,
parentTitle,
}: NewChildButtonsProps) => {
// WARNING: Do not use "useStudioHome" to get "librariesV2Enabled" flag below,
// as it has a useEffect that fetches course waffle flags whenever
// location.search is updated. Course search updates location.search when
// user types, which will then trigger the useEffect and reload the page.
// See https://github.com/openedx/frontend-app-authoring/pull/1938.
const { librariesV2Enabled } = useSelector(getStudioHomeData);
const intl = useIntl();
const {
courseUsageKey,
handleAddSection,
handleAddSubsection,
handleAddUnit,
} = useCourseAuthoringContext();
const { startCurrentFlow } = useOutlineSidebarContext();
let messageMap = {
newButton: messages.newUnitButton,
importButton: messages.useUnitFromLibraryButton,
};
let onNewCreateContent: () => Promise<void>;
let flowType: OutlineFlowType;
// Based on the childType, determine the correct action and messages to display.
switch (childType) {
case ContainerType.Section:
messageMap = {
newButton: messages.newSectionButton,
importButton: messages.useSectionFromLibraryButton,
};
onNewCreateContent = () => handleAddSection.mutateAsync({
type: ContainerType.Chapter,
parentLocator: courseUsageKey,
displayName: COURSE_BLOCK_NAMES.chapter.name,
});
flowType = 'use-section';
break;
case ContainerType.Subsection:
messageMap = {
newButton: messages.newSubsectionButton,
importButton: messages.useSubsectionFromLibraryButton,
};
onNewCreateContent = () => handleAddSubsection.mutateAsync({
type: ContainerType.Sequential,
parentLocator,
displayName: COURSE_BLOCK_NAMES.sequential.name,
});
flowType = 'use-subsection';
break;
case ContainerType.Unit:
messageMap = {
newButton: messages.newUnitButton,
importButton: messages.useUnitFromLibraryButton,
};
onNewCreateContent = () => handleAddUnit.mutateAsync({
type: ContainerType.Vertical,
parentLocator,
displayName: COURSE_BLOCK_NAMES.vertical.name,
});
flowType = 'use-unit';
break;
default:
// istanbul ignore next: unreachable
throw new Error(`Unrecognized block type ${childType}`);
}
/**
* Starts add flow in sidebar when `Use content from library` button is clicked.
*/
const onUseLibraryContent = useCallback(async () => {
startCurrentFlow({
flowType,
parentLocator,
parentTitle,
});
}, [
childType,
parentLocator,
parentTitle,
startCurrentFlow,
]);
return (
<>
<AddPlaceholder parentLocator={parentLocator} />
<Stack direction="horizontal" gap={3} onClick={onClickCard}>
<Button
className={btnClasses}
variant={btnVariant}
iconBefore={IconAdd}
size={btnSize}
block
onClick={handleNewButtonClick || onNewCreateContent}
>
{intl.formatMessage(messageMap.newButton)}
</Button>
{librariesV2Enabled && (
<Button
className={btnClasses}
variant={btnVariant}
iconBefore={Newsstand}
block
size={btnSize}
onClick={handleUseFromLibraryClick || onUseLibraryContent}
>
{intl.formatMessage(messageMap.importButton)}
</Button>
)}
</Stack>
</>
);
};
/**
* Legacy component for adding child blocks in Studio.
* Uses the old flow of opening a modal to allow user to select content from library.
*/
const LegacyOutlineAddChildButtons = ({
handleNewButtonClick,
childType,
btnVariant = 'outline-primary',
btnClasses = 'mt-4 border-gray-500 rounded-0',
btnSize,
parentLocator,
onClickCard,
}: BaseProps) => {
// WARNING: Do not use "useStudioHome" to get "librariesV2Enabled" flag below,
// as it has a useEffect that fetches course waffle flags whenever
// location.search is updated. Course search updates location.search when
// user types, which will then trigger the useEffect and reload the page.
// See https://github.com/openedx/frontend-app-authoring/pull/1938.
const { librariesV2Enabled } = useSelector(getStudioHomeData);
const intl = useIntl();
const {
courseUsageKey,
handleAddSection,
handleAddSubsection,
handleAddUnit,
} = useCourseAuthoringContext();
const [
isAddLibrarySectionModalOpen,
openAddLibrarySectionModal,
closeAddLibrarySectionModal,
] = useToggle(false);
let messageMap = {
newButton: messages.newUnitButton,
importButton: messages.useUnitFromLibraryButton,
modalTitle: messages.unitPickerModalTitle,
};
let onNewCreateContent: () => Promise<void>;
let onUseLibraryContent: (selected: SelectedComponent) => Promise<void>;
let visibleTabs: ContentType[] = [];
let query: string[] = [];
switch (childType) {
case ContainerType.Section:
messageMap = {
newButton: messages.newSectionButton,
importButton: messages.useSectionFromLibraryButton,
modalTitle: messages.sectionPickerModalTitle,
};
onNewCreateContent = () => handleAddSection.mutateAsync({
type: ContainerType.Chapter,
parentLocator: courseUsageKey,
displayName: COURSE_BLOCK_NAMES.chapter.name,
});
onUseLibraryContent = (selected: SelectedComponent) => handleAddSection.mutateAsync({
type: COMPONENT_TYPES.libraryV2,
category: ContainerType.Chapter,
parentLocator: courseUsageKey,
libraryContentKey: selected.usageKey,
});
visibleTabs = [ContentType.sections];
query = ['block_type = "section"'];
break;
case ContainerType.Subsection:
messageMap = {
newButton: messages.newSubsectionButton,
importButton: messages.useSubsectionFromLibraryButton,
modalTitle: messages.subsectionPickerModalTitle,
};
onNewCreateContent = () => handleAddSubsection.mutateAsync({
type: ContainerType.Sequential,
parentLocator,
displayName: COURSE_BLOCK_NAMES.sequential.name,
});
onUseLibraryContent = (selected: SelectedComponent) => handleAddSubsection.mutateAsync({
type: COMPONENT_TYPES.libraryV2,
category: ContainerType.Sequential,
parentLocator,
libraryContentKey: selected.usageKey,
});
visibleTabs = [ContentType.subsections];
query = ['block_type = "subsection"'];
break;
case ContainerType.Unit:
messageMap = {
newButton: messages.newUnitButton,
importButton: messages.useUnitFromLibraryButton,
modalTitle: messages.unitPickerModalTitle,
};
onNewCreateContent = () => handleAddUnit.mutateAsync({
type: ContainerType.Vertical,
parentLocator,
displayName: COURSE_BLOCK_NAMES.vertical.name,
});
onUseLibraryContent = (selected: SelectedComponent) => handleAddUnit.mutateAsync({
type: COMPONENT_TYPES.libraryV2,
category: ContainerType.Vertical,
parentLocator,
libraryContentKey: selected.usageKey,
});
visibleTabs = [ContentType.units];
query = ['block_type = "unit"'];
break;
default:
// istanbul ignore next: unreachable
throw new Error(`Unrecognized block type ${childType}`);
}
const handleOnComponentSelected = (selected: SelectedComponent) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
onUseLibraryContent(selected);
closeAddLibrarySectionModal();
};
return (
<>
<Stack direction="horizontal" gap={3} onClick={onClickCard}>
<Button
className={btnClasses}
variant={btnVariant}
iconBefore={IconAdd}
size={btnSize}
block
onClick={handleNewButtonClick || onNewCreateContent}
>
{intl.formatMessage(messageMap.newButton)}
</Button>
{librariesV2Enabled && (
<Button
className={btnClasses}
variant={btnVariant}
iconBefore={Newsstand}
block
size={btnSize}
onClick={openAddLibrarySectionModal}
>
{intl.formatMessage(messageMap.importButton)}
</Button>
)}
</Stack>
<StandardModal
title={intl.formatMessage(messageMap.modalTitle)}
isOpen={isAddLibrarySectionModalOpen}
onClose={closeAddLibrarySectionModal}
isOverflowVisible={false}
size="xl"
>
<LibraryAndComponentPicker
showOnlyPublished
extraFilter={query}
componentPickerMode="single"
onComponentSelected={handleOnComponentSelected}
visibleTabs={visibleTabs}
/>
</StandardModal>
</>
);
};
/**
* Wrapper component that displays the correct component based on the configuration.
*/
const OutlineAddChildButtons = (props: NewChildButtonsProps) => {
const showNewActionsBar = isOutlineNewDesignEnabled();
if (showNewActionsBar) {
return (
<NewOutlineAddChildButtons {...props} />
);
}
return (
<LegacyOutlineAddChildButtons {...props} />
);
};
export default OutlineAddChildButtons;