Skip to content

Commit

Permalink
Add: Delete bulk action to patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Feb 6, 2024
1 parent 2078ac4 commit f31be0f
Showing 1 changed file with 106 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,49 +161,125 @@ export const deleteAction = {
return canDeleteOrReset( item ) && ! hasThemeFile;
},
hideModalHeader: true,
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
supportsBulk: true,
RenderModal: ( { items, closeModal, onPerform } ) => {
const { __experimentalDeleteReusableBlock } =
useDispatch( reusableBlocksStore );
const { createErrorNotice, createSuccessNotice } =
useDispatch( noticesStore );
const { removeTemplate } = useDispatch( editSiteStore );
const { removeTemplates } = unlock( useDispatch( editSiteStore ) );

const deletePattern = async () => {
try {
await __experimentalDeleteReusableBlock( item.id );
createSuccessNotice(
sprintf(
// translators: %s: The pattern's title e.g. 'Call to action'.
const promiseResult = await Promise.allSettled(
items.map( ( item ) => {
return __experimentalDeleteReusableBlock( item.id );
} )
);
// 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" deleted.' ),
item.title
),
{ type: 'snackbar', id: 'edit-site-patterns-success' }
);
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while deleting the pattern.' );
createErrorNotice( errorMessage, {
items[ 0 ].title
);
} else {
successMessage = __( 'The patterns were deleted.' );
}
createSuccessNotice( successMessage, {
type: 'snackbar',
id: 'edit-site-patterns-error',
id: 'edit-site-page-trashed',
} );
} else {
// If there was at lease one failure.
let errorMessage;
// If we were trying to delete a single pattern.
if ( promiseResult.length === 1 ) {
if ( promiseResult[ 0 ].reason?.message ) {
errorMessage = promiseResult[ 0 ].reason.message;
} else {
errorMessage = __(
'An error occurred while deleting the pattern.'
);
}
// If we were trying to delete multiple patterns.
} else {
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 deleting the patterns.'
);
} else if ( errorMessages.size === 1 ) {
errorMessage = sprintf(
/* translators: %s: an error message */
__(
'An error occurred while deleting the patterns: %s'
),
[ ...errorMessages ][ 0 ]
);
} else {
errorMessage = sprintf(
/* translators: %s: a list of comma separated error messages */
__(
'Some errors occurred while deleting the patterns: %s'
),
[ ...errorMessages ].join( ',' )
);
}
createErrorNotice( errorMessage, {
type: 'snackbar',
} );
}
}
};
const deleteItem = () =>
item.type === TEMPLATE_PART_POST_TYPE
? removeTemplate( item )
: deletePattern();
const deleteItem = () => {
if ( items[ 0 ].type === TEMPLATE_PART_POST_TYPE ) {
removeTemplates( items );
} else {
deletePattern();
}
if ( onPerform ) {
onPerform();
}
closeModal();
};
let questionMessage;
if ( items.length === 1 ) {
questionMessage = sprintf(
// translators: %s: The page's title.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities( items[ 0 ].title || items[ 0 ].name )
);
} else if (
items.length > 1 &&
items[ 0 ].type === TEMPLATE_PART_POST_TYPE
) {
questionMessage = sprintf(
// translators: %d: The number of template parts (2 or more).
__( 'Are you sure you want to delete %d template parts?' ),
items.length
);
} else {
questionMessage = sprintf(
// translators: %d: The number of patterns (2 or more).
__( 'Are you sure you want to delete %d patterns?' ),
items.length
);
}
return (
<VStack spacing="5">
<Text>
{ sprintf(
// translators: %s: The pattern or template part's title e.g. 'Call to action'.
__( 'Are you sure you want to delete "%s"?' ),
decodeEntities( item.title || item.name )
) }
</Text>
<Text>{ questionMessage }</Text>
<HStack justify="right">
<Button variant="tertiary" onClick={ closeModal }>
{ __( 'Cancel' ) }
Expand Down

0 comments on commit f31be0f

Please sign in to comment.