-
- { posts.length === 1
- ? sprintf(
- /* translators: The post's title. */
- __(
- 'Are you sure you want to permanently delete "%s"?'
- ),
- getItemTitle( posts[ 0 ] )
- )
- : __(
- 'Are you sure you want to permanently delete these items?'
- ) }
-
-
-
-
-
-
-
-);
-
const permanentlyDeletePost: Action< PostWithPermissions > = {
id: 'permanently-delete',
label: __( 'Permanently delete' ),
@@ -74,130 +34,153 @@ const permanentlyDeletePost: Action< PostWithPermissions > = {
const { status, permissions } = item;
return status === 'trash' && permissions?.delete;
},
- async callback( posts, { registry, onActionPerformed } ) {
+ hideModalHeader: true,
+ RenderModal: ( { items, closeModal, onActionPerformed } ) => {
+ const [ isBusy, setIsBusy ] = useState( false );
const { createSuccessNotice, createErrorNotice } =
- registry.dispatch( noticesStore );
- const { deleteEntityRecord } = registry.dispatch( coreStore );
+ useDispatch( noticesStore );
+ const { deleteEntityRecord } = useDispatch( coreStore );
- // Wrap logic in a Promise resolved by modal interaction
- return new Promise< void >( ( resolve ) => {
- // Setup modal container
- const container = document.createElement( 'div' );
- document.body.appendChild( container );
- const root = createRoot( container );
- const cleanup = () => {
- root.unmount();
- document.body.removeChild( container );
- };
-
- const handleConfirm = async () => {
- cleanup();
- const promiseResult = await Promise.allSettled(
- posts.map( ( post ) =>
- deleteEntityRecord(
- 'postType',
- post.type,
- post.id,
- { force: true },
- { throwOnError: true }
- )
- )
- );
-
- // If all the promises were fulfilled with success.
- if (
- promiseResult.every(
- ( { status } ) => status === 'fulfilled'
- )
- ) {
- let successMessage;
- if ( promiseResult.length === 1 ) {
- successMessage = sprintf(
- /* translators: The posts's title. */
- __( '"%s" permanently deleted.' ),
- getItemTitle( posts[ 0 ] )
- );
- } else {
- successMessage = __(
- 'The items were permanently deleted.'
- );
- }
- createSuccessNotice( successMessage, {
- type: 'snackbar',
- id: 'permanently-delete-post-action',
- } );
- onActionPerformed?.( posts );
- } else {
- // If there was at lease one failure.
- let errorMessage;
- // If we were trying to permanently delete a single post.
- if ( promiseResult.length === 1 ) {
- const typedError = promiseResult[ 0 ] as {
- reason?: CoreDataError;
- };
- if ( typedError.reason?.message ) {
- errorMessage = typedError.reason.message;
- } else {
- errorMessage = __(
- 'An error occurred while permanently deleting the item.'
- );
- }
- // If we were trying to permanently delete multiple posts
- } else {
- const errorMessages = new Set();
- const failedPromises = promiseResult.filter(
- ( { status } ) => status === 'rejected'
- );
- for ( const failedPromise of failedPromises ) {
- const typedError = failedPromise as {
- reason?: CoreDataError;
- };
- if ( typedError.reason?.message ) {
- errorMessages.add( typedError.reason.message );
- }
- }
- if ( errorMessages.size === 0 ) {
- errorMessage = __(
- 'An error occurred while permanently deleting the items.'
- );
- } else if ( errorMessages.size === 1 ) {
- errorMessage = sprintf(
- /* translators: %s: an error message */
- __(
- 'An error occurred while permanently deleting the items: %s'
+ return (
+