@@ -5,23 +5,25 @@ import { ActionRow, Button, StandardModal } from '@openedx/paragon';
55import { ToastContext } from '../../generic/toast-context' ;
66import { useLibraryContext } from '../common/context/LibraryContext' ;
77import type { SelectedComponent } from '../common/context/ComponentPickerContext' ;
8- import { useAddComponentsToCollection } from '../data/apiHooks' ;
8+ import { useAddComponentsToCollection , useAddComponentsToContainer } from '../data/apiHooks' ;
99import messages from './messages' ;
1010
1111interface PickLibraryContentModalFooterProps {
1212 onSubmit : ( ) => void ;
1313 selectedComponents : SelectedComponent [ ] ;
14+ buttonText : React . ReactNode ;
1415}
1516
1617const PickLibraryContentModalFooter : React . FC < PickLibraryContentModalFooterProps > = ( {
1718 onSubmit,
1819 selectedComponents,
20+ buttonText,
1921} ) => (
2022 < ActionRow >
2123 < FormattedMessage { ...messages . selectedComponents } values = { { count : selectedComponents . length } } />
2224 < ActionRow . Spacer />
2325 < Button variant = "primary" onClick = { onSubmit } >
24- < FormattedMessage { ... messages . addToCollectionButton } />
26+ { buttonText }
2527 </ Button >
2628 </ ActionRow >
2729) ;
@@ -40,18 +42,20 @@ export const PickLibraryContentModal: React.FC<PickLibraryContentModalProps> = (
4042 const {
4143 libraryId,
4244 collectionId,
45+ unitId,
4346 /** We need to get it as a reference instead of directly importing it to avoid the import cycle:
4447 * ComponentPicker > LibraryAuthoringPage/LibraryCollectionPage >
4548 * Sidebar > AddContent > ComponentPicker */
4649 componentPicker : ComponentPicker ,
4750 } = useLibraryContext ( ) ;
4851
4952 // istanbul ignore if: this should never happen
50- if ( ! collectionId || ! ComponentPicker ) {
51- throw new Error ( 'libraryId and componentPicker are required' ) ;
53+ if ( ! ( collectionId || unitId ) || ! ComponentPicker ) {
54+ throw new Error ( 'collectionId/unitId and componentPicker are required' ) ;
5255 }
5356
54- const updateComponentsMutation = useAddComponentsToCollection ( libraryId , collectionId ) ;
57+ const updateCollectionItemsMutation = useAddComponentsToCollection ( libraryId , collectionId ) ;
58+ const updateUnitComponentsMutation = useAddComponentsToContainer ( unitId ) ;
5559
5660 const { showToast } = useContext ( ToastContext ) ;
5761
@@ -60,13 +64,23 @@ export const PickLibraryContentModal: React.FC<PickLibraryContentModalProps> = (
6064 const onSubmit = useCallback ( ( ) => {
6165 const usageKeys = selectedComponents . map ( ( { usageKey } ) => usageKey ) ;
6266 onClose ( ) ;
63- updateComponentsMutation . mutateAsync ( usageKeys )
64- . then ( ( ) => {
65- showToast ( intl . formatMessage ( messages . successAssociateComponentMessage ) ) ;
66- } )
67- . catch ( ( ) => {
68- showToast ( intl . formatMessage ( messages . errorAssociateComponentToCollectionMessage ) ) ;
69- } ) ;
67+ if ( collectionId ) {
68+ updateCollectionItemsMutation . mutateAsync ( usageKeys )
69+ . then ( ( ) => {
70+ showToast ( intl . formatMessage ( messages . successAssociateComponentMessage ) ) ;
71+ } )
72+ . catch ( ( ) => {
73+ showToast ( intl . formatMessage ( messages . errorAssociateComponentToCollectionMessage ) ) ;
74+ } ) ;
75+ } else if ( unitId ) {
76+ updateUnitComponentsMutation . mutateAsync ( usageKeys )
77+ . then ( ( ) => {
78+ showToast ( intl . formatMessage ( messages . successAssociateComponentMessage ) ) ;
79+ } )
80+ . catch ( ( ) => {
81+ showToast ( intl . formatMessage ( messages . errorAssociateComponentToContainerMessage ) ) ;
82+ } ) ;
83+ }
7084 } , [ selectedComponents ] ) ;
7185
7286 return (
@@ -76,7 +90,16 @@ export const PickLibraryContentModal: React.FC<PickLibraryContentModalProps> = (
7690 size = "xl"
7791 isOpen = { isOpen }
7892 onClose = { onClose }
79- footerNode = { < PickLibraryContentModalFooter onSubmit = { onSubmit } selectedComponents = { selectedComponents } /> }
93+ footerNode = { (
94+ < PickLibraryContentModalFooter
95+ onSubmit = { onSubmit }
96+ selectedComponents = { selectedComponents }
97+ buttonText = { ( collectionId
98+ ? intl . formatMessage ( messages . addToCollectionButton )
99+ : intl . formatMessage ( messages . addToUnitButton )
100+ ) }
101+ />
102+ ) }
80103 >
81104 < ComponentPicker
82105 libraryId = { libraryId }
0 commit comments