Skip to content
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

Core data: getEditedEntityRecord: do not return empty object #60988

Merged
merged 7 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

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

This undefined< EntityRecord > type exposes a bug in the docgen tool. The type in the source is ET.Updatable<EntityRecord>, and is imported with import type * as ET from './types';. Apparently the tool is unable to follow the wildcard references.

After I change the import to be non-wildcard (import { Updatable, ... } from './types') then docgen can process it correctly.

Copy link
Member

Choose a reason for hiding this comment

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

Fixing this in #61097.

- `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 => ( {
Copy link
Member Author

Choose a reason for hiding this comment

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

It's interesting how the type and docs already accounted for 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 );
Copy link
Member Author

Choose a reason for hiding this comment

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

We could probably avoid the getEntityRecordEdits call if the raw entity is not defined yet?

// 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
Loading