From e01188c3ebdcb5615109e9f481c6ea98ccd0fa91 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 5 Jul 2024 12:36:03 +0400 Subject: [PATCH] Site Editor: Fix template parts 'Reset' action (#62951) (#63084) Co-authored-by: richtabor Co-authored-by: youknowriad Co-authored-by: jorgefilipecosta Co-authored-by: ellatrix Co-authored-by: t-hamano --- .../src/components/post-actions/actions.js | 77 +++++++------------ .../src/store/utils/is-template-revertable.js | 16 ++-- 2 files changed, 35 insertions(+), 58 deletions(-) diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 6c4c005c025411..5286b5e65a8204 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -59,11 +59,6 @@ function isTemplateRemovable( template ) { ! template.templatePart?.has_theme_file ); } -const canDeleteOrReset = ( item ) => { - const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE; - const isUserPattern = item.type === PATTERN_TYPES.user; - return isUserPattern || ( isTemplatePart && item.isCustom ); -}; function getItemTitle( item ) { if ( typeof item.title === 'string' ) { @@ -879,21 +874,11 @@ const duplicatePostAction = { }, }; -const isTemplatePartRevertable = ( item ) => { - if ( ! item ) { - return false; - } - const hasThemeFile = item.templatePart?.has_theme_file; - return canDeleteOrReset( item ) && hasThemeFile; -}; - const resetTemplateAction = { id: 'reset-template', label: __( 'Reset' ), isEligible: ( item ) => { - return item.type === TEMPLATE_PART_POST_TYPE - ? isTemplatePartRevertable( item ) - : isTemplateRevertable( item ); + return isTemplateRevertable( item ); }, icon: backup, supportsBulk: true, @@ -905,47 +890,39 @@ const resetTemplateAction = { onActionPerformed, } ) => { const [ isBusy, setIsBusy ] = useState( false ); - const { revertTemplate, removeTemplates } = unlock( - useDispatch( editorStore ) - ); + const { revertTemplate } = unlock( useDispatch( editorStore ) ); const { saveEditedEntityRecord } = useDispatch( coreStore ); const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); const onConfirm = async () => { try { - if ( items[ 0 ].type === TEMPLATE_PART_POST_TYPE ) { - await removeTemplates( items ); - } else { - for ( const template of items ) { - if ( template.type === TEMPLATE_POST_TYPE ) { - await revertTemplate( template, { - allowUndo: false, - } ); - await saveEditedEntityRecord( - 'postType', - template.type, - template.id - ); - } - } - createSuccessNotice( - items.length > 1 - ? sprintf( - /* translators: The number of items. */ - __( '%s items reset.' ), - items.length - ) - : sprintf( - /* translators: The template/part's name. */ - __( '"%s" reset.' ), - decodeEntities( getItemTitle( items[ 0 ] ) ) - ), - { - type: 'snackbar', - id: 'revert-template-action', - } + for ( const template of items ) { + await revertTemplate( template, { + allowUndo: false, + } ); + await saveEditedEntityRecord( + 'postType', + template.type, + template.id ); } + createSuccessNotice( + items.length > 1 + ? sprintf( + /* translators: The number of items. */ + __( '%s items reset.' ), + items.length + ) + : sprintf( + /* translators: The template/part's name. */ + __( '"%s" reset.' ), + decodeEntities( getItemTitle( items[ 0 ] ) ) + ), + { + type: 'snackbar', + id: 'revert-template-action', + } + ); } catch ( error ) { let fallbackErrorMessage; if ( items[ 0 ].type === TEMPLATE_POST_TYPE ) { diff --git a/packages/editor/src/store/utils/is-template-revertable.js b/packages/editor/src/store/utils/is-template-revertable.js index efe4647f212801..a09715af875bc2 100644 --- a/packages/editor/src/store/utils/is-template-revertable.js +++ b/packages/editor/src/store/utils/is-template-revertable.js @@ -6,18 +6,18 @@ import { TEMPLATE_ORIGINS } from '../constants'; // Copy of the function from packages/edit-site/src/utils/is-template-revertable.js /** - * Check if a template is revertable to its original theme-provided template file. + * Check if a template or template part is revertable to its original theme-provided file. * - * @param {Object} template The template entity to check. - * @return {boolean} Whether the template is revertable. + * @param {Object} templateOrTemplatePart The entity to check. + * @return {boolean} Whether the entity is revertable. */ -export default function isTemplateRevertable( template ) { - if ( ! template ) { +export default function isTemplateRevertable( templateOrTemplatePart ) { + if ( ! templateOrTemplatePart ) { return false; } - /* eslint-disable camelcase */ + return ( - template?.source === TEMPLATE_ORIGINS.custom && template?.has_theme_file + templateOrTemplatePart.source === TEMPLATE_ORIGINS.custom && + templateOrTemplatePart.has_theme_file ); - /* eslint-enable camelcase */ }