Skip to content

Commit

Permalink
Core data: getEditedEntityRecord: do not return empty object (#60988)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 73f91a2 commit 4ec0363
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ _Parameters_

_Returns_

- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits.
- `undefined< EntityRecord > | false`: The entity record, merged with its edits.

### getEmbedPreview

Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ _Parameters_

_Returns_

- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits.
- `undefined< EntityRecord > | false`: The entity record, merged with its edits.

### getEmbedPreview

Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe( 'useEntityRecord', () => {

expect( data ).toEqual( {
edit: expect.any( Function ),
editedRecord: {},
editedRecord: false,
hasEdits: false,
edits: {},
record: undefined,
Expand Down
21 changes: 16 additions & 5 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,21 @@ export const getEditedEntityRecord = createSelector(
kind: string,
name: string,
recordId: EntityRecordKey
): ET.Updatable< EntityRecord > | undefined => ( {
...getRawEntityRecord( state, kind, name, recordId ),
...getEntityRecordEdits( state, kind, name, recordId ),
} ),
): ET.Updatable< EntityRecord > | false => {
const raw = getRawEntityRecord( state, kind, name, recordId );
const edited = getEntityRecordEdits( state, kind, name, recordId );
// Never return a non-falsy empty object. Unfortunately we can't return
// undefined or null because we were previously returning an empty
// object, so trying to read properties from the result would throw.
// Using false here is a workaround to avoid breaking changes.
if ( ! raw && ! edited ) {
return false;
}
return {
...raw,
...edited,
};
},
(
state: State,
kind: string,
Expand Down Expand Up @@ -1264,7 +1275,7 @@ export function getReferenceByDistinctEdits( state ) {
export function __experimentalGetTemplateForLink(
state: State,
link: string
): Optional< ET.Updatable< ET.WpTemplate > > | null {
): Optional< ET.Updatable< ET.WpTemplate > > | null | false {
const records = getEntityRecords< ET.WpTemplate >(
state,
'postType',
Expand Down

0 comments on commit 4ec0363

Please sign in to comment.