From c9d2d64eaee995ffced39b025986b3b9148bb48a Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 28 Jun 2024 09:30:22 +0400 Subject: [PATCH 1/4] Site Editor: Fix template parts 'Reset' action --- .../src/components/post-actions/actions.js | 80 +++++++++---------- 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 60fc718279afb..1ebe08c191bdf 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -57,14 +57,6 @@ function isTemplateRemovable( template ) { ! template?.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.source === TEMPLATE_ORIGINS.custom ) - ); -}; function getItemTitle( item ) { if ( typeof item.title === 'string' ) { @@ -799,8 +791,16 @@ const isTemplatePartRevertable = ( item ) => { if ( ! item ) { return false; } - const hasThemeFile = item?.has_theme_file; - return canDeleteOrReset( item ) && hasThemeFile; + // In patterns list page we map the templates parts to a different object + // than the one returned from the endpoint. This is why we need to check for + // two props whether is custom or has a theme file. + const hasThemeFile = + item.has_theme_file || item.templatePart?.has_theme_file; + const isCustom = [ item.source, item.templatePart?.source ].includes( + TEMPLATE_ORIGINS.custom + ); + + return hasThemeFile && isCustom; }; const resetTemplateAction = { @@ -816,47 +816,39 @@ const resetTemplateAction = { hideModalHeader: true, RenderModal: ( { items, closeModal, 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 ) { From 2018be8ce9861aa00de9c335469ea88afa74b5a9 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 28 Jun 2024 09:48:16 +0400 Subject: [PATCH 2/4] Fix the 'Reset' action in dataviews --- .../src/components/post-actions/actions.js | 17 +-------------- packages/editor/src/store/private-actions.js | 9 +++++++- .../utils/is-template-part-revertable.js | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 packages/editor/src/store/utils/is-template-part-revertable.js diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 1ebe08c191bdf..638b6039193c8 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -32,6 +32,7 @@ import { import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; import isTemplateRevertable from '../../store/utils/is-template-revertable'; +import isTemplatePartRevertable from '../../store/utils/is-template-part-revertable'; import { exportPatternAsJSONAction } from './export-pattern-action'; import { CreateTemplatePartModalContents } from '../create-template-part-modal'; @@ -787,22 +788,6 @@ const useDuplicatePostAction = ( postType ) => { ); }; -const isTemplatePartRevertable = ( item ) => { - if ( ! item ) { - return false; - } - // In patterns list page we map the templates parts to a different object - // than the one returned from the endpoint. This is why we need to check for - // two props whether is custom or has a theme file. - const hasThemeFile = - item.has_theme_file || item.templatePart?.has_theme_file; - const isCustom = [ item.source, item.templatePart?.source ].includes( - TEMPLATE_ORIGINS.custom - ); - - return hasThemeFile && isCustom; -}; - const resetTemplateAction = { id: 'reset-template', label: __( 'Reset' ), diff --git a/packages/editor/src/store/private-actions.js b/packages/editor/src/store/private-actions.js index c5fe1c260071a..dc308a6329982 100644 --- a/packages/editor/src/store/private-actions.js +++ b/packages/editor/src/store/private-actions.js @@ -15,6 +15,8 @@ import { decodeEntities } from '@wordpress/html-entities'; * Internal dependencies */ import isTemplateRevertable from './utils/is-template-revertable'; +import isTemplatePartRevertable from './utils/is-template-part-revertable'; +import { TEMPLATE_PART_POST_TYPE } from './constants'; export * from '../dataviews/store/private-actions'; /** @@ -241,7 +243,12 @@ export const revertTemplate = async ( { registry } ) => { const noticeId = 'edit-site-template-reverted'; registry.dispatch( noticesStore ).removeNotice( noticeId ); - if ( ! isTemplateRevertable( template ) ) { + + const isRevertable = + template.type === TEMPLATE_PART_POST_TYPE + ? isTemplatePartRevertable + : isTemplateRevertable; + if ( ! isRevertable( template ) ) { registry .dispatch( noticesStore ) .createErrorNotice( __( 'This template is not revertable.' ), { diff --git a/packages/editor/src/store/utils/is-template-part-revertable.js b/packages/editor/src/store/utils/is-template-part-revertable.js new file mode 100644 index 0000000000000..16d5ab7c3e6b7 --- /dev/null +++ b/packages/editor/src/store/utils/is-template-part-revertable.js @@ -0,0 +1,21 @@ +/** + * Internal dependencies + */ +import { TEMPLATE_ORIGINS } from '../constants'; + +/** + * Check if a template part is revertable to its original theme-provided template file. + * + * @param {Object} templatePart The template part entity to check. + * @return {boolean} Whether the template part is revertable. + */ +export default function isTemplatePartRevertable( templatePart ) { + if ( ! templatePart ) { + return false; + } + + return ( + templatePart.has_theme_file && + templatePart.source === TEMPLATE_ORIGINS.custom + ); +} From 1a2980082942812f6060d48662dca079c33c5d7f Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 3 Jul 2024 12:25:07 +0400 Subject: [PATCH 3/4] Reuse the check --- .../src/components/post-actions/actions.js | 5 +---- packages/editor/src/store/private-actions.js | 8 +------ .../utils/is-template-part-revertable.js | 21 ------------------- .../src/store/utils/is-template-revertable.js | 16 +++++++------- 4 files changed, 10 insertions(+), 40 deletions(-) delete mode 100644 packages/editor/src/store/utils/is-template-part-revertable.js diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index 638b6039193c8..535e474aafff1 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -32,7 +32,6 @@ import { import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; import isTemplateRevertable from '../../store/utils/is-template-revertable'; -import isTemplatePartRevertable from '../../store/utils/is-template-part-revertable'; import { exportPatternAsJSONAction } from './export-pattern-action'; import { CreateTemplatePartModalContents } from '../create-template-part-modal'; @@ -792,9 +791,7 @@ 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, diff --git a/packages/editor/src/store/private-actions.js b/packages/editor/src/store/private-actions.js index dc308a6329982..19b30c9dd4541 100644 --- a/packages/editor/src/store/private-actions.js +++ b/packages/editor/src/store/private-actions.js @@ -15,8 +15,6 @@ import { decodeEntities } from '@wordpress/html-entities'; * Internal dependencies */ import isTemplateRevertable from './utils/is-template-revertable'; -import isTemplatePartRevertable from './utils/is-template-part-revertable'; -import { TEMPLATE_PART_POST_TYPE } from './constants'; export * from '../dataviews/store/private-actions'; /** @@ -244,11 +242,7 @@ export const revertTemplate = const noticeId = 'edit-site-template-reverted'; registry.dispatch( noticesStore ).removeNotice( noticeId ); - const isRevertable = - template.type === TEMPLATE_PART_POST_TYPE - ? isTemplatePartRevertable - : isTemplateRevertable; - if ( ! isRevertable( template ) ) { + if ( ! isTemplateRevertable( template ) ) { registry .dispatch( noticesStore ) .createErrorNotice( __( 'This template is not revertable.' ), { diff --git a/packages/editor/src/store/utils/is-template-part-revertable.js b/packages/editor/src/store/utils/is-template-part-revertable.js deleted file mode 100644 index 16d5ab7c3e6b7..0000000000000 --- a/packages/editor/src/store/utils/is-template-part-revertable.js +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Internal dependencies - */ -import { TEMPLATE_ORIGINS } from '../constants'; - -/** - * Check if a template part is revertable to its original theme-provided template file. - * - * @param {Object} templatePart The template part entity to check. - * @return {boolean} Whether the template part is revertable. - */ -export default function isTemplatePartRevertable( templatePart ) { - if ( ! templatePart ) { - return false; - } - - return ( - templatePart.has_theme_file && - templatePart.source === TEMPLATE_ORIGINS.custom - ); -} diff --git a/packages/editor/src/store/utils/is-template-revertable.js b/packages/editor/src/store/utils/is-template-revertable.js index efe4647f21280..a09715af875bc 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 */ } From 413cba368a6fdc695633a173791dccdc4583dc2d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 3 Jul 2024 14:00:37 +0400 Subject: [PATCH 4/4] Cleanup --- packages/editor/src/store/private-actions.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/editor/src/store/private-actions.js b/packages/editor/src/store/private-actions.js index 19b30c9dd4541..c5fe1c260071a 100644 --- a/packages/editor/src/store/private-actions.js +++ b/packages/editor/src/store/private-actions.js @@ -241,7 +241,6 @@ export const revertTemplate = async ( { registry } ) => { const noticeId = 'edit-site-template-reverted'; registry.dispatch( noticesStore ).removeNotice( noticeId ); - if ( ! isTemplateRevertable( template ) ) { registry .dispatch( noticesStore )