-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add existing content to a collection
- Loading branch information
Showing
10 changed files
with
169 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/library-authoring/add-content/PickLibraryContentModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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'; | ||
|
||
interface PickLibraryContentModalFooterProps { | ||
onSubmit: () => void; | ||
selectedComponents: SelectedComponent[]; | ||
} | ||
|
||
const PickLibraryContentModalFooter: React.FC<PickLibraryContentModalFooterProps> = ({ | ||
onSubmit, | ||
selectedComponents, | ||
}) => ( | ||
<ActionRow> | ||
<FormattedMessage {...messages.selectedComponents} values={{ count: selectedComponents.length }} /> | ||
<ActionRow.Spacer /> | ||
<Button variant="primary" onClick={onSubmit}> | ||
<FormattedMessage {...messages.addToCollectionButton} /> | ||
</Button> | ||
</ActionRow> | ||
); | ||
|
||
interface PickLibraryContentModalProps { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const PickLibraryContentModal: React.FC<PickLibraryContentModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
}) => { | ||
const intl = useIntl(); | ||
|
||
const { collectionId } = useParams(); | ||
const { | ||
libraryId, | ||
} = useLibraryContext(); | ||
|
||
const updateComponentsMutation = useAddComponentsToCollection(libraryId, collectionId); | ||
|
||
const { showToast } = useContext(ToastContext); | ||
|
||
const [selectedComponents, setSelectedComponents] = useState<SelectedComponent[]>([]); | ||
|
||
const onSubmit = useCallback(() => { | ||
const usageKeys = selectedComponents.map(({ usageKey }) => usageKey); | ||
updateComponentsMutation.mutateAsync(usageKeys) | ||
.then(() => { | ||
showToast(intl.formatMessage(messages.successAssociateComponentMessage)); | ||
onClose(); | ||
}) | ||
.catch(() => { | ||
showToast(intl.formatMessage(messages.errorAssociateComponentMessage)); | ||
}); | ||
}, [selectedComponents]); | ||
|
||
return ( | ||
<ComponentPickerModal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
onChangeComponentSelection={setSelectedComponents} | ||
footerNode={<PickLibraryContentModalFooter onSubmit={onSubmit} selectedComponents={selectedComponents} />} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/library-authoring/component-picker/ComponentPickerModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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 { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
onChangeComponentSelection: ComponentSelectionChangedEvent; | ||
footerNode?: React.ReactNode; | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const ComponentPickerModal: React.FC<ComponentPickerModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
onChangeComponentSelection, | ||
footerNode, | ||
}) => { | ||
if (!isOpen) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<StandardModal | ||
title="Select components" | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
isOverflowVisible={false} | ||
size="xl" | ||
footerNode={footerNode} | ||
> | ||
<ComponentPicker | ||
componentPickerMode="multiple" | ||
onChangeComponentSelection={onChangeComponentSelection} | ||
/> | ||
</StandardModal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
// eslint-disable-next-line import/no-cycle | ||
export { ComponentPicker } from './ComponentPicker'; | ||
export { ComponentPickerModal } from './ComponentPickerModal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// eslint-disable-next-line import/prefer-default-export | ||
// eslint-disable-next-line import/prefer-default-export, import/no-cycle | ||
export { default as LibrarySidebar } from './LibrarySidebar'; |