forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionCard.tsx
More file actions
379 lines (348 loc) · 12.4 KB
/
SectionCard.tsx
File metadata and controls
379 lines (348 loc) · 12.4 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
import {
useContext, useEffect, useState, useRef, useCallback, ReactNode, useMemo,
} from 'react';
import { useDispatch } from 'react-redux';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
Bubble, Button, StandardModal, useToggle,
} from '@openedx/paragon';
import { useParams, useSearchParams } from 'react-router-dom';
import classNames from 'classnames';
import { useQueryClient } from '@tanstack/react-query';
import { setCurrentItem, setCurrentSection } from '@src/course-outline/data/slice';
import { RequestStatus, RequestStatusType } from '@src/data/constants';
import CardHeader from '@src/course-outline/card-header/CardHeader';
import SortableItem from '@src/course-outline/drag-helper/SortableItem';
import { DragContext } from '@src/course-outline/drag-helper/DragContextProvider';
import TitleButton from '@src/course-outline/card-header/TitleButton';
import XBlockStatus from '@src/course-outline/xblock-status/XBlockStatus';
import { fetchCourseSectionQuery } from '@src/course-outline/data/thunk';
import { getItemStatus, getItemStatusBorder, scrollToElement } from '@src/course-outline/utils';
import OutlineAddChildButtons from '@src/course-outline/OutlineAddChildButtons';
import { ContainerType } from '@src/generic/key-utils';
import { ComponentPicker, SelectedComponent } from '@src/library-authoring';
import { ContentType } from '@src/library-authoring/routes';
import { COMPONENT_TYPES } from '@src/generic/block-type-utils/constants';
import { PreviewLibraryXBlockChanges } from '@src/course-unit/preview-changes';
import { UpstreamInfoIcon } from '@src/generic/upstream-info-icon';
import type { XBlock } from '@src/data/types';
import { invalidateLinksQuery } from '@src/course-libraries/data/apiHooks';
import messages from './messages';
interface SectionCardProps {
section: XBlock,
isSelfPaced: boolean,
isCustomRelativeDatesActive: boolean,
children: ReactNode,
onOpenHighlightsModal: (section: XBlock) => void,
onOpenPublishModal: () => void,
onOpenConfigureModal: () => void,
onEditSectionSubmit: (itemId: string, sectionId: string, displayName: string) => void,
savingStatus?: RequestStatusType,
onOpenDeleteModal: () => void,
onOpenUnlinkModal: () => void,
onDuplicateSubmit: () => void,
isSectionsExpanded: boolean,
onNewSubsectionSubmit: (id: string) => void,
onAddSubsectionFromLibrary: (props: object) => {},
index: number,
canMoveItem: (oldIndex: number, newIndex: number) => boolean,
onOrderChange: (oldIndex: number, newIndex: number) => void,
resetScrollState: () => void,
}
const SectionCard = ({
section,
isSelfPaced,
isCustomRelativeDatesActive,
children,
index,
canMoveItem,
onOpenHighlightsModal,
onOpenPublishModal,
onOpenConfigureModal,
onEditSectionSubmit,
savingStatus,
onOpenDeleteModal,
onOpenUnlinkModal,
onDuplicateSubmit,
isSectionsExpanded,
onNewSubsectionSubmit,
onAddSubsectionFromLibrary,
onOrderChange,
resetScrollState,
}: SectionCardProps) => {
const currentRef = useRef(null);
const intl = useIntl();
const dispatch = useDispatch();
const { activeId, overId } = useContext(DragContext);
const [searchParams] = useSearchParams();
const locatorId = searchParams.get('show');
const isScrolledToElement = locatorId === section.id;
const [
isAddLibrarySubsectionModalOpen,
openAddLibrarySubsectionModal,
closeAddLibrarySubsectionModal,
] = useToggle(false);
const { courseId } = useParams();
const queryClient = useQueryClient();
// Expand the section if a search result should be shown/scrolled to
const containsSearchResult = () => {
if (locatorId) {
const subsections = section.childInfo?.children;
if (subsections) {
for (let i = 0; i < subsections.length; i++) {
const subsection = subsections[i];
// Check if the search result is one of the subsections
const matchedSubsection = subsection.id === locatorId;
if (matchedSubsection) {
return true;
}
// Check if the search result is one of the units
const matchedUnit = !!subsection.childInfo?.children?.filter((child) => child.id === locatorId).length;
if (matchedUnit) {
return true;
}
}
}
}
return false;
};
const [isExpanded, setIsExpanded] = useState(containsSearchResult() || isSectionsExpanded);
const [isFormOpen, openForm, closeForm] = useToggle(false);
const [isSyncModalOpen, openSyncModal, closeSyncModal] = useToggle(false);
const namePrefix = 'section';
useEffect(() => {
setIsExpanded(isSectionsExpanded);
}, [isSectionsExpanded]);
const {
id,
category,
displayName,
hasChanges,
published,
visibilityState,
highlights,
actions: sectionActions,
isHeaderVisible = true,
upstreamInfo,
} = section;
const blockSyncData = useMemo(() => {
if (!upstreamInfo?.readyToSync) {
return undefined;
}
return {
displayName,
downstreamBlockId: id,
upstreamBlockId: upstreamInfo.upstreamRef,
upstreamBlockVersionSynced: upstreamInfo.versionSynced,
isReadyToSyncIndividually: upstreamInfo.isReadyToSyncIndividually,
isContainer: true,
blockType: 'section',
};
}, [upstreamInfo]);
useEffect(() => {
if (activeId === id && isExpanded) {
setIsExpanded(false);
} else if (overId === id && !isExpanded) {
setIsExpanded(true);
}
}, [activeId, overId]);
useEffect(() => {
if (currentRef.current && (section.shouldScroll || isScrolledToElement)) {
// Align element closer to the top of the screen if scrolling for search result
const alignWithTop = !!isScrolledToElement;
scrollToElement(currentRef.current, alignWithTop, true);
resetScrollState();
}
}, [isScrolledToElement]);
useEffect(() => {
// If the locatorId is set/changed, we need to make sure that the section is expanded
// if it contains the result, in order to scroll to it
setIsExpanded((prevState) => containsSearchResult() || prevState);
}, [locatorId, setIsExpanded]);
const handleOnPostChangeSync = useCallback(() => {
dispatch(fetchCourseSectionQuery([section.id]));
if (courseId) {
invalidateLinksQuery(queryClient, courseId);
}
}, [dispatch, section, courseId, queryClient]);
// re-create actions object for customizations
const actions = { ...sectionActions };
// add actions to control display of move up & down menu buton.
actions.allowMoveUp = canMoveItem(index, -1);
actions.allowMoveDown = canMoveItem(index, 1);
const sectionStatus = getItemStatus({
published,
visibilityState,
hasChanges,
});
// remove border when section is expanded
const borderStyle = getItemStatusBorder(!isExpanded ? sectionStatus : '');
const handleExpandContent = () => {
setIsExpanded((prevState) => !prevState);
};
const handleClickMenuButton = () => {
dispatch(setCurrentItem(section));
dispatch(setCurrentSection(section));
};
const handleEditSubmit = (titleValue: string) => {
if (displayName !== titleValue) {
// both itemId and sectionId are same
onEditSectionSubmit(id, id, titleValue);
return;
}
closeForm();
};
const handleOpenHighlightsModal = () => {
onOpenHighlightsModal(section);
};
const handleNewSubsectionSubmit = () => {
onNewSubsectionSubmit(id);
};
const handleSectionMoveUp = () => {
onOrderChange(index, index - 1);
};
const handleSectionMoveDown = () => {
onOrderChange(index, index + 1);
};
/**
* Callback to handle the selection of a library subsection to be imported to course.
* @param {Object} selectedSubection - The selected subsection details.
* @returns {void}
*/
const handleSelectLibrarySubsection = useCallback((selectedSubection: SelectedComponent) => {
onAddSubsectionFromLibrary({
type: COMPONENT_TYPES.libraryV2,
category: ContainerType.Sequential,
parentLocator: id,
libraryContentKey: selectedSubection.usageKey,
});
closeAddLibrarySubsectionModal();
}, [id, onAddSubsectionFromLibrary, closeAddLibrarySubsectionModal]);
useEffect(() => {
if (savingStatus === RequestStatus.SUCCESSFUL) {
closeForm();
}
}, [savingStatus]);
const titleComponent = (
<TitleButton
title={displayName}
isExpanded={isExpanded}
onTitleClick={handleExpandContent}
namePrefix={namePrefix}
prefixIcon={<UpstreamInfoIcon upstreamInfo={upstreamInfo} />}
/>
);
const isDraggable = actions.draggable && (actions.allowMoveUp || actions.allowMoveDown);
return (
<>
<SortableItem
id={id}
data={{
category,
displayName,
status: sectionStatus,
childAddable: actions.childAddable,
}}
isDraggable={isDraggable}
componentStyle={{
padding: '1.75rem',
...borderStyle,
}}
>
<div
className={`section-card ${isScrolledToElement ? 'highlight' : ''}`}
data-testid="section-card"
ref={currentRef}
>
<div>
{isHeaderVisible && (
<CardHeader
cardId={id}
title={displayName}
status={sectionStatus}
hasChanges={hasChanges}
onClickMenuButton={handleClickMenuButton}
onClickPublish={onOpenPublishModal}
onClickConfigure={onOpenConfigureModal}
onClickEdit={openForm}
onClickDelete={onOpenDeleteModal}
onClickUnlink={onOpenUnlinkModal}
onClickMoveUp={handleSectionMoveUp}
onClickMoveDown={handleSectionMoveDown}
onClickSync={openSyncModal}
isFormOpen={isFormOpen}
closeForm={closeForm}
onEditSubmit={handleEditSubmit}
savingStatus={savingStatus}
onClickDuplicate={onDuplicateSubmit}
titleComponent={titleComponent}
namePrefix={namePrefix}
actions={actions}
readyToSync={upstreamInfo?.readyToSync}
/>
)}
<div className="section-card__content" data-testid="section-card__content">
<div className="outline-section__status mb-1">
<Button
className="p-0 bg-transparent"
data-destid="section-card-highlights-button"
variant="tertiary"
onClick={handleOpenHighlightsModal}
>
<Bubble className="mr-1">
{highlights.length}
</Bubble>
<p className="m-0 text-black">{messages.sectionHighlightsBadge.defaultMessage}</p>
</Button>
</div>
<XBlockStatus
isSelfPaced={isSelfPaced}
isCustomRelativeDatesActive={isCustomRelativeDatesActive}
blockData={section}
/>
</div>
{isExpanded && (
<div
data-testid="section-card__subsections"
className={classNames('section-card__subsections', { 'item-children': isDraggable })}
>
{children}
{actions.childAddable && (
<OutlineAddChildButtons
handleNewButtonClick={handleNewSubsectionSubmit}
handleUseFromLibraryClick={openAddLibrarySubsectionModal}
childType={ContainerType.Subsection}
/>
)}
</div>
)}
</div>
</div>
</SortableItem>
<StandardModal
title={intl.formatMessage(messages.subsectionPickerModalTitle)}
isOpen={isAddLibrarySubsectionModalOpen}
onClose={closeAddLibrarySubsectionModal}
isOverflowVisible={false}
size="xl"
>
<ComponentPicker
showOnlyPublished
extraFilter={['block_type = "subsection"']}
componentPickerMode="single"
onComponentSelected={handleSelectLibrarySubsection}
visibleTabs={[ContentType.subsections]}
/>
</StandardModal>
{blockSyncData && (
<PreviewLibraryXBlockChanges
blockData={blockSyncData}
isModalOpen={isSyncModalOpen}
closeModal={closeSyncModal}
postChange={handleOnPostChangeSync}
/>
)}
</>
);
};
export default SectionCard;