Skip to content

Commit 2801b8a

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 590f24c commit 2801b8a

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' ) {
@@ -879,21 +874,11 @@ const duplicatePostAction = {
879874
},
880875
};
881876

882-
const isTemplatePartRevertable = ( item ) => {
883-
if ( ! item ) {
884-
return false;
885-
}
886-
const hasThemeFile = item.templatePart?.has_theme_file;
887-
return canDeleteOrReset( item ) && hasThemeFile;
888-
};
889-
890877
const resetTemplateAction = {
891878
id: 'reset-template',
892879
label: __( 'Reset' ),
893880
isEligible: ( item ) => {
894-
return item.type === TEMPLATE_PART_POST_TYPE
895-
? isTemplatePartRevertable( item )
896-
: isTemplateRevertable( item );
881+
return isTemplateRevertable( item );
897882
},
898883
icon: backup,
899884
supportsBulk: true,
@@ -905,47 +890,39 @@ const resetTemplateAction = {
905890
onActionPerformed,
906891
} ) => {
907892
const [ isBusy, setIsBusy ] = useState( false );
908-
const { revertTemplate, removeTemplates } = unlock(
909-
useDispatch( editorStore )
910-
);
893+
const { revertTemplate } = unlock( useDispatch( editorStore ) );
911894
const { saveEditedEntityRecord } = useDispatch( coreStore );
912895
const { createSuccessNotice, createErrorNotice } =
913896
useDispatch( noticesStore );
914897
const onConfirm = async () => {
915898
try {
916-
if ( items[ 0 ].type === TEMPLATE_PART_POST_TYPE ) {
917-
await removeTemplates( items );
918-
} else {
919-
for ( const template of items ) {
920-
if ( template.type === TEMPLATE_POST_TYPE ) {
921-
await revertTemplate( template, {
922-
allowUndo: false,
923-
} );
924-
await saveEditedEntityRecord(
925-
'postType',
926-
template.type,
927-
template.id
928-
);
929-
}
930-
}
931-
createSuccessNotice(
932-
items.length > 1
933-
? sprintf(
934-
/* translators: The number of items. */
935-
__( '%s items reset.' ),
936-
items.length
937-
)
938-
: sprintf(
939-
/* translators: The template/part's name. */
940-
__( '"%s" reset.' ),
941-
decodeEntities( getItemTitle( items[ 0 ] ) )
942-
),
943-
{
944-
type: 'snackbar',
945-
id: 'revert-template-action',
946-
}
899+
for ( const template of items ) {
900+
await revertTemplate( template, {
901+
allowUndo: false,
902+
} );
903+
await saveEditedEntityRecord(
904+
'postType',
905+
template.type,
906+
template.id
947907
);
948908
}
909+
createSuccessNotice(
910+
items.length > 1
911+
? sprintf(
912+
/* translators: The number of items. */
913+
__( '%s items reset.' ),
914+
items.length
915+
)
916+
: sprintf(
917+
/* translators: The template/part's name. */
918+
__( '"%s" reset.' ),
919+
decodeEntities( getItemTitle( items[ 0 ] ) )
920+
),
921+
{
922+
type: 'snackbar',
923+
id: 'revert-template-action',
924+
}
925+
);
949926
} catch ( error ) {
950927
let fallbackErrorMessage;
951928
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)