Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/course-unit/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';

import { PUBLISH_TYPES } from '../constants';
import { normalizeCourseSectionVerticalData, updateXBlockBlockIdToId } from './utils';
import { isUnitReadOnly, normalizeCourseSectionVerticalData, updateXBlockBlockIdToId } from './utils';

const getStudioBaseUrl = () => getConfig().STUDIO_BASE_URL;

Expand All @@ -24,7 +24,9 @@ export async function getCourseUnitData(unitId) {
const { data } = await getAuthenticatedHttpClient()
.get(getCourseUnitApiUrl(unitId));

return camelCaseObject(data);
const result = camelCaseObject(data);
result.readOnly = isUnitReadOnly(result);
return result;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/course-unit/data/thunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ import {
updateCourseOutlineInfoLoadingStatus,
updateMovedXBlockParams,
} from './slice';
import { getNotificationMessage, isUnitReadOnly } from './utils';
import { getNotificationMessage } from './utils';

export function fetchCourseUnitQuery(courseId) {
return async (dispatch) => {
dispatch(updateLoadingCourseUnitStatus({ status: RequestStatus.IN_PROGRESS }));

try {
const courseUnit = await getCourseUnitData(courseId);
courseUnit.readOnly = isUnitReadOnly(courseUnit);

dispatch(fetchCourseItemSuccess(courseUnit));
dispatch(updateLoadingCourseUnitStatus({ status: RequestStatus.SUCCESSFUL }));
Expand Down
4 changes: 3 additions & 1 deletion src/library-authoring/components/ContainerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ const ContainerCard = ({ hit } : ContainerCardProps) => {
if (itemType === 'unit') {
openUnitInfoSidebar(unitId);
setUnitId(unitId);
navigateTo({ unitId });
if (!componentPickerMode) {
navigateTo({ unitId });
}
}
}, [unitId, itemType, openUnitInfoSidebar, navigateTo]);

Expand Down
12 changes: 10 additions & 2 deletions src/library-authoring/containers/UnitInfo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
initializeMocks, render as baseRender, screen, waitFor,
fireEvent,
} from '../../testUtils';
import { mockContentLibrary, mockGetContainerMetadata } from '../data/api.mocks';
import { mockContentLibrary, mockGetContainerChildren, mockGetContainerMetadata } from '../data/api.mocks';
import { LibraryProvider } from '../common/context/LibraryContext';
import UnitInfo from './UnitInfo';
import { getLibraryContainerApiUrl, getLibraryContainerPublishApiUrl } from '../data/api';
Expand All @@ -14,14 +14,16 @@ import { SidebarBodyComponentId, SidebarProvider } from '../common/context/Sideb
mockGetContainerMetadata.applyMock();
mockContentLibrary.applyMock();
mockGetContainerMetadata.applyMock();
mockGetContainerChildren.applyMock();

const { libraryId } = mockContentLibrary;
const { containerId } = mockGetContainerMetadata;

const render = () => baseRender(<UnitInfo />, {
const render = (showOnlyPublished: boolean = false) => baseRender(<UnitInfo />, {
extraWrapper: ({ children }) => (
<LibraryProvider
libraryId={libraryId}
showOnlyPublished={showOnlyPublished}
>
<SidebarProvider
initialSidebarComponentInfo={{
Expand Down Expand Up @@ -95,4 +97,10 @@ describe('<UnitInfo />', () => {
});
expect(mockShowToast).toHaveBeenCalledWith('Failed to publish changes');
});

it('show only published content', async () => {
render(true);
expect(await screen.findByTestId('unit-info-menu-toggle')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /text block published 1/i })).toBeInTheDocument();
});
});
9 changes: 9 additions & 0 deletions src/library-authoring/data/api.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ mockCreateLibraryBlock.newHtmlData = {
id: 'lb:Axim:TEST:html:123',
blockType: 'html',
displayName: 'New Text Component',
publishedDisplayName: null,
hasUnpublishedChanges: true,
lastPublished: null, // or e.g. '2024-08-30T16:37:42Z',
publishedBy: null, // or e.g. 'test_author',
Expand All @@ -202,6 +203,7 @@ mockCreateLibraryBlock.newProblemData = {
id: 'lb:Axim:TEST:problem:prob1',
blockType: 'problem',
displayName: 'New Problem',
publishedDisplayName: null,
hasUnpublishedChanges: true,
lastPublished: null, // or e.g. '2024-08-30T16:37:42Z',
publishedBy: null, // or e.g. 'test_author',
Expand All @@ -216,6 +218,7 @@ mockCreateLibraryBlock.newVideoData = {
id: 'lb:Axim:TEST:video:vid1',
blockType: 'video',
displayName: 'New Video',
publishedDisplayName: null,
hasUnpublishedChanges: true,
lastPublished: null, // or e.g. '2024-08-30T16:37:42Z',
publishedBy: null, // or e.g. 'test_author',
Expand Down Expand Up @@ -348,6 +351,7 @@ mockLibraryBlockMetadata.dataNeverPublished = {
id: 'lb:Axim:TEST1:html:571fe018-f3ce-45c9-8f53-5dafcb422fd1',
blockType: 'html',
displayName: 'Introduction to Testing 1',
publishedDisplayName: null,
lastPublished: null,
publishedBy: null,
lastDraftCreated: null,
Expand All @@ -363,6 +367,7 @@ mockLibraryBlockMetadata.dataPublished = {
id: 'lb:Axim:TEST2:html:571fe018-f3ce-45c9-8f53-5dafcb422fd2',
blockType: 'html',
displayName: 'Introduction to Testing 2',
publishedDisplayName: 'Introduction to Testing 2',
lastPublished: '2024-06-22T00:00:00',
publishedBy: 'Luke',
lastDraftCreated: null,
Expand Down Expand Up @@ -391,6 +396,7 @@ mockLibraryBlockMetadata.dataWithCollections = {
id: 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fdd',
blockType: 'html',
displayName: 'Introduction to Testing 2',
publishedDisplayName: null,
lastPublished: '2024-06-21T00:00:00',
publishedBy: 'Luke',
lastDraftCreated: null,
Expand All @@ -407,6 +413,7 @@ mockLibraryBlockMetadata.dataPublishedWithChanges = {
id: 'lb:Axim:TEST2:html:571fe018-f3ce-45c9-8f53-5dafcb422fvv',
blockType: 'html',
displayName: 'Introduction to Testing 2',
publishedDisplayName: 'Introduction to Testing 3',
lastPublished: '2024-06-22T00:00:00',
publishedBy: 'Luke',
lastDraftCreated: null,
Expand Down Expand Up @@ -536,6 +543,7 @@ export async function mockGetContainerChildren(containerId: string): Promise<api
// Generate a unique ID for each child block to avoid "duplicate key" errors in tests
id: `lb:org1:Demo_course:html:text-${idx}`,
displayName: `text block ${idx}`,
publishedDisplayName: `text block published ${idx}`,
}
)),
);
Expand All @@ -546,6 +554,7 @@ mockGetContainerChildren.childTemplate = {
id: 'lb:org1:Demo_course:html:text',
blockType: 'html',
displayName: 'text block',
publishedDisplayName: 'text block published',
lastPublished: null,
publishedBy: null,
lastDraftCreated: null,
Expand Down
12 changes: 9 additions & 3 deletions src/library-authoring/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getLibraryContainerRestoreApiUrl = (containerId: string) => `${getL
/**
* Get the URL for a single container children api.
*/
export const getLibraryContainerChildrenApiUrl = (containerId: string) => `${getLibraryContainerApiUrl(containerId)}children/`;
export const getLibraryContainerChildrenApiUrl = (containerId: string, published: boolean = false) => `${getLibraryContainerApiUrl(containerId)}children/?published=${published}`;
/**
* Get the URL for library container collections.
*/
Expand Down Expand Up @@ -250,6 +250,7 @@ export interface LibraryBlockMetadata {
id: string;
blockType: string;
displayName: string;
publishedDisplayName: string | null;
lastPublished: string | null;
publishedBy: string | null;
lastDraftCreated: string | null;
Expand Down Expand Up @@ -652,8 +653,13 @@ export async function restoreContainer(containerId: string) {
/**
* Fetch a library container's children's metadata.
*/
export async function getLibraryContainerChildren(containerId: string): Promise<LibraryBlockMetadata[]> {
const { data } = await getAuthenticatedHttpClient().get(getLibraryContainerChildrenApiUrl(containerId));
export async function getLibraryContainerChildren(
containerId: string,
published: boolean = false,
): Promise<LibraryBlockMetadata[]> {
const { data } = await getAuthenticatedHttpClient().get(
getLibraryContainerChildrenApiUrl(containerId, published),
);
return camelCaseObject(data);
}

Expand Down
4 changes: 2 additions & 2 deletions src/library-authoring/data/apiHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ export const useRestoreContainer = (containerId: string) => {
/**
* Get the metadata and children for a container in a library
*/
export const useContainerChildren = (containerId?: string) => (
export const useContainerChildren = (containerId?: string, published: boolean = false) => (
useQuery({
enabled: !!containerId,
queryKey: libraryAuthoringQueryKeys.containerChildren(containerId!),
queryFn: () => api.getLibraryContainerChildren(containerId!),
queryFn: () => api.getLibraryContainerChildren(containerId!, published),
structuralSharing: (oldData: api.LibraryBlockMetadata[], newData: api.LibraryBlockMetadata[]) => {
// This just sets `isNew` flag to new children components
if (oldData) {
Expand Down
11 changes: 7 additions & 4 deletions src/library-authoring/units/LibraryUnitBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface ComponentBlockProps {
/** Component header */
const BlockHeader = ({ block }: ComponentBlockProps) => {
const intl = useIntl();
const { showOnlyPublished } = useLibraryContext();
const { showToast } = useContext(ToastContext);
const { navigateTo } = useLibraryRoutes();
const { openComponentInfoSidebar, setSidebarAction } = useSidebarContext();
Expand Down Expand Up @@ -101,7 +102,7 @@ const BlockHeader = ({ block }: ComponentBlockProps) => {
<Icon src={getItemIcon(block.blockType)} />
<InplaceTextEditor
onSave={handleSaveDisplayName}
text={block.displayName}
text={showOnlyPublished ? (block.publishedDisplayName ?? block.displayName) : block.displayName}
/>
</Stack>
<ActionRow.Spacer />
Expand All @@ -112,7 +113,7 @@ const BlockHeader = ({ block }: ComponentBlockProps) => {
/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
onClick={(e) => e.stopPropagation()}
>
{block.hasUnpublishedChanges && (
{!showOnlyPublished && block.hasUnpublishedChanges && (
<Badge
className="px-2 py-1"
variant="warning"
Expand Down Expand Up @@ -233,7 +234,9 @@ export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
const [hidePreviewFor, setHidePreviewFor] = useState<string | null>(null);
const { showToast } = useContext(ToastContext);

const { unitId, readOnly } = useLibraryContext();
const { readOnly, showOnlyPublished } = useLibraryContext();
const { sidebarComponentInfo } = useSidebarContext();
const unitId = sidebarComponentInfo?.id;

const { openAddContentSidebar } = useSidebarContext();

Expand All @@ -243,7 +246,7 @@ export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
isLoading,
isError,
error,
} = useContainerChildren(unitId);
} = useContainerChildren(unitId, showOnlyPublished);

const handleReorder = useCallback(() => async (newOrder?: LibraryBlockMetadataWithUniqueId[]) => {
if (!newOrder) {
Expand Down