Skip to content

Commit

Permalink
Core Data: Use new raw entity record selector instead of modifying th…
Browse files Browse the repository at this point in the history
…e existing one.
  • Loading branch information
epiqueras committed Aug 19, 2019
1 parent ff28c04 commit 450b7f2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
16 changes: 16 additions & 0 deletions docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ _Returns_

- `?Object`: The entity record's save error.

<a name="getRawEntityRecord" href="#getRawEntityRecord">#</a> **getRawEntityRecord**

Returns the entity's record object by key,
with its attributes mapped to their raw values.

_Parameters_

- _state_ `Object`: State tree.
- _kind_ `string`: Entity kind.
- _name_ `string`: Entity name.
- _key_ `number`: Record's key.

_Returns_

- `?Object`: Object with the entity's raw attributes.

<a name="getRedoEdit" href="#getRedoEdit">#</a> **getRedoEdit**

Returns the next edit from the current undo offset
Expand Down
16 changes: 16 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ _Returns_

- `?Object`: The entity record's save error.

<a name="getRawEntityRecord" href="#getRawEntityRecord">#</a> **getRawEntityRecord**

Returns the entity's record object by key,
with its attributes mapped to their raw values.

_Parameters_

- _state_ `Object`: State tree.
- _kind_ `string`: Entity kind.
- _name_ `string`: Entity name.
- _key_ `number`: Record's key.

_Returns_

- `?Object`: Object with the entity's raw attributes.

<a name="getRedoEdit" href="#getRedoEdit">#</a> **getRedoEdit**

Returns the next edit from the current undo offset
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function* editEntityRecord( kind, name, recordId, edits ) {
kind,
name
);
const record = yield select( 'getEntityRecord', kind, name, recordId );
const record = yield select( 'getRawEntityRecord', kind, name, recordId );
const editedRecord = yield select(
'getEditedEntityRecord',
kind,
Expand Down Expand Up @@ -243,7 +243,7 @@ export function* saveEntityRecord(
let updatedRecord;
let persistedRecord;
if ( isAutosave || getSuccessNoticeActionArgs || getFailureNoticeActionArgs ) {
persistedRecord = yield select( 'getEntityRecord', kind, name, recordId );
persistedRecord = yield select( 'getRawEntityRecord', kind, name, recordId );
}
let error;
try {
Expand Down
41 changes: 25 additions & 16 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,33 @@ export function getEntity( state, kind, name ) {
*
* @return {Object?} Record.
*/
export const getEntityRecord = createSelector(
export function getEntityRecord( state, kind, name, key ) {
return get( state.entities.data, [ kind, name, 'queriedData', 'items', key ] );
}

/**
* Returns the entity's record object by key,
* with its attributes mapped to their raw values.
*
* @param {Object} state State tree.
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {number} key Record's key.
*
* @return {Object?} Object with the entity's raw attributes.
*/
export const getRawEntityRecord = createSelector(
( state, kind, name, key ) => {
const record = get( state.entities.data, [
kind,
name,
'queriedData',
'items',
key,
] );
const record = getEntityRecord( state, kind, name, key );
return (
record &&
Object.keys( record ).reduce( ( acc, _key ) => {
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
acc[ _key ] = get( record[ _key ], 'raw', record[ _key ] );
return acc;
}, {} )
Object.keys( record ).reduce( ( acc, _key ) => {
// Because edits are the "raw" attribute values,
// we return those from record selectors to make rendering,
// comparisons, and joins with edits easier.
acc[ _key ] = get( record[ _key ], 'raw', record[ _key ] );
return acc;
}, {} )
);
},
( state ) => [ state.entities.data ]
Expand Down Expand Up @@ -213,7 +222,7 @@ export function hasEditsForEntityRecord( state, kind, name, recordId ) {
*/
export const getEditedEntityRecord = createSelector(
( state, kind, name, recordId ) => ( {
...getEntityRecord( state, kind, name, recordId ),
...getRawEntityRecord( state, kind, name, recordId ),
...getEntityRecordEdits( state, kind, name, recordId ),
} ),
( state ) => [ state.entities.data ]
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const getCurrentPost = createRegistrySelector( ( select ) => ( state ) =>
const postId = getCurrentPostId( state );
const postType = getCurrentPostType( state );

const post = select( 'core' ).getEntityRecord( 'postType', postType, postId );
const post = select( 'core' ).getRawEntityRecord( 'postType', postType, postId );
if ( post ) {
return post;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const selectorNames = Object.keys( selectors );
selectorNames.forEach( ( name ) => {
selectors[ name ] = ( state, ...args ) => {
const select = () => ( {
getEntityRecord() {
getRawEntityRecord() {
return state.currentPost;
},

Expand Down Expand Up @@ -69,7 +69,7 @@ selectorNames.forEach( ( name ) => {
};
}
return {
...this.getEntityRecord(),
...this.getRawEntityRecord(),
...edits,
};
},
Expand Down

0 comments on commit 450b7f2

Please sign in to comment.