Skip to content

Commit e9e7ede

Browse files
committed
Save entity after reverting it
1 parent 3e65513 commit e9e7ede

2 files changed

Lines changed: 43 additions & 26 deletions

File tree

packages/edit-site/src/components/list/table.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import isTemplateRevertable from '../../utils/is-template-revertable';
2222

2323
function Actions( { template } ) {
2424
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore );
25+
const { saveEditedEntityRecord } = useDispatch( coreStore );
2526

2627
const isRemovable = isTemplateRemovable( template );
2728
const isRevertable = isTemplateRevertable( template );
@@ -30,6 +31,11 @@ function Actions( { template } ) {
3031
return null;
3132
}
3233

34+
async function revertAndSaveTemplate() {
35+
await revertTemplate( template, { allowUndo: false } );
36+
await saveEditedEntityRecord( 'postType', template.type, template.id );
37+
}
38+
3339
return (
3440
<DropdownMenu
3541
icon={ moreVertical }
@@ -52,7 +58,7 @@ function Actions( { template } ) {
5258
<MenuItem
5359
info={ __( 'Restore template to theme default' ) }
5460
onClick={ () => {
55-
revertTemplate( template );
61+
revertAndSaveTemplate();
5662
onClose();
5763
} }
5864
>

packages/edit-site/src/store/actions.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,12 @@ export function setIsListViewOpened( isOpen ) {
338338
/**
339339
* Reverts a template to its original theme-provided file.
340340
*
341-
* @param {Object} template The template to revert.
341+
* @param {Object} template The template to revert.
342+
* @param {Object} [options]
343+
* @param {boolean} [options.allowUndo] Whether to allow the user to undo
344+
* reverting the template. Default true.
342345
*/
343-
export function* revertTemplate( template ) {
346+
export function* revertTemplate( template, { allowUndo = false } = {} ) {
344347
if ( ! isTemplateRevertable( template ) ) {
345348
yield controls.dispatch(
346349
noticesStore,
@@ -428,32 +431,40 @@ export function* revertTemplate( template ) {
428431
}
429432
);
430433

431-
const undoRevert = async () => {
432-
await dispatch( coreStore ).editEntityRecord(
433-
'postType',
434-
template.type,
435-
edited.id,
434+
if ( allowUndo ) {
435+
const undoRevert = async () => {
436+
await dispatch( coreStore ).editEntityRecord(
437+
'postType',
438+
template.type,
439+
edited.id,
440+
{
441+
content: serializeBlocks,
442+
blocks: edited.blocks,
443+
source: 'custom',
444+
}
445+
);
446+
};
447+
yield controls.dispatch(
448+
noticesStore,
449+
'createSuccessNotice',
450+
__( 'Template reverted.' ),
436451
{
437-
content: serializeBlocks,
438-
blocks: edited.blocks,
439-
source: 'custom',
452+
type: 'snackbar',
453+
actions: [
454+
{
455+
label: __( 'Undo' ),
456+
onClick: undoRevert,
457+
},
458+
],
440459
}
441460
);
442-
};
443-
yield controls.dispatch(
444-
noticesStore,
445-
'createSuccessNotice',
446-
__( 'Template reverted.' ),
447-
{
448-
type: 'snackbar',
449-
actions: [
450-
{
451-
label: __( 'Undo' ),
452-
onClick: undoRevert,
453-
},
454-
],
455-
}
456-
);
461+
} else {
462+
yield controls.dispatch(
463+
noticesStore,
464+
'createSuccessNotice',
465+
__( 'Template reverted.' )
466+
);
467+
}
457468
} catch ( error ) {
458469
const errorMessage =
459470
error.message && error.code !== 'unknown_error'

0 commit comments

Comments
 (0)