Skip to content

Commit

Permalink
fix: import cyle
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Oct 23, 2024
1 parent a89d4da commit 3fe699f
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/library-authoring/LibraryAuthoringPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
import LibraryComponents from './components/LibraryComponents';
import LibraryCollections from './collections/LibraryCollections';
import LibraryHome from './LibraryHome';
// eslint-disable-next-line import/no-cycle
import { LibrarySidebar } from './library-sidebar';
import { SidebarBodyComponentId, useLibraryContext } from './common/context';
import messages from './messages';
Expand Down
12 changes: 11 additions & 1 deletion src/library-authoring/LibraryLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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 { ComponentEditorModal } from './components/ComponentEditorModal';

const LibraryLayout = () => {
Expand All @@ -25,7 +26,16 @@ const LibraryLayout = () => {
}

return (
<LibraryProvider key={collectionId} libraryId={libraryId} collectionId={collectionId}>
<LibraryProvider
key={collectionId}
libraryId={libraryId}
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}
>
<Routes>
<Route
path="collection/:collectionId"
Expand Down
21 changes: 11 additions & 10 deletions src/library-authoring/add-content/AddContentContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import {
ContentPaste,
} from '@openedx/paragon/icons';
import { v4 as uuid4 } from 'uuid';
import { useParams } from 'react-router-dom';

import { ToastContext } from '../../generic/toast-context';
import { useCopyToClipboard } from '../../generic/clipboard';
import { getCanEdit } from '../../course-unit/data/selectors';
import { useCreateLibraryBlock, useLibraryPasteClipboard, useAddComponentsToCollection } from '../data/apiHooks';
import { useLibraryContext } from '../common/context';
import { canEditComponent } from '../components/ComponentEditorModal';
// eslint-disable-next-line import/no-cycle
import { PickLibraryContentModal } from './PickLibraryContentModal';

import messages from './messages';
Expand Down Expand Up @@ -66,11 +64,12 @@ const AddContentButton = ({ contentType, onCreateContent } : AddContentButtonPro

const AddContentContainer = () => {
const intl = useIntl();
const { collectionId } = useParams();
const {
libraryId,
collectionId,
openCreateCollectionModal,
openComponentEditor,
componentPickerModal,
} = useLibraryContext();
const createBlockMutation = useCreateLibraryBlock();
const updateComponentsMutation = useAddComponentsToCollection(libraryId, collectionId);
Expand Down Expand Up @@ -214,13 +213,15 @@ const AddContentContainer = () => {
return (
<Stack direction="vertical">
{collectionId ? (
<>
<AddContentButton contentType={libraryContentButtonData} onCreateContent={onCreateContent} />
<PickLibraryContentModal
isOpen={isAddLibraryContentModalOpen}
onClose={closeAddLibraryContentModal}
/>
</>
componentPickerModal && (
<>
<AddContentButton contentType={libraryContentButtonData} onCreateContent={onCreateContent} />
<PickLibraryContentModal
isOpen={isAddLibraryContentModalOpen}
onClose={closeAddLibraryContentModal}
/>
</>
)
) : (
<AddContentButton contentType={collectionButtonData} onCreateContent={onCreateContent} />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../testUtils';
import mockResult from '../__mocks__/library-search.json';
import { LibraryProvider } from '../common/context';
import { ComponentPickerModal } from '../component-picker';
import * as api from '../data/api';
import {
mockContentLibrary,
Expand Down Expand Up @@ -35,6 +36,7 @@ const render = () => baseRender(<PickLibraryContentModal isOpen onClose={onClose
<LibraryProvider
libraryId={libraryId}
collectionId="collectionId"
componentPickerModal={ComponentPickerModal}
>
{children}
</LibraryProvider>
Expand Down
13 changes: 9 additions & 4 deletions src/library-authoring/add-content/PickLibraryContentModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import React, { useCallback, useContext, useState } from 'react';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import { ActionRow, Button } from '@openedx/paragon';
import { useParams } from 'react-router-dom';

import { ToastContext } from '../../generic/toast-context';
import { type SelectedComponent, useLibraryContext } from '../common/context';
// eslint-disable-next-line import/no-cycle
import { ComponentPickerModal } from '../component-picker';
import { useAddComponentsToCollection } from '../data/apiHooks';
import messages from './messages';

Expand Down Expand Up @@ -40,11 +37,19 @@ export const PickLibraryContentModal: React.FC<PickLibraryContentModalProps> = (
}) => {
const intl = useIntl();

const { collectionId } = useParams();
const {
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,
} = useLibraryContext();

if (!collectionId || !ComponentPickerModal) {
throw new Error('libraryId and componentPickerModal are required');
}

const updateComponentsMutation = useAddComponentsToCollection(libraryId, collectionId);

const { showToast } = useContext(ToastContext);
Expand Down
2 changes: 1 addition & 1 deletion src/library-authoring/add-content/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// eslint-disable-next-line import/prefer-default-export, import/no-cycle
// eslint-disable-next-line import/prefer-default-export
export { default as AddContentContainer } from './AddContentContainer';
export { default as AddContentHeader } from './AddContentHeader';
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
import { useCollection, useContentLibrary } from '../data/apiHooks';
import { useLibraryContext } from '../common/context';
import messages from './messages';
// eslint-disable-next-line import/no-cycle
import { LibrarySidebar } from '../library-sidebar';
import LibraryCollectionComponents from './LibraryCollectionComponents';

Expand Down
20 changes: 20 additions & 0 deletions src/library-authoring/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useState,
} from 'react';

import type { ComponentPickerModal } from '../component-picker';
import type { ContentLibrary } from '../data/api';
import { useContentLibrary } from '../data/apiHooks';

Expand All @@ -24,6 +25,11 @@ type NoComponentPickerType = {
addComponentToSelectedComponents?: never;
removeComponentFromSelectedComponents?: never;
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;
};

type ComponentPickerSingleType = {
Expand All @@ -33,6 +39,7 @@ type ComponentPickerSingleType = {
addComponentToSelectedComponents?: never;
removeComponentFromSelectedComponents?: never;
restrictToLibrary: boolean;
componentPickerModal?: never;
};

type ComponentPickerMultipleType = {
Expand All @@ -42,6 +49,7 @@ type ComponentPickerMultipleType = {
addComponentToSelectedComponents: ComponentSelectedEvent;
removeComponentFromSelectedComponents: ComponentSelectedEvent;
restrictToLibrary: boolean;
componentPickerModal?: never;
};

type ComponentPickerType = NoComponentPickerType | ComponentPickerSingleType | ComponentPickerMultipleType;
Expand Down Expand Up @@ -113,20 +121,23 @@ type NoComponentPickerProps = {
onComponentSelected?: never;
onChangeComponentSelection?: never;
restrictToLibrary?: never;
componentPickerModal?: typeof ComponentPickerModal;
};

export type ComponentPickerSingleProps = {
componentPickerMode: 'single';
onComponentSelected: ComponentSelectedEvent;
onChangeComponentSelection?: never;
restrictToLibrary?: boolean;
componentPickerModal?: never;
};

export type ComponentPickerMultipleProps = {
componentPickerMode: 'multiple';
onComponentSelected?: never;
onChangeComponentSelection?: ComponentSelectionChangedEvent;
restrictToLibrary?: boolean;
componentPickerModal?: never;
};

type ComponentPickerProps = NoComponentPickerProps | ComponentPickerSingleProps | ComponentPickerMultipleProps;
Expand All @@ -139,6 +150,7 @@ type LibraryProviderProps = {
showOnlyPublished?: boolean;
/** Only used for testing */
initialSidebarComponentInfo?: SidebarComponentInfo;
componentPickerModal?: typeof ComponentPickerModal;
} & ComponentPickerProps;

/**
Expand All @@ -154,6 +166,7 @@ export const LibraryProvider = ({
onChangeComponentSelection,
showOnlyPublished = false,
initialSidebarComponentInfo,
componentPickerModal,
}: LibraryProviderProps) => {
const [collectionId, setCollectionId] = useState(collectionIdProp);
const [sidebarComponentInfo, setSidebarComponentInfo] = useState<SidebarComponentInfo | undefined>(
Expand Down Expand Up @@ -260,6 +273,12 @@ export const LibraryProvider = ({
closeComponentEditor,
resetSidebarAdditionalActions,
};
if (!componentPickerMode) {
return {
...contextValue,
componentPickerModal,
};
}
if (componentPickerMode === 'single') {
return {
...contextValue,
Expand Down Expand Up @@ -310,6 +329,7 @@ export const LibraryProvider = ({
openComponentEditor,
closeComponentEditor,
resetSidebarAdditionalActions,
componentPickerModal,
]);

return (
Expand Down
21 changes: 5 additions & 16 deletions src/library-authoring/component-picker/ComponentPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import React, { Suspense, useState } from 'react';
import React, { useState } from 'react';
import { useLocation } from 'react-router-dom';
import { Stepper } from '@openedx/paragon';

import Loading from '../../generic/Loading';
import {
type ComponentSelectedEvent,
type ComponentSelectionChangedEvent,
LibraryProvider,
useLibraryContext,
} from '../common/context';
import LibraryAuthoringPage from '../LibraryAuthoringPage';
import LibraryCollectionPage from '../collections/LibraryCollectionPage';
import SelectLibrary from './SelectLibrary';

// eslint-disable-next-line import/no-cycle
const LibraryAuthoringPage = React.lazy(() => import('../LibraryAuthoringPage'));
const LibraryCollectionPage = React.lazy(() => import('../collections/LibraryCollectionPage'));

interface LibraryComponentPickerProps {
returnToLibrarySelection: () => void;
}
Expand All @@ -23,17 +20,9 @@ const InnerComponentPicker: React.FC<LibraryComponentPickerProps> = ({ returnToL
const { collectionId } = useLibraryContext();

if (collectionId) {
return (
<Suspense fallback={<Loading />}>
<LibraryCollectionPage />;
</Suspense>
);
<LibraryCollectionPage />;
}
return (
<Suspense fallback={<Loading />}>
<LibraryAuthoringPage returnToLibrarySelection={returnToLibrarySelection} />;
</Suspense>
);
return <LibraryAuthoringPage returnToLibrarySelection={returnToLibrarySelection} />;
};

/** Default handler in single-select mode. Used by the legacy UI for adding a single selected component to a course. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { StandardModal } from '@openedx/paragon';

import type { ComponentSelectionChangedEvent } from '../common/context';
// eslint-disable-next-line import/no-cycle
import { ComponentPicker } from './ComponentPicker';

interface ComponentPickerModalProps {
Expand Down
1 change: 0 additions & 1 deletion src/library-authoring/component-picker/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// eslint-disable-next-line import/no-cycle
export { ComponentPicker } from './ComponentPicker';
export { ComponentPickerModal } from './ComponentPickerModal';
1 change: 0 additions & 1 deletion src/library-authoring/library-sidebar/LibrarySidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { Close } from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';

// eslint-disable-next-line import/no-cycle
import { AddContentContainer, AddContentHeader } from '../add-content';
import { CollectionInfo, CollectionInfoHeader } from '../collections';
import { SidebarBodyComponentId, useLibraryContext } from '../common/context';
Expand Down
2 changes: 1 addition & 1 deletion src/library-authoring/library-sidebar/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/prefer-default-export, import/no-cycle
// eslint-disable-next-line import/prefer-default-export
export { default as LibrarySidebar } from './LibrarySidebar';

0 comments on commit 3fe699f

Please sign in to comment.