Core Data: Treat single-item responses specially#76318
Conversation
|
Size Change: +32 B (0%) Total Size: 6.89 MB
ℹ️ View Unchanged
|
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in 6b68e83. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/22895526360
|
jsnajdr
left a comment
There was a problem hiding this comment.
Looks good, just a few code nitpicks 🙂
| // reducer tracks only a single query object. | ||
| onSubKey( 'stableKey' ), | ||
| ] )( ( state = {}, action ) => { | ||
| const { type, page, perPage, key = DEFAULT_ENTITY_KEY } = action; |
There was a problem hiding this comment.
I wanted to avoid this destructuring because it's not strictly type-safe and after a potential TypeScript rewrite it would be an error. It accesses fields like page on actions of all types, even these that don't have the field at all.
| 'postType', | ||
| template.type, | ||
| template | ||
| ); |
There was a problem hiding this comment.
Yes, this is the one, thanks for finding it 😆
There was a problem hiding this comment.
I added this, what did I do? 🙊
|
Thanks, @jsnajdr! Pushed changes based your feedback ✅ |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| } | ||
|
|
||
| if ( type !== 'RECEIVE_ITEMS' ) { | ||
| if ( ! Array.isArray( action.items ) ) { |
There was a problem hiding this comment.
This maybe deserves a comment: when items is only an individual item, then the action doesn't have any information for updating the item list.
| ); | ||
| records = Array.isArray( records ) | ||
| ? records.map( addTitleToAutoDraft ) | ||
| : addTitleToAutoDraft( records ); |
There was a problem hiding this comment.
It's not possible to receive multiple auto-drafts?
There was a problem hiding this comment.
Maybe not, but this case was here, we just adjusted it to work with this concept.
There was a problem hiding this comment.
You mean that there can ever be only one auto-draft in any list of received entities? Then addTitleToAutoDraft will find and update only max one, no problem. This code merely aims to preserve single-item records parameter, without converting it to one-element array.
There was a problem hiding this comment.
The collection can also have more auto-draft records, if created programmatically. Here's one case that comes to mind - #56881. I know it's rare, but technically still possible.
| return state; | ||
| } | ||
|
|
||
| const key = action.key || DEFAULT_ENTITY_KEY; |
There was a problem hiding this comment.
Isn't this subtly different from the prior destructuring? Technically, right now, if action.key is 0, this will fall back to the default key due to the boolean check. Previously, this would only happen if key is not defined.
Maybe that's alright and even safer than before, but just wanted to point it out.
There was a problem hiding this comment.
Good point. I'll change it to action.key ?? DEFAULT_ENTITY_KEY.
|
I'm going to merge this if there's no further feedback. Then I can land the remaining improvements for revisions - #76043. |
What?
Extracted from #76043. Props to @jsnajdr!
PR fixes a bug where receiving a single-entity item could override or corrupt the entire collection.
queries.itemIdsunless we're dealing with collections.Why?
See #76043 (comment).
Testing Instructions
I've included basic integration tests, happy to include more.
Testing Instructions for Keyboard
Same.