Skip to content

Commit 3d1e5c3

Browse files
committed
Fix the 'Reset' action in dataviews
1 parent e9c625d commit 3d1e5c3

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

packages/editor/src/components/post-actions/actions.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import { store as editorStore } from '../../store';
3333
import { unlock } from '../../lock-unlock';
3434
import isTemplateRevertable from '../../store/utils/is-template-revertable';
35+
import isTemplatePartRevertable from '../../store/utils/is-template-part-revertable';
3536
import { exportPatternAsJSONAction } from './export-pattern-action';
3637
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
3738

@@ -787,22 +788,6 @@ const useDuplicatePostAction = ( postType ) => {
787788
);
788789
};
789790

790-
const isTemplatePartRevertable = ( item ) => {
791-
if ( ! item ) {
792-
return false;
793-
}
794-
// In patterns list page we map the templates parts to a different object
795-
// than the one returned from the endpoint. This is why we need to check for
796-
// two props whether is custom or has a theme file.
797-
const hasThemeFile =
798-
item.has_theme_file || item.templatePart?.has_theme_file;
799-
const isCustom = [ item.source, item.templatePart?.source ].includes(
800-
TEMPLATE_ORIGINS.custom
801-
);
802-
803-
return hasThemeFile && isCustom;
804-
};
805-
806791
const resetTemplateAction = {
807792
id: 'reset-template',
808793
label: __( 'Reset' ),

packages/editor/src/store/private-actions.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { decodeEntities } from '@wordpress/html-entities';
1515
* Internal dependencies
1616
*/
1717
import isTemplateRevertable from './utils/is-template-revertable';
18+
import isTemplatePartRevertable from './utils/is-template-part-revertable';
19+
import { TEMPLATE_PART_POST_TYPE } from './constants';
1820
export * from '../dataviews/store/private-actions';
1921

2022
/**
@@ -241,7 +243,12 @@ export const revertTemplate =
241243
async ( { registry } ) => {
242244
const noticeId = 'edit-site-template-reverted';
243245
registry.dispatch( noticesStore ).removeNotice( noticeId );
244-
if ( ! isTemplateRevertable( template ) ) {
246+
247+
const isRevertable =
248+
template.type === TEMPLATE_PART_POST_TYPE
249+
? isTemplatePartRevertable
250+
: isTemplateRevertable;
251+
if ( ! isRevertable( template ) ) {
245252
registry
246253
.dispatch( noticesStore )
247254
.createErrorNotice( __( 'This template is not revertable.' ), {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { TEMPLATE_ORIGINS } from '../constants';
5+
6+
/**
7+
* Check if a template part is revertable to its original theme-provided template file.
8+
*
9+
* @param {Object} templatePart The template part entity to check.
10+
* @return {boolean} Whether the template part is revertable.
11+
*/
12+
export default function isTemplatePartRevertable( templatePart ) {
13+
if ( ! templatePart ) {
14+
return false;
15+
}
16+
// In patterns list page we map the templates parts to a different object
17+
// than the one returned from the endpoint. This is why we need to check for
18+
// two props whether is custom or has a theme file.
19+
const hasThemeFile =
20+
templatePart.has_theme_file ||
21+
templatePart.templatePart?.has_theme_file;
22+
const isCustom = [
23+
templatePart.source,
24+
templatePart.templatePart?.source,
25+
].includes( TEMPLATE_ORIGINS.custom );
26+
27+
return hasThemeFile && isCustom;
28+
}

0 commit comments

Comments
 (0)