Skip to content

Commit 55e2c7f

Browse files
committed
Fix the 'Reset' action in dataviews
1 parent 3f7195f commit 55e2c7f

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
@@ -31,6 +31,7 @@ import {
3131
import { store as editorStore } from '../../store';
3232
import { unlock } from '../../lock-unlock';
3333
import isTemplateRevertable from '../../store/utils/is-template-revertable';
34+
import isTemplatePartRevertable from '../../store/utils/is-template-part-revertable';
3435
import { exportPatternAsJSONAction } from './export-pattern-action';
3536
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
3637

@@ -870,22 +871,6 @@ const useDuplicatePostAction = ( postType ) => {
870871
);
871872
};
872873

873-
const isTemplatePartRevertable = ( item ) => {
874-
if ( ! item ) {
875-
return false;
876-
}
877-
// In patterns list page we map the templates parts to a different object
878-
// than the one returned from the endpoint. This is why we need to check for
879-
// two props whether is custom or has a theme file.
880-
const hasThemeFile =
881-
item.has_theme_file || item.templatePart?.has_theme_file;
882-
const isCustom = [ item.source, item.templatePart?.source ].includes(
883-
TEMPLATE_ORIGINS.custom
884-
);
885-
886-
return hasThemeFile && isCustom;
887-
};
888-
889874
const resetTemplateAction = {
890875
id: 'reset-template',
891876
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)