Skip to content

Commit

Permalink
Fix renaming in Site View sidebar rename saves all edits for Template…
Browse files Browse the repository at this point in the history
… Parts and Navigation Menus (#52373)

* Use action to save specific edits to title only

* Fix action to update specified record

* Compute correct recordKey based on entity config

* Fix identical bug with Template Parts

* Update packages/edit-site/src/components/template-actions/rename-menu-item.js

---------

Co-authored-by: Ben Dwyer <ben@scruffian.com>
  • Loading branch information
getdave and scruffian authored Jul 7, 2023
1 parent f093a91 commit 66e8da1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
16 changes: 16 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,22 @@ export const __experimentalSaveSpecifiedEntityEdits =
editsToSave[ edit ] = edits[ edit ];
}
}

const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);

const entityIdKey = entityConfig?.key || DEFAULT_ENTITY_KEY;

// If a record key is provided then update the existing record.
// This necessitates providing `recordKey` to saveEntityRecord as part of the
// `record` argument (here called `editsToSave`) to stop that action creating
// a new record and instead cause it to update the existing record.
if ( recordId ) {
editsToSave[ entityIdKey ] = recordId;
}

return await dispatch.saveEntityRecord(
kind,
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ function useSaveNavigationMenu() {
};
}, [] );

const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );
const {
editEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );

const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );
Expand All @@ -87,11 +89,19 @@ function useSaveNavigationMenu() {
// Apply the edits.
editEntityRecord( 'postType', postType, postId, edits );

const recordPropertiesToSave = Object.keys( edits );

// Attempt to persist.
try {
await saveEditedEntityRecord( 'postType', postType, postId, {
throwOnError: true,
} );
await saveSpecifiedEntityEdits(
'postType',
postType,
postId,
recordPropertiesToSave,
{
throwOnError: true,
}
);
createSuccessNotice( __( 'Renamed Navigation menu' ), {
type: 'snackbar',
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export default function RenameMenuItem( { template, onClose } ) {
const [ title, setTitle ] = useState( () => template.title.rendered );
const [ isModalOpen, setIsModalOpen ] = useState( false );

const { editEntityRecord, saveEditedEntityRecord } =
useDispatch( coreStore );
const {
editEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );

Expand All @@ -42,11 +44,14 @@ export default function RenameMenuItem( { template, onClose } ) {
onClose();

// Persist edited entity.
await saveEditedEntityRecord(
await saveSpecifiedEntityEdits(
'postType',
template.type,
template.id,
{ throwOnError: true }
[ 'title' ], // Only save title to avoid persisting other edits.
{
throwOnError: true,
}
);

createSuccessNotice( __( 'Entity renamed.' ), {
Expand Down

1 comment on commit 66e8da1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 66e8da1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5487693545
📝 Reported issues:

Please sign in to comment.