Skip to content

Commit

Permalink
feat: add 'single' and 'multiple' componentPickerModes
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Oct 21, 2024
1 parent 21423e3 commit 0831ba7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/library-authoring/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import React, {
import type { ContentLibrary } from '../data/api';
import { useContentLibrary } from '../data/apiHooks';

export type ComponentPickerMode = 'single' | 'multiple';

export enum SidebarBodyComponentId {
AddContent = 'add-content',
Info = 'info',
Expand All @@ -25,7 +27,7 @@ export interface LibraryContextData {
collectionId: string | undefined;
setCollectionId: (collectionId?: string) => void;
// Whether we're in "component picker" mode
componentPickerMode: boolean;
componentPickerMode?: ComponentPickerMode;
onComponentSelected?: (usageKey: string, category: string) => void;
// Sidebar stuff - only one sidebar is active at any given time:
sidebarBodyComponent: SidebarBodyComponentId | null;
Expand Down Expand Up @@ -69,7 +71,7 @@ interface LibraryProviderProps {
collectionId?: string;
/** The component picker mode is a special mode where the user is selecting a component to add to a Unit (or another
* XBlock) */
componentPickerMode?: boolean;
componentPickerMode?: ComponentPickerMode;
/** Function to call when a component is selected */
onComponentSelected?: (usageKey: string, category: string) => void;
/** Only used for testing */
Expand All @@ -85,7 +87,7 @@ export const LibraryProvider = ({
children,
libraryId,
collectionId: collectionIdProp,
componentPickerMode = false,
componentPickerMode,
onComponentSelected,
initialSidebarComponentUsageKey,
initialSidebarCollectionId,
Expand Down Expand Up @@ -134,7 +136,7 @@ export const LibraryProvider = ({

const { data: libraryData, isLoading: isLoadingLibraryData } = useContentLibrary(libraryId);

const readOnly = componentPickerMode || !libraryData?.canEditLibrary;
const readOnly = !!componentPickerMode || !libraryData?.canEditLibrary;

const context = useMemo<LibraryContextData>(() => ({
libraryId,
Expand Down
13 changes: 11 additions & 2 deletions src/library-authoring/component-picker/ComponentPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { Stepper } from '@openedx/paragon';

import { LibraryProvider, useLibraryContext } from '../common/context';
import { type ComponentPickerMode, LibraryProvider, useLibraryContext } from '../common/context';
import LibraryAuthoringPage from '../LibraryAuthoringPage';
import LibraryCollectionPage from '../collections/LibraryCollectionPage';
import SelectLibrary from './SelectLibrary';
Expand Down Expand Up @@ -29,10 +29,15 @@ const defaultComponentSelectedCallback = (usageKey: string, category: string) =>

interface ComponentPickerProps {
onComponentSelected?: (usageKey: string, category: string) => void;
componentPickerMode?: ComponentPickerMode;
}

// eslint-disable-next-line import/prefer-default-export
export const ComponentPicker: React.FC<ComponentPickerProps> = ({
componentPickerMode = 'single',
/** This default callback is used to send the selected component back to the parent window,
* when the component picker is used in an iframe.
*/
onComponentSelected = defaultComponentSelectedCallback,
}) => {
const [currentStep, setCurrentStep] = useState('select-library');
Expand All @@ -57,7 +62,11 @@ export const ComponentPicker: React.FC<ComponentPickerProps> = ({
</Stepper.Step>

<Stepper.Step eventKey="pick-components" title="Pick some components">
<LibraryProvider libraryId={selectedLibrary} componentPickerMode onComponentSelected={onComponentSelected}>
<LibraryProvider
libraryId={selectedLibrary}
componentPickerMode={componentPickerMode}
onComponentSelected={onComponentSelected}
>
<InnerComponentPicker returnToLibrarySelection={returnToLibrarySelection} />
</LibraryProvider>
</Stepper.Step>
Expand Down

0 comments on commit 0831ba7

Please sign in to comment.