Skip to content

Commit

Permalink
refactor: add onComponentSelected callback function
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Oct 21, 2024
1 parent d49fc85 commit 21423e3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/library-authoring/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface LibraryContextData {
setCollectionId: (collectionId?: string) => void;
// Whether we're in "component picker" mode
componentPickerMode: boolean;
onComponentSelected?: (usageKey: string, category: string) => void;
// Sidebar stuff - only one sidebar is active at any given time:
sidebarBodyComponent: SidebarBodyComponentId | null;
closeLibrarySidebar: () => void;
Expand Down Expand Up @@ -69,6 +70,8 @@ interface LibraryProviderProps {
/** 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;
/** Function to call when a component is selected */
onComponentSelected?: (usageKey: string, category: string) => void;
/** Only used for testing */
initialSidebarComponentUsageKey?: string;
/** Only used for testing */
Expand All @@ -83,6 +86,7 @@ export const LibraryProvider = ({
libraryId,
collectionId: collectionIdProp,
componentPickerMode = false,
onComponentSelected,
initialSidebarComponentUsageKey,
initialSidebarCollectionId,
}: LibraryProviderProps) => {
Expand Down Expand Up @@ -140,6 +144,7 @@ export const LibraryProvider = ({
readOnly,
isLoadingLibraryData,
componentPickerMode,
onComponentSelected,
sidebarBodyComponent,
closeLibrarySidebar,
openAddContentSidebar,
Expand All @@ -165,6 +170,7 @@ export const LibraryProvider = ({
readOnly,
isLoadingLibraryData,
componentPickerMode,
onComponentSelected,
sidebarBodyComponent,
closeLibrarySidebar,
openAddContentSidebar,
Expand Down
12 changes: 5 additions & 7 deletions src/library-authoring/component-info/ComponentInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Tabs,
Stack,
} from '@openedx/paragon';
import { useCallback } from 'react';

import { useLibraryContext } from '../common/context';
import { ComponentMenu } from '../components';
Expand All @@ -23,6 +24,7 @@ const ComponentInfo = () => {
readOnly,
openComponentEditor,
componentPickerMode,
onComponentSelected,
} = useLibraryContext();

// istanbul ignore if: this should never happen
Expand All @@ -32,13 +34,9 @@ const ComponentInfo = () => {

const canEdit = canEditComponent(usageKey);

const handleAddComponentToCourse = () => {
window.parent.postMessage({
usageKey,
type: 'pickerComponentSelected',
category: getBlockType(usageKey),
}, '*');
};
const handleAddComponentToCourse = useCallback(() => {
onComponentSelected?.(usageKey, getBlockType(usageKey));
}, [usageKey]);

return (
<Stack>
Expand Down
18 changes: 16 additions & 2 deletions src/library-authoring/component-picker/ComponentPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,22 @@ const InnerComponentPicker: React.FC<LibraryComponentPickerProps> = ({ returnToL
return <LibraryAuthoringPage returnToLibrarySelection={returnToLibrarySelection} />;
};

const defaultComponentSelectedCallback = (usageKey: string, category: string) => {
window.parent.postMessage({
usageKey,
type: 'pickerComponentSelected',
category,
}, '*');
};

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

// eslint-disable-next-line import/prefer-default-export
export const ComponentPicker = () => {
export const ComponentPicker: React.FC<ComponentPickerProps> = ({
onComponentSelected = defaultComponentSelectedCallback,
}) => {
const [currentStep, setCurrentStep] = useState('select-library');
const [selectedLibrary, setSelectedLibrary] = useState('');

Expand All @@ -43,7 +57,7 @@ export const ComponentPicker = () => {
</Stepper.Step>

<Stepper.Step eventKey="pick-components" title="Pick some components">
<LibraryProvider libraryId={selectedLibrary} componentPickerMode>
<LibraryProvider libraryId={selectedLibrary} componentPickerMode onComponentSelected={onComponentSelected}>
<InnerComponentPicker returnToLibrarySelection={returnToLibrarySelection} />
</LibraryProvider>
</Stepper.Step>
Expand Down
7 changes: 2 additions & 5 deletions src/library-authoring/components/ComponentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => {
const {
openComponentInfoSidebar,
componentPickerMode,
onComponentSelected,
} = useLibraryContext();

const {
Expand All @@ -109,11 +110,7 @@ const ComponentCard = ({ contentHit }: ComponentCardProps) => {
const displayName = formatted?.displayName ?? '';

const handleAddComponentToCourse = () => {
window.parent.postMessage({
usageKey,
type: 'pickerComponentSelected',
category: blockType,
}, '*');
onComponentSelected?.(usageKey, blockType);
};

return (
Expand Down

0 comments on commit 21423e3

Please sign in to comment.