Skip to content

Commit 7e3afc8

Browse files
Mamadukarichtaboryouknowriadjorgefilipecostaellatrix
committed
Site Editor: Fix template parts 'Reset' action (#62951)
Co-authored-by: Mamaduka <[email protected]> Co-authored-by: richtabor <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: jorgefilipecosta <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: t-hamano <[email protected]>
1 parent f5d0452 commit 7e3afc8

File tree

2 files changed

+35
-58
lines changed

2 files changed

+35
-58
lines changed

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

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ function isTemplateRemovable( template ) {
5959
! template.templatePart?.has_theme_file
6060
);
6161
}
62-
const canDeleteOrReset = ( item ) => {
63-
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
64-
const isUserPattern = item.type === PATTERN_TYPES.user;
65-
return isUserPattern || ( isTemplatePart && item.isCustom );
66-
};
6762

6863
function getItemTitle( item ) {
6964
if ( typeof item.title === 'string' ) {
@@ -885,21 +880,11 @@ const duplicatePostAction = {
885880
},
886881
};
887882

888-
const isTemplatePartRevertable = ( item ) => {
889-
if ( ! item ) {
890-
return false;
891-
}
892-
const hasThemeFile = item.templatePart?.has_theme_file;
893-
return canDeleteOrReset( item ) && hasThemeFile;
894-
};
895-
896883
const resetTemplateAction = {
897884
id: 'reset-template',
898885
label: __( 'Reset' ),
899886
isEligible: ( item ) => {
900-
return item.type === TEMPLATE_PART_POST_TYPE
901-
? isTemplatePartRevertable( item )
902-
: isTemplateRevertable( item );
887+
return isTemplateRevertable( item );
903888
},
904889
icon: backup,
905890
supportsBulk: true,
@@ -911,47 +896,39 @@ const resetTemplateAction = {
911896
onActionPerformed,
912897
} ) => {
913898
const [ isBusy, setIsBusy ] = useState( false );
914-
const { revertTemplate, removeTemplates } = unlock(
915-
useDispatch( editorStore )
916-
);
899+
const { revertTemplate } = unlock( useDispatch( editorStore ) );
917900
const { saveEditedEntityRecord } = useDispatch( coreStore );
918901
const { createSuccessNotice, createErrorNotice } =
919902
useDispatch( noticesStore );
920903
const onConfirm = async () => {
921904
try {
922-
if ( items[ 0 ].type === TEMPLATE_PART_POST_TYPE ) {
923-
await removeTemplates( items );
924-
} else {
925-
for ( const template of items ) {
926-
if ( template.type === TEMPLATE_POST_TYPE ) {
927-
await revertTemplate( template, {
928-
allowUndo: false,
929-
} );
930-
await saveEditedEntityRecord(
931-
'postType',
932-
template.type,
933-
template.id
934-
);
935-
}
936-
}
937-
createSuccessNotice(
938-
items.length > 1
939-
? sprintf(
940-
/* translators: The number of items. */
941-
__( '%s items reset.' ),
942-
items.length
943-
)
944-
: sprintf(
945-
/* translators: The template/part's name. */
946-
__( '"%s" reset.' ),
947-
decodeEntities( getItemTitle( items[ 0 ] ) )
948-
),
949-
{
950-
type: 'snackbar',
951-
id: 'revert-template-action',
952-
}
905+
for ( const template of items ) {
906+
await revertTemplate( template, {
907+
allowUndo: false,
908+
} );
909+
await saveEditedEntityRecord(
910+
'postType',
911+
template.type,
912+
template.id
953913
);
954914
}
915+
createSuccessNotice(
916+
items.length > 1
917+
? sprintf(
918+
/* translators: The number of items. */
919+
__( '%s items reset.' ),
920+
items.length
921+
)
922+
: sprintf(
923+
/* translators: The template/part's name. */
924+
__( '"%s" reset.' ),
925+
decodeEntities( getItemTitle( items[ 0 ] ) )
926+
),
927+
{
928+
type: 'snackbar',
929+
id: 'revert-template-action',
930+
}
931+
);
955932
} catch ( error ) {
956933
let fallbackErrorMessage;
957934
if ( items[ 0 ].type === TEMPLATE_POST_TYPE ) {

packages/editor/src/store/utils/is-template-revertable.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import { TEMPLATE_ORIGINS } from '../constants';
66
// Copy of the function from packages/edit-site/src/utils/is-template-revertable.js
77

88
/**
9-
* Check if a template is revertable to its original theme-provided template file.
9+
* Check if a template or template part is revertable to its original theme-provided file.
1010
*
11-
* @param {Object} template The template entity to check.
12-
* @return {boolean} Whether the template is revertable.
11+
* @param {Object} templateOrTemplatePart The entity to check.
12+
* @return {boolean} Whether the entity is revertable.
1313
*/
14-
export default function isTemplateRevertable( template ) {
15-
if ( ! template ) {
14+
export default function isTemplateRevertable( templateOrTemplatePart ) {
15+
if ( ! templateOrTemplatePart ) {
1616
return false;
1717
}
18-
/* eslint-disable camelcase */
18+
1919
return (
20-
template?.source === TEMPLATE_ORIGINS.custom && template?.has_theme_file
20+
templateOrTemplatePart.source === TEMPLATE_ORIGINS.custom &&
21+
templateOrTemplatePart.has_theme_file
2122
);
22-
/* eslint-enable camelcase */
2323
}

0 commit comments

Comments
 (0)