Skip to content

Commit da46fe0

Browse files
authored
fix: remove interactivity from section/subsection preview in sidebar (#2362)
Removes interactivity from section/subsection preview in sidebar. Also fixes an issue with unit sidebar, where users could press enter after clicking on any component and it would select it.
1 parent 7c4ef47 commit da46fe0

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

src/library-authoring/containers/ContainerInfo.test.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import type MockAdapter from 'axios-mock-adapter';
44
import {
55
initializeMocks, render as baseRender, screen, waitFor,
66
fireEvent,
7-
} from '../../testUtils';
7+
within,
8+
} from '@src/testUtils';
9+
import { ContainerType } from '@src/generic/key-utils';
10+
import { mockContentSearchConfig, mockSearchResult } from '@src/search-manager/data/api.mock';
11+
import type { ToastActionData } from '@src/generic/toast-context';
812
import { mockContentLibrary, mockGetContainerChildren, mockGetContainerMetadata } from '../data/api.mocks';
913
import { LibraryProvider } from '../common/context/LibraryContext';
1014
import ContainerInfo from './ContainerInfo';
1115
import { getLibraryContainerApiUrl, getLibraryContainerPublishApiUrl } from '../data/api';
1216
import { SidebarBodyItemId, SidebarProvider } from '../common/context/SidebarContext';
13-
import { ContainerType } from '../../generic/key-utils';
14-
import { mockContentSearchConfig, mockSearchResult } from '../../search-manager/data/api.mock';
15-
import type { ToastActionData } from '../../generic/toast-context';
1617

17-
mockGetContainerMetadata.applyMock();
1818
mockContentLibrary.applyMock();
1919
mockContentSearchConfig.applyMock();
2020
mockGetContainerMetadata.applyMock();
@@ -23,6 +23,12 @@ mockGetContainerChildren.applyMock();
2323
const { libraryId } = mockContentLibrary;
2424
const { unitId, subsectionId, sectionId } = mockGetContainerMetadata;
2525

26+
const mockNavigate = jest.fn();
27+
jest.mock('react-router-dom', () => ({
28+
...jest.requireActual('react-router-dom'),
29+
useNavigate: () => mockNavigate,
30+
}));
31+
2632
const render = (containerId: string, showOnlyPublished: boolean = false) => {
2733
const params: { libraryId: string, selectedItemId?: string } = { libraryId, selectedItemId: containerId };
2834
return baseRender(<ContainerInfo />, {
@@ -141,6 +147,7 @@ let mockShowToast: { (message: string, action?: ToastActionData | undefined): vo
141147
});
142148

143149
it(`shows the ${containerType} Preview tab by default and the children are readonly`, async () => {
150+
const user = userEvent.setup();
144151
render(containerId);
145152
const previewTab = await screen.findByText('Preview');
146153
expect(previewTab).toBeInTheDocument();
@@ -149,11 +156,36 @@ let mockShowToast: { (message: string, action?: ToastActionData | undefined): vo
149156
// Check that there are no edit buttons for components titles
150157
expect(screen.queryAllByRole('button', { name: /edit/i }).length).toBe(0);
151158

152-
// Check that there are no drag handle for components
159+
// Check that there are no drag handle for components/containers
153160
expect(screen.queryAllByRole('button', { name: 'Drag to reorder' }).length).toBe(0);
154161

155162
// Check that there are no menu buttons for components
156163
expect(screen.queryAllByRole('button', { name: /component actions menu/i }).length).toBe(0);
164+
165+
let childType: string;
166+
switch (containerType) {
167+
case ContainerType.Section:
168+
childType = ContainerType.Subsection;
169+
break;
170+
case ContainerType.Subsection:
171+
childType = ContainerType.Unit;
172+
break;
173+
case ContainerType.Unit:
174+
childType = 'text';
175+
break;
176+
default:
177+
break;
178+
}
179+
const child = await screen.findByText(`${childType!} block 0`);
180+
screen.debug(child.parentElement!.parentElement!.parentElement!);
181+
// Check that there are no menu buttons for containers
182+
expect(within(
183+
child.parentElement!.parentElement!.parentElement!,
184+
).queryAllByRole('button', { name: /container actions menu/i }).length).toBe(0);
185+
// Trigger double click. Find the child card as the parent element
186+
await user.dblClick(child.parentElement!.parentElement!.parentElement!);
187+
// Click should not do anything in preview
188+
expect(mockNavigate).not.toHaveBeenCalled();
157189
});
158190
});
159191
});

src/library-authoring/section-subsections/LibraryContainerChildren.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,17 @@ export const LibraryContainerChildren = ({ containerKey, readOnly }: LibraryCont
166166
}, [children, setOrderedChildren]);
167167

168168
const handleChildClick = useCallback((child: LibraryContainerMetadataWithUniqueId, numberOfClicks: number) => {
169+
if (readOnly) {
170+
// don't allow interaction if rendered as preview
171+
return;
172+
}
169173
const doubleClicked = numberOfClicks > 1;
170174
if (!doubleClicked) {
171175
openItemSidebar(child.originalId, SidebarBodyItemId.ContainerInfo);
172176
} else {
173177
navigateTo({ containerId: child.originalId });
174178
}
175-
}, [openItemSidebar, navigateTo]);
179+
}, [openItemSidebar, navigateTo, readOnly]);
176180

177181
const getComponentStyle = useCallback((childId: string) => {
178182
const style: { marginBottom: string, borderRadius: string, outline?: string } = {
@@ -225,7 +229,7 @@ export const LibraryContainerChildren = ({ containerKey, readOnly }: LibraryCont
225229
borderRadius: '8px',
226230
borderLeft: '8px solid #E1DDDB',
227231
}}
228-
isClickable
232+
isClickable={!readOnly}
229233
onClick={(e) => handleChildClick(child, e.detail)}
230234
onKeyDown={(e) => {
231235
if (e.key === 'Enter') {

src/library-authoring/units/LibraryUnitBlocks.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,17 @@ const ComponentBlock = ({ block, readOnly, isDragging }: ComponentBlockProps) =>
135135
const { sidebarItemInfo, openItemSidebar } = useSidebarContext();
136136

137137
const handleComponentSelection = useCallback((numberOfClicks: number) => {
138+
if (readOnly) {
139+
// don't allow interaction if rendered as preview
140+
return;
141+
}
138142
openItemSidebar(block.originalId, SidebarBodyItemId.ComponentInfo);
139143
const canEdit = canEditComponent(block.originalId);
140144
if (numberOfClicks > 1 && canEdit) {
141145
// Open editor on double click.
142146
openComponentEditor(block.originalId);
143147
}
144-
}, [block, openItemSidebar, canEditComponent, openComponentEditor]);
148+
}, [block, openItemSidebar, canEditComponent, openComponentEditor, readOnly]);
145149

146150
useEffect(() => {
147151
if (block.isNew) {
@@ -181,7 +185,7 @@ const ComponentBlock = ({ block, readOnly, isDragging }: ComponentBlockProps) =>
181185
borderBottom: 'solid 1px #E1DDDB',
182186
}}
183187
isClickable={!readOnly}
184-
onClick={(e) => !readOnly && handleComponentSelection(e.detail)}
188+
onClick={(e) => handleComponentSelection(e.detail)}
185189
onKeyDown={(e) => {
186190
if (e.key === 'Enter') {
187191
handleComponentSelection(e.detail);

0 commit comments

Comments
 (0)