From ae3839d25a148d664442e4a15bd0b03593dedb25 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 14 Jun 2024 16:10:08 +0100 Subject: [PATCH] Fix: User capacities are not taken into account in the trash post action. --- .../src/components/post-actions/actions.js | 371 ++++++++++-------- 1 file changed, 207 insertions(+), 164 deletions(-) diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 6dcc7f6cb66708..401917f375c249 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -3,7 +3,7 @@ */ import { external, trash, backup } from '@wordpress/icons'; import { addQueryArgs } from '@wordpress/url'; -import { useDispatch, useSelect } from '@wordpress/data'; +import { useDispatch, useSelect, useRegistry } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { store as coreStore } from '@wordpress/core-data'; import { __, _n, sprintf, _x } from '@wordpress/i18n'; @@ -155,174 +155,215 @@ const deletePostAction = { }, }; -const trashPostAction = { - id: 'move-to-trash', - label: __( 'Move to Trash' ), - isPrimary: true, - icon: trash, - isEligible( item ) { - return ! [ 'auto-draft', 'trash' ].includes( item.status ); - }, - supportsBulk: true, - hideModalHeader: true, - RenderModal: ( { - items, - closeModal, - onActionStart, - onActionPerformed, - } ) => { - const [ isBusy, setIsBusy ] = useState( false ); - const { createSuccessNotice, createErrorNotice } = - useDispatch( noticesStore ); - const { deleteEntityRecord } = useDispatch( coreStore ); - return ( - - - { items.length === 1 - ? sprintf( - // translators: %s: The item's title. - __( - 'Are you sure you want to move to trash "%s"?' - ), - getItemTitle( items[ 0 ] ) - ) - : sprintf( - // translators: %d: The number of items (2 or more). - _n( - 'Are you sure you want to move to trash %d item?', - 'Are you sure you want to move to trash %d items?', - items.length - ), - items.length - ) } - - - - + - - - ); - }, -}; - + if ( onActionPerformed ) { + onActionPerformed( items ); + } + setIsBusy( false ); + closeModal(); + } } + isBusy={ isBusy } + disabled={ isBusy } + __experimentalIsFocusable + > + { __( 'Trash' ) } + + + + ); + }, + } ), + // eslint-disable-next-line react-hooks/exhaustive-deps + [ registry, resource, canUserResolvers ] + ); +} function usePermanentlyDeletePostAction() { const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); @@ -725,8 +766,8 @@ const renamePostAction = { const duplicatePostAction = { id: 'duplicate-post', label: _x( 'Duplicate', 'action label' ), - isEligible( { status } ) { - return status !== 'trash'; + isEligible( item ) { + return item.status !== 'trash'; }, RenderModal: ( { items, closeModal, onActionPerformed } ) => { const [ item ] = items; @@ -1044,6 +1085,7 @@ export function usePostActions( postType, onActionPerformed ) { const permanentlyDeletePostAction = usePermanentlyDeletePostAction(); const restorePostAction = useRestorePostAction(); + const trashPostAction = useTrashPostAction( postType ); const isTemplateOrTemplatePart = [ TEMPLATE_POST_TYPE, TEMPLATE_PART_POST_TYPE, @@ -1125,6 +1167,7 @@ export function usePostActions( postType, onActionPerformed ) { isPattern, postTypeObject?.viewable, permanentlyDeletePostAction, + trashPostAction, restorePostAction, onActionPerformed, isLoaded,