Skip to content

Commit

Permalink
Fix the 'Reset' action in dataviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 2, 2024
1 parent e9c625d commit 3d1e5c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
17 changes: 1 addition & 16 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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' ),
Expand Down
9 changes: 8 additions & 1 deletion packages/editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.' ), {
Expand Down
28 changes: 28 additions & 0 deletions packages/editor/src/store/utils/is-template-part-revertable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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;
}
// 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 =
templatePart.has_theme_file ||
templatePart.templatePart?.has_theme_file;
const isCustom = [
templatePart.source,
templatePart.templatePart?.source,
].includes( TEMPLATE_ORIGINS.custom );

return hasThemeFile && isCustom;
}

0 comments on commit 3d1e5c3

Please sign in to comment.