Skip to content

Commit

Permalink
update how restore post action works
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 10, 2024
1 parent 08e0e47 commit 6a151ed
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,34 @@ export function useRestorePostAction() {
return status === 'trash';
},
async callback( posts, onActionPerformed ) {
try {
for ( const post of posts ) {
await editEntityRecord(
await Promise.allSettled(
posts.map( ( post ) => {
return editEntityRecord(
'postType',
post.type,
post.id,
{
status: 'draft',
}
);
await saveEditedEntityRecord(
} )
);
const promiseResult = await Promise.allSettled(
posts.map( ( post ) => {
return saveEditedEntityRecord(
'postType',
post.type,
post.id,
{ throwOnError: true }
);
}
} )
);

if (
promiseResult.every(
( { status } ) => status === 'fulfilled'
)
) {
createSuccessNotice(
posts.length > 1
? sprintf(
Expand All @@ -326,25 +336,56 @@ export function useRestorePostAction() {
if ( onActionPerformed ) {
onActionPerformed( posts );
}
} catch ( error ) {
} else {
// If there was at lease one failure.
let errorMessage;
if (
error.message &&
error.code !== 'unknown_error' &&
error.message
) {
errorMessage = error.message;
} else if ( posts.length > 1 ) {
errorMessage = __(
'An error occurred while restoring the posts.'
);
// If we were trying to move a single post to the trash.
if ( promiseResult.length === 1 ) {
if ( promiseResult[ 0 ].reason?.message ) {
errorMessage = promiseResult[ 0 ].reason.message;
} else {
errorMessage = __(
'An error occurred while restoring the post.'
);
}
// If we were trying to move multiple posts to the trash
} else {
errorMessage = __(
'An error occurred while restoring the post.'
const errorMessages = new Set();
const failedPromises = promiseResult.filter(
( { status } ) => status === 'rejected'
);
for ( const failedPromise of failedPromises ) {
if ( failedPromise.reason?.message ) {
errorMessages.add(
failedPromise.reason.message
);
}
}
if ( errorMessages.size === 0 ) {
errorMessage = __(
'An error occurred while restoring the posts.'
);
} else if ( errorMessages.size === 1 ) {
errorMessage = sprintf(
/* translators: %s: an error message */
__(
'An error occurred while restoring the posts: %s'
),
[ ...errorMessages ][ 0 ]
);
} else {
errorMessage = sprintf(
/* translators: %s: a list of comma separated error messages */
__(
'Some errors occurred while restoring the posts: %s'
),
[ ...errorMessages ].join( ',' )
);
}
}

createErrorNotice( errorMessage, { type: 'snackbar' } );
createErrorNotice( errorMessage, {
type: 'snackbar',
} );
}
},
} ),
Expand Down

0 comments on commit 6a151ed

Please sign in to comment.