1- import React from 'react' ;
1+ import React , { useCallback , useContext } from 'react' ;
22import { FormattedMessage , useIntl } from '@edx/frontend-platform/i18n' ;
3- import {
4- ActionRow ,
5- AlertModal ,
6- Button ,
7- } from '@openedx/paragon' ;
83import { Warning } from '@openedx/paragon/icons' ;
94
105import { useSidebarContext } from '../common/context/SidebarContext' ;
11- import { useDeleteLibraryBlock , useLibraryBlockMetadata } from '../data/apiHooks' ;
6+ import { useDeleteLibraryBlock , useLibraryBlockMetadata , useRestoreLibraryBlock } from '../data/apiHooks' ;
127import messages from './messages' ;
8+ import { ToastContext } from '../../generic/toast-context' ;
9+ import DeleteModal from '../../generic/delete-modal/DeleteModal' ;
1310
1411/**
1512 * Helper component to load and display the name of the block.
@@ -35,11 +32,29 @@ interface Props {
3532const ComponentDeleter = ( { usageKey, ...props } : Props ) => {
3633 const intl = useIntl ( ) ;
3734 const { sidebarComponentInfo, closeLibrarySidebar } = useSidebarContext ( ) ;
35+ const { showToast } = useContext ( ToastContext ) ;
3836 const sidebarComponentUsageKey = sidebarComponentInfo ?. id ;
3937
38+ const restoreComponentMutation = useRestoreLibraryBlock ( ) ;
39+ const restoreComponent = useCallback ( async ( ) => {
40+ try {
41+ await restoreComponentMutation . mutateAsync ( { usageKey } ) ;
42+ showToast ( intl . formatMessage ( messages . undoDeleteComponentToastSuccess ) ) ;
43+ } catch ( e ) {
44+ showToast ( intl . formatMessage ( messages . undoDeleteComponentToastFailed ) ) ;
45+ }
46+ } , [ ] ) ;
47+
4048 const deleteComponentMutation = useDeleteLibraryBlock ( ) ;
41- const doDelete = React . useCallback ( ( ) => {
42- deleteComponentMutation . mutateAsync ( { usageKey } ) ;
49+ const doDelete = React . useCallback ( async ( ) => {
50+ await deleteComponentMutation . mutateAsync ( { usageKey } ) ;
51+ showToast (
52+ intl . formatMessage ( messages . deleteComponentSuccess ) ,
53+ {
54+ label : intl . formatMessage ( messages . undoDeleteCollectionToastAction ) ,
55+ onClick : restoreComponent ,
56+ } ,
57+ ) ;
4358 props . cancelDelete ( ) ;
4459 // Close the sidebar if it's still open showing the deleted component:
4560 if ( usageKey === sidebarComponentUsageKey ) {
@@ -52,20 +67,13 @@ const ComponentDeleter = ({ usageKey, ...props }: Props) => {
5267 }
5368
5469 return (
55- < AlertModal
56- title = { intl . formatMessage ( messages . deleteComponentWarningTitle ) }
70+ < DeleteModal
5771 isOpen
58- onClose = { props . cancelDelete }
72+ close = { props . cancelDelete }
5973 variant = "warning"
74+ title = { intl . formatMessage ( messages . deleteComponentWarningTitle ) }
6075 icon = { Warning }
61- footerNode = { (
62- < ActionRow >
63- < Button variant = "tertiary" onClick = { props . cancelDelete } > < FormattedMessage { ...messages . deleteComponentCancelButton } /> </ Button >
64- < Button variant = "danger" onClick = { doDelete } > < FormattedMessage { ...messages . deleteComponentButton } /> </ Button >
65- </ ActionRow >
66- ) }
67- >
68- < p >
76+ description = { (
6977 < FormattedMessage
7078 { ...messages . deleteComponentConfirm }
7179 values = { {
@@ -74,8 +82,9 @@ const ComponentDeleter = ({ usageKey, ...props }: Props) => {
7482 ) ,
7583 } }
7684 />
77- </ p >
78- </ AlertModal >
85+ ) }
86+ onDeleteSubmit = { doDelete }
87+ />
7988 ) ;
8089} ;
8190
0 commit comments