Skip to content
10 changes: 10 additions & 0 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,16 @@ _Returns_

- `Object`: Action object.

### clearEntityRecordEdits

Action triggered to clear all edits from an entity record.

_Parameters_

- _kind_ `string`: Kind of the entity.
- _name_ `string`: Name of the entity.
- _recordId_ `number|string`: Record ID of the entity record.

### deleteEntityRecord

Action triggered to delete an entity record.
Expand Down
10 changes: 10 additions & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ _Returns_

- `Object`: Action object.

### clearEntityRecordEdits

Action triggered to clear all edits from an entity record.

_Parameters_

- _kind_ `string`: Kind of the entity.
- _name_ `string`: Name of the entity.
- _recordId_ `number|string`: Record ID of the entity record.

### deleteEntityRecord

Action triggered to delete an entity record.
Expand Down
47 changes: 47 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,53 @@ export const editEntityRecord =
} );
};

/**
* Action triggered to clear all edits from
* an entity record.
*
* @param {string} kind Kind of the entity.
* @param {string} name Name of the entity.
* @param {number|string} recordId Record ID of the entity record.
*/
export const clearEntityRecordEdits =
( kind, name, recordId ) =>
( { select, dispatch } ) => {
const entityConfig = select.getEntityConfig( kind, name );
logEntityDeprecation( kind, name, 'clearEntityRecordEdits' );
if ( ! entityConfig ) {
throw new Error(
`The entity being edited (${ kind }, ${ name }) does not have a loaded config.`
);
}

const currentEdits = select.getEntityRecordEdits(
kind,
name,
recordId
);
if ( ! currentEdits ) {
return;
}

// Build an edits object with all current edit keys set to undefined
// so the reducer removes them.
const clearedEdits = Object.keys( currentEdits ).reduce(
( acc, key ) => {
acc[ key ] = undefined;
return acc;
},
{}
);

dispatch( {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason that this directly dispatches this action instead of calling the editEntityRecord action creator?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, the issue is this logic in editEntityRecord:

const editsWithMerges = Object.keys( edits ).reduce( ( acc, key ) => {
acc[ key ] = mergedEdits[ key ]
? { ...editedRecord[ key ], ...edits[ key ] }
: edits[ key ];
return acc;
}, {} );

To clean up an editedEntity, we need to make the values of all the changes undefined. According to the logic above, when ...edits[ key ] is undefined, ...editedRecord[ key ] will not be replaced.

type: 'EDIT_ENTITY_RECORD',
kind,
name,
recordId,
edits: clearedEdits,
} );
};

/**
* Action triggered to undo the last edit to
* an entity record, if any.
Expand Down
79 changes: 79 additions & 0 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock( '@wordpress/api-fetch' );
*/
import {
editEntityRecord,
clearEntityRecordEdits,
saveEntityRecord,
saveEditedEntityRecord,
deleteEntityRecord,
Expand Down Expand Up @@ -454,6 +455,84 @@ describe( 'editEntityRecord', () => {
} );
} );

describe( 'clearEntityRecordEdits', () => {
it( 'throws when the entity does not have a loaded config.', async () => {
const select = {
getEntityConfig: jest.fn(),
};
const fulfillment = async () =>
clearEntityRecordEdits(
'someKind',
'someName',
'someId'
)( { select } );
await expect( fulfillment ).rejects.toThrow(
`The entity being edited (someKind, someName) does not have a loaded config.`
);
} );

it( 'does nothing when there are no edits', () => {
const dispatch = jest.fn();
const select = {
getEntityConfig: () => ( {
kind: 'postType',
name: 'post',
} ),
getEntityRecordEdits: () => undefined,
};

clearEntityRecordEdits(
'postType',
'post',
1
)( {
select,
dispatch,
} );

expect( dispatch ).not.toHaveBeenCalled();
} );

it( 'clears all edits for an entity record', () => {
const dispatch = jest.fn();
const select = {
getEntityConfig: () => ( {
kind: 'postType',
name: 'post',
} ),
getEntityRecordEdits: () => ( {
title: 'New Title',
content: 'New Content',
} ),
getEditedEntityRecord: () => ( {
id: 1,
title: 'New Title',
content: 'New Content',
} ),
};

clearEntityRecordEdits(
'postType',
'post',
1
)( {
select,
dispatch,
} );

expect( dispatch ).toHaveBeenCalledWith( {
type: 'EDIT_ENTITY_RECORD',
kind: 'postType',
name: 'post',
recordId: 1,
edits: {
title: undefined,
content: undefined,
},
} );
} );
} );

describe( 'deleteEntityRecord', () => {
beforeEach( async () => {
apiFetch.mockReset();
Expand Down
30 changes: 30 additions & 0 deletions packages/core-data/src/test/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,33 @@ describe( 'getEntityRecord', () => {
expect( triggerFetch ).not.toHaveBeenCalled();
} );
} );

describe( 'clearEntityRecordEdits', () => {
let registry;

beforeEach( () => {
registry = createTestRegistry();
triggerFetch.mockReset();
} );

it( 'should return the persisted record after clearing edits', () => {
const post = createTestPost( 1 );
const dispatch = registry.dispatch( coreDataStore );
const select = registry.select( coreDataStore );

dispatch.receiveEntityRecords( 'postType', 'post', post );
dispatch.editEntityRecord( 'postType', 'post', post.id, {
slug: 'updated-slug',
} );

expect(
select.getEditedEntityRecord( 'postType', 'post', post.id ).slug
).toBe( 'updated-slug' );

dispatch.clearEntityRecordEdits( 'postType', 'post', post.id );

expect(
select.getEditedEntityRecord( 'postType', 'post', post.id )
).toEqual( select.getRawEntityRecord( 'postType', 'post', post.id ) );
} );
} );
Loading