Skip to content

Commit 44fd9fd

Browse files
committed
fix(course-outline): update sidebar back navigation
1 parent f874447 commit 44fd9fd

5 files changed

Lines changed: 114 additions & 6 deletions

File tree

src/course-outline/outline-sidebar/AddSidebar.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export const AddSidebar = () => {
366366
clearSelection,
367367
stopCurrentFlow,
368368
selectedContainerState,
369+
openContainerSidebar,
369370
} = useOutlineSidebarContext();
370371
const { data: flowData } = useCourseItemData(currentFlow?.parentLocator);
371372
const titleAndIcon = useMemo(() => {
@@ -387,8 +388,30 @@ export const AddSidebar = () => {
387388
]);
388389

389390
const handleBack = () => {
390-
clearSelection();
391391
stopCurrentFlow();
392+
393+
if (!selectedContainerState) {
394+
return;
395+
}
396+
397+
const { currentId, subsectionId, sectionId } = selectedContainerState;
398+
399+
if (currentId === subsectionId && sectionId) {
400+
openContainerSidebar(sectionId, undefined, sectionId);
401+
return;
402+
}
403+
404+
if (currentId === sectionId) {
405+
clearSelection();
406+
return;
407+
}
408+
409+
if (subsectionId) {
410+
openContainerSidebar(subsectionId, subsectionId, sectionId);
411+
return;
412+
}
413+
414+
clearSelection();
392415
};
393416

394417
return (

src/course-outline/outline-sidebar/OutlineAlignSidebar.test.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ jest.mock('@src/content-tags-drawer', () => ({
1616
}));
1717

1818
describe('OutlineAlignSidebar', () => {
19+
const setCurrentSelection = jest.fn();
20+
const clearSelection = jest.fn();
21+
const openContainerSidebar = jest.fn();
22+
1923
beforeEach(() => {
2024
initializeMocks();
25+
setCurrentSelection.mockReset();
26+
clearSelection.mockReset();
27+
openContainerSidebar.mockReset();
2128
jest
2229
.spyOn(CourseAuthoringContext, 'useCourseAuthoringContext')
2330
.mockReturnValue({
@@ -26,14 +33,16 @@ describe('OutlineAlignSidebar', () => {
2633
jest
2734
.spyOn(CourseOutlineContext, 'useCourseOutlineContext')
2835
.mockReturnValue({
29-
setCurrentSelection: jest.fn(),
36+
setCurrentSelection,
3037
} as any);
3138
jest
3239
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')
3340
.mockReturnValue({
3441
selectedContainerState: {
3542
currentId: 'block-v1:test+course+run+type@sequential+block@seq1',
3643
},
44+
clearSelection,
45+
openContainerSidebar,
3746
} as any);
3847
jest
3948
.spyOn(CourseDetailsApi, 'useCourseDetails')
@@ -67,6 +76,8 @@ describe('OutlineAlignSidebar', () => {
6776
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')
6877
.mockReturnValue({
6978
selectedContainerState: undefined,
79+
clearSelection,
80+
openContainerSidebar,
7081
} as any);
7182
jest
7283
.spyOn(CourseDetailsApi, 'useCourseDetails')
@@ -82,4 +93,30 @@ describe('OutlineAlignSidebar', () => {
8293

8394
expect(await screen.findByText('Test Course')).toBeInTheDocument();
8495
});
96+
97+
it('back button selects parent block in align sidebar', async () => {
98+
jest
99+
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')
100+
.mockReturnValue({
101+
selectedContainerState: {
102+
currentId: 'unit-1',
103+
subsectionId: 'subsection-1',
104+
sectionId: 'section-1',
105+
},
106+
clearSelection,
107+
openContainerSidebar,
108+
} as any);
109+
110+
render(<OutlineAlignSidebar />);
111+
112+
const backButton = await screen.findByRole('button', { name: /back/i });
113+
backButton.click();
114+
115+
expect(openContainerSidebar).toHaveBeenCalledWith('subsection-1', 'subsection-1', 'section-1');
116+
expect(setCurrentSelection).toHaveBeenCalledWith({
117+
currentId: 'subsection-1',
118+
subsectionId: 'subsection-1',
119+
sectionId: 'section-1',
120+
});
121+
});
85122
});

src/course-outline/outline-sidebar/OutlineAlignSidebar.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,39 @@ import { useOutlineSidebarContext } from './OutlineSidebarContext';
1010
export const OutlineAlignSidebar = () => {
1111
const { courseId } = useCourseAuthoringContext();
1212
const { setCurrentSelection } = useCourseOutlineContext();
13-
const { selectedContainerState, clearSelection } = useOutlineSidebarContext();
13+
const { selectedContainerState, clearSelection, openContainerSidebar } = useOutlineSidebarContext();
1414

1515
const sidebarContentId = selectedContainerState?.currentId || courseId;
1616

1717
const { data: contentData } = useContentData(sidebarContentId);
1818

19-
// istanbul ignore next
2019
const handleBack = () => {
20+
const { currentId, subsectionId, sectionId } = selectedContainerState || {};
21+
22+
if (!currentId) {
23+
clearSelection();
24+
setCurrentSelection(undefined);
25+
return;
26+
}
27+
28+
if (currentId === subsectionId && sectionId) {
29+
openContainerSidebar(sectionId, undefined, sectionId);
30+
setCurrentSelection({ currentId: sectionId, sectionId });
31+
return;
32+
}
33+
34+
if (currentId === sectionId) {
35+
clearSelection();
36+
setCurrentSelection(undefined);
37+
return;
38+
}
39+
40+
if (subsectionId) {
41+
openContainerSidebar(subsectionId, subsectionId, sectionId);
42+
setCurrentSelection({ currentId: subsectionId, subsectionId, sectionId });
43+
return;
44+
}
45+
2146
clearSelection();
2247
setCurrentSelection(undefined);
2348
};

src/course-outline/outline-sidebar/info-sidebar/SubsectionInfoSidebar.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const SubsectionSidebar = () => {
3131
setCurrentTabKey,
3232
selectedContainerState,
3333
setSelectedContainerState,
34+
openContainerInfoSidebar,
3435
} = useOutlineSidebarContext();
3536
const { subsectionId = '', index } = selectedContainerState ?? {};
3637

@@ -125,12 +126,20 @@ export const SubsectionSidebar = () => {
125126
}
126127
};
127128

129+
const handleBack = () => {
130+
if (selectedContainerState?.sectionId) {
131+
openContainerInfoSidebar(selectedContainerState.sectionId, undefined, selectedContainerState.sectionId);
132+
return;
133+
}
134+
clearSelection();
135+
};
136+
128137
return (
129138
<>
130139
<SidebarTitle
131140
title={subsectionData?.displayName || ''}
132141
icon={getItemIcon(subsectionData?.category || '')}
133-
onBackBtnClick={clearSelection}
142+
onBackBtnClick={handleBack}
134143
menuProps={{
135144
itemId: subsectionId,
136145
index: index ?? -1,

src/course-outline/outline-sidebar/info-sidebar/UnitInfoSidebar.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const UnitSidebar = () => {
7575
currentTabKey,
7676
setCurrentTabKey,
7777
setSelectedContainerState,
78+
openContainerInfoSidebar,
7879
} = useOutlineSidebarContext();
7980
const {
8081
currentId: unitId = /* istanbul ignore next */ '',
@@ -209,12 +210,25 @@ export const UnitSidebar = () => {
209210
showToast(intl.formatMessage(messages.locationCopiedText));
210211
};
211212

213+
const handleBack = () => {
214+
if (selectedContainerState?.subsectionId) {
215+
openContainerInfoSidebar(
216+
selectedContainerState.subsectionId,
217+
selectedContainerState.subsectionId,
218+
selectedContainerState.sectionId,
219+
subsectionIndex >= 0 ? subsectionIndex : undefined,
220+
);
221+
return;
222+
}
223+
clearSelection();
224+
};
225+
212226
return (
213227
<>
214228
<SidebarTitle
215229
title={unitData?.displayName || ''}
216230
icon={getItemIcon(unitData?.category || '')}
217-
onBackBtnClick={clearSelection}
231+
onBackBtnClick={handleBack}
218232
menuProps={{
219233
itemId: unitId,
220234
index: index ?? -1,

0 commit comments

Comments
 (0)