-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add __experimentalUpdateSpecifiedEntityEdits and use it on the Font Library update button #60390
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -798,6 +798,59 @@ export const saveEditedEntityRecord = | |||||||||||||||
return await dispatch.saveEntityRecord( kind, name, record, options ); | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Action triggered to update only specified properties for the entity. | ||||||||||||||||
* | ||||||||||||||||
* @param {string} kind Kind of the entity. | ||||||||||||||||
* @param {string} name Name of the entity. | ||||||||||||||||
* @param {Object} recordId ID of the record. | ||||||||||||||||
* @param {Array} itemsToSave List of entity properties or property paths to save. | ||||||||||||||||
* @param {Object} options Saving options. | ||||||||||||||||
*/ | ||||||||||||||||
export const __experimentalUpdateSpecifiedEntityEdits = | ||||||||||||||||
( kind, name, recordId, itemsToSave, options ) => | ||||||||||||||||
async ( { select, dispatch } ) => { | ||||||||||||||||
if ( ! select.hasEditsForEntityRecord( kind, name, recordId ) ) { | ||||||||||||||||
return; | ||||||||||||||||
} | ||||||||||||||||
const edits = select.getEntityRecordNonTransientEdits( | ||||||||||||||||
kind, | ||||||||||||||||
name, | ||||||||||||||||
recordId | ||||||||||||||||
); | ||||||||||||||||
|
||||||||||||||||
// Get the base record from the datase to apply the edits to. | ||||||||||||||||
const base = select.getEntityRecord( kind, name, recordId ) || {}; | ||||||||||||||||
|
||||||||||||||||
// Use the base record as the starting point for the edits. | ||||||||||||||||
const editsToSave = base; | ||||||||||||||||
Comment on lines
+822
to
+826
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like these lines are the only difference here from Is there a reason that cc @getdave @ntsekouras who had PRs that used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, too, I decided to create a new one to be explicit about the difference with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still, if we find that the behavior of |
||||||||||||||||
|
||||||||||||||||
for ( const item of itemsToSave ) { | ||||||||||||||||
setNestedValue( editsToSave, item, getNestedValue( edits, item ) ); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) ); | ||||||||||||||||
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, | ||||||||||||||||
editsToSave, | ||||||||||||||||
options | ||||||||||||||||
); | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Action triggered to save only specified properties for the entity. | ||||||||||||||||
* | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting that we shouldn't be adding new experimental APIs anymore, we should add private APIs instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it's not really clear what this API offers in addition to what saveEntityRecord does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saveEntityRecord
saves all the changes to the database.__experimentalUpdateSpecifiedEntityEdits
saves only the specified edits.Let's take this case as example:
Using
saveEntityRecord
{ "a": "a", "b":"b", "c":"c" }
is persisted to the database.Using
__experimentalUpdateSpecifiedEntityEdits
:{ "a": "a", "c":"c" }
is to the database.Using
__experimentalSaveSpecifiedEntityEdits
:{"c":"c" }
is to the database without changing the client-side entity.I added these examples to the PR's description.
Probably the behavior added in this PR for
__experimentalUpdateSpecifiedEntityEdits
was the one originally thought for__experimentalSaveSpecifiedEntityEdits
. In that case, we can move the additions to that function instead of adding a new one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But in this case, can't we just send
saveEntityRecord( 'root', 'post-type', 1, { "c":"c" } )
directly? In other words, can't we retrieve the edits that we want to save and just pass them (or not even mark them as edits). I'm pretty sure we do this already in other places already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__experimentalSaveSpecifiedEntityEdits
to be honest I don't know much about this function, I need to check the history.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that it's possible that it's an endpoint issue then because if I'm not wrong all of the endpoints in Core work as "PATCH" (maybe not the global styles one, we need to check)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is also why we didn't see the issue before with
__experimentalSaveSpecifiedEntityEdits
(we never used it with the global styles endpoint before)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh so you're actually trying to send only
settings.typography.fontFamilies
and notsettings
. That is going to override the whole "settings" object of course. So IMO, you should just send the updatesettings
object entirely.The endpoints don't do "nested patches", only top level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is true for top level keys.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct in my testing for the global-styles endpoint, and true generally across the REST API.
styles
are preserved here when a font is activated because it's a different top level key, butsettings
are reset (without this PR) because they're empty except forsettings.typography.fontFamilies
.