1- import { FormattedMessage } from '@edx/frontend-platform/i18n' ;
1+ import { FormattedMessage , useIntl } from '@edx/frontend-platform/i18n' ;
22import {
33 ActionRow , Badge , Icon , IconButton , Stack , useToggle ,
44} from '@openedx/paragon' ;
55import { Description , DragIndicator } from '@openedx/paragon/icons' ;
66import { useQueryClient } from '@tanstack/react-query' ;
77import classNames from 'classnames' ;
8- import { useEffect , useState } from 'react' ;
8+ import { useContext , useEffect , useState } from 'react' ;
99import { ContentTagsDrawerSheet } from '../../content-tags-drawer' ;
1010import { blockTypes } from '../../editors/data/constants/app' ;
1111import DraggableList , { SortableItem } from '../../generic/DraggableList' ;
@@ -18,10 +18,11 @@ import TagCount from '../../generic/tag-count';
1818import { useLibraryContext } from '../common/context/LibraryContext' ;
1919import ComponentMenu from '../components' ;
2020import { LibraryBlockMetadata } from '../data/api' ;
21- import { libraryAuthoringQueryKeys , useContainerChildren } from '../data/apiHooks' ;
21+ import { libraryAuthoringQueryKeys , useContainerChildren , useUpdateContainerChildren } from '../data/apiHooks' ;
2222import { LibraryBlock } from '../LibraryBlock' ;
2323import { useLibraryRoutes } from '../routes' ;
2424import messages from './messages' ;
25+ import { ToastContext } from '../../generic/toast-context' ;
2526
2627interface LibraryUnitBlocksProps {
2728 /** set to true if it is rendered as preview
@@ -31,10 +32,12 @@ interface LibraryUnitBlocksProps {
3132}
3233
3334export const LibraryUnitBlocks = ( { preview } : LibraryUnitBlocksProps ) => {
35+ const intl = useIntl ( ) ;
3436 const [ orderedBlocks , setOrderedBlocks ] = useState < LibraryBlockMetadata [ ] > ( [ ] ) ;
3537 const [ isManageTagsDrawerOpen , openManageTagsDrawer , closeManageTagsDrawer ] = useToggle ( false ) ;
3638 const [ hidePreviewFor , setHidePreviewFor ] = useState < string | null > ( null ) ;
3739 const { navigateTo } = useLibraryRoutes ( ) ;
40+ const { showToast } = useContext ( ToastContext ) ;
3841
3942 const {
4043 libraryId,
@@ -45,6 +48,7 @@ export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
4548 } = useLibraryContext ( ) ;
4649
4750 const queryClient = useQueryClient ( ) ;
51+ const orderMutator = useUpdateContainerChildren ( unitId ) ;
4852 const {
4953 data : blocks ,
5054 isLoading,
@@ -63,11 +67,14 @@ export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
6367 return < ErrorAlert error = { error } /> ;
6468 }
6569
66- /* istanbul ignore next */
67- const handleReorder = ( ) => ( newOrder : LibraryBlockMetadata [ ] ) => {
68- // eslint-disable-next-line no-console
69- console . log ( 'LibraryUnitBlocks newOrder: ' , newOrder ) ;
70- // TODO: update order of components in unit
70+ const handleReorder = ( ) => async ( newOrder : LibraryBlockMetadata [ ] ) => {
71+ const usageKeys = newOrder . map ( ( o ) => o . id ) ;
72+ try {
73+ await orderMutator . mutateAsync ( usageKeys ) ;
74+ showToast ( intl . formatMessage ( messages . orderUpdatedMsg ) ) ;
75+ } catch ( e ) {
76+ showToast ( intl . formatMessage ( messages . failedOrderUpdatedMsg ) ) ;
77+ }
7178 } ;
7279
7380 const onTagSidebarClose = ( ) => {
0 commit comments