From 6c489d19f4933ea4f48ca758ed412a21c39db57d Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Wed, 13 Nov 2024 10:29:40 +0530 Subject: [PATCH] refactor: remove ComponentPickerModal component Replace with standard modal and component picker components. --- src/library-authoring/LibraryLayout.tsx | 8 ++-- .../add-content/AddContentContainer.tsx | 4 +- .../PickLibraryContentModal.test.tsx | 4 +- .../add-content/PickLibraryContentModal.tsx | 27 +++++++----- src/library-authoring/common/context.tsx | 26 +++++------ .../component-picker/ComponentPickerModal.tsx | 43 ------------------- .../component-picker/index.ts | 1 - 7 files changed, 38 insertions(+), 75 deletions(-) delete mode 100644 src/library-authoring/component-picker/ComponentPickerModal.tsx diff --git a/src/library-authoring/LibraryLayout.tsx b/src/library-authoring/LibraryLayout.tsx index 09cb45b3d3..fb67cb893c 100644 --- a/src/library-authoring/LibraryLayout.tsx +++ b/src/library-authoring/LibraryLayout.tsx @@ -10,7 +10,7 @@ import { LibraryProvider } from './common/context'; import { CreateCollectionModal } from './create-collection'; import { LibraryTeamModal } from './library-team'; import LibraryCollectionPage from './collections/LibraryCollectionPage'; -import { ComponentPickerModal } from './component-picker'; +import { ComponentPicker} from './component-picker'; import { ComponentEditorModal } from './components/ComponentEditorModal'; const LibraryLayout = () => { @@ -32,9 +32,9 @@ const LibraryLayout = () => { collectionId={collectionId} /** The component picker modal to use. We need to pass it as a reference instead of * directly importing it to avoid the import cycle: - * ComponentPickerModal > ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > - * Sidebar > AddContentContainer > ComponentPickerModal */ - componentPickerModal={ComponentPickerModal} + * ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > + * Sidebar > AddContentContainer > ComponentPicker */ + componentPicker={ComponentPicker} > { collectionId, openCreateCollectionModal, openComponentEditor, - componentPickerModal, + componentPicker, } = useLibraryContext(); const createBlockMutation = useCreateLibraryBlock(); const updateComponentsMutation = useAddComponentsToCollection(libraryId, collectionId); @@ -239,7 +239,7 @@ const AddContentContainer = () => { return ( {collectionId ? ( - componentPickerModal && ( + componentPicker && ( <> baseRender( {children} diff --git a/src/library-authoring/add-content/PickLibraryContentModal.tsx b/src/library-authoring/add-content/PickLibraryContentModal.tsx index f8a8ac3965..ef08b68ec0 100644 --- a/src/library-authoring/add-content/PickLibraryContentModal.tsx +++ b/src/library-authoring/add-content/PickLibraryContentModal.tsx @@ -1,6 +1,6 @@ import React, { useCallback, useContext, useState } from 'react'; import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; -import { ActionRow, Button } from '@openedx/paragon'; +import { ActionRow, Button, StandardModal } from '@openedx/paragon'; import { ToastContext } from '../../generic/toast-context'; import { type SelectedComponent, useLibraryContext } from '../common/context'; @@ -41,14 +41,14 @@ export const PickLibraryContentModal: React.FC = ( libraryId, collectionId, /** We need to get it as a reference instead of directly importing it to avoid the import cycle: - * ComponentPickerModal > ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > - * Sidebar > AddContentContainer > ComponentPickerModal */ - componentPickerModal: ComponentPickerModal, + * ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > + * Sidebar > AddContentContainer > ComponentPicker */ + componentPicker: ComponentPicker, } = useLibraryContext(); // istanbul ignore if: this should never happen - if (!collectionId || !ComponentPickerModal) { - throw new Error('libraryId and componentPickerModal are required'); + if (!collectionId || !ComponentPicker) { + throw new Error('libraryId and componentPicker are required'); } const updateComponentsMutation = useAddComponentsToCollection(libraryId, collectionId); @@ -70,12 +70,19 @@ export const PickLibraryContentModal: React.FC = ( }, [selectedComponents]); return ( - } - /> + > + + ); }; diff --git a/src/library-authoring/common/context.tsx b/src/library-authoring/common/context.tsx index be229065f5..4e3ac78cc5 100644 --- a/src/library-authoring/common/context.tsx +++ b/src/library-authoring/common/context.tsx @@ -6,7 +6,7 @@ import React, { useState, } from 'react'; -import type { ComponentPickerModal } from '../component-picker'; +import type { ComponentPicker } from '../component-picker'; import type { ContentLibrary } from '../data/api'; import { useContentLibrary } from '../data/apiHooks'; @@ -27,9 +27,9 @@ type NoComponentPickerType = { restrictToLibrary?: never; /** The component picker modal to use. We need to pass it as a reference instead of * directly importing it to avoid the import cycle: - * ComponentPickerModal > ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > - * Sidebar > AddContentContainer > ComponentPickerModal */ - componentPickerModal?: typeof ComponentPickerModal; + * ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage > + * Sidebar > AddContentContainer > ComponentPicker */ + componentPicker?: typeof ComponentPicker; }; type ComponentPickerSingleType = { @@ -39,7 +39,7 @@ type ComponentPickerSingleType = { addComponentToSelectedComponents?: never; removeComponentFromSelectedComponents?: never; restrictToLibrary: boolean; - componentPickerModal?: never; + componentPicker?: never; }; type ComponentPickerMultipleType = { @@ -49,7 +49,7 @@ type ComponentPickerMultipleType = { addComponentToSelectedComponents: ComponentSelectedEvent; removeComponentFromSelectedComponents: ComponentSelectedEvent; restrictToLibrary: boolean; - componentPickerModal?: never; + componentPicker?: never; }; type ComponentPickerType = NoComponentPickerType | ComponentPickerSingleType | ComponentPickerMultipleType; @@ -121,7 +121,7 @@ type NoComponentPickerProps = { onComponentSelected?: never; onChangeComponentSelection?: never; restrictToLibrary?: never; - componentPickerModal?: typeof ComponentPickerModal; + componentPicker?: typeof ComponentPicker; }; export type ComponentPickerSingleProps = { @@ -129,7 +129,7 @@ export type ComponentPickerSingleProps = { onComponentSelected: ComponentSelectedEvent; onChangeComponentSelection?: never; restrictToLibrary?: boolean; - componentPickerModal?: never; + componentPicker?: never; }; export type ComponentPickerMultipleProps = { @@ -137,7 +137,7 @@ export type ComponentPickerMultipleProps = { onComponentSelected?: never; onChangeComponentSelection?: ComponentSelectionChangedEvent; restrictToLibrary?: boolean; - componentPickerModal?: never; + componentPicker?: never; }; type ComponentPickerProps = NoComponentPickerProps | ComponentPickerSingleProps | ComponentPickerMultipleProps; @@ -150,7 +150,7 @@ type LibraryProviderProps = { showOnlyPublished?: boolean; /** Only used for testing */ initialSidebarComponentInfo?: SidebarComponentInfo; - componentPickerModal?: typeof ComponentPickerModal; + componentPicker?: typeof ComponentPicker; } & ComponentPickerProps; /** @@ -166,7 +166,7 @@ export const LibraryProvider = ({ onChangeComponentSelection, showOnlyPublished = false, initialSidebarComponentInfo, - componentPickerModal, + componentPicker, }: LibraryProviderProps) => { const [collectionId, setCollectionId] = useState(collectionIdProp); const [sidebarComponentInfo, setSidebarComponentInfo] = useState( @@ -276,7 +276,7 @@ export const LibraryProvider = ({ if (!componentPickerMode) { return { ...contextValue, - componentPickerModal, + componentPicker, }; } if (componentPickerMode === 'single') { @@ -329,7 +329,7 @@ export const LibraryProvider = ({ openComponentEditor, closeComponentEditor, resetSidebarAdditionalActions, - componentPickerModal, + componentPicker, ]); return ( diff --git a/src/library-authoring/component-picker/ComponentPickerModal.tsx b/src/library-authoring/component-picker/ComponentPickerModal.tsx deleted file mode 100644 index 5d6e6ae30b..0000000000 --- a/src/library-authoring/component-picker/ComponentPickerModal.tsx +++ /dev/null @@ -1,43 +0,0 @@ -import React from 'react'; -import { StandardModal } from '@openedx/paragon'; - -import type { ComponentSelectionChangedEvent } from '../common/context'; -import { ComponentPicker } from './ComponentPicker'; - -interface ComponentPickerModalProps { - libraryId?: string; - isOpen: boolean; - onClose: () => void; - onChangeComponentSelection: ComponentSelectionChangedEvent; - footerNode?: React.ReactNode; -} - -// eslint-disable-next-line import/prefer-default-export -export const ComponentPickerModal: React.FC = ({ - libraryId, - isOpen, - onClose, - onChangeComponentSelection, - footerNode, -}) => { - if (!isOpen) { - return null; - } - - return ( - - - - ); -}; diff --git a/src/library-authoring/component-picker/index.ts b/src/library-authoring/component-picker/index.ts index 5ffe86d0f8..5d2ce68f76 100644 --- a/src/library-authoring/component-picker/index.ts +++ b/src/library-authoring/component-picker/index.ts @@ -1,2 +1 @@ export { ComponentPicker } from './ComponentPicker'; -export { ComponentPickerModal } from './ComponentPickerModal';