From 5b4ac3129f809a801d0d1d4d44b88aa18236eb29 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 4 Sep 2023 22:19:29 -0300 Subject: [PATCH] Add edits data to the useEntityRecord --- .../core-data/src/hooks/test/use-entity-record.js | 4 ++++ packages/core-data/src/hooks/use-entity-record.ts | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index f86906f4f74467..d78a64d5f2b628 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -46,6 +46,7 @@ describe( 'useEntityRecord', () => { edit: expect.any( Function ), editedRecord: {}, hasEdits: false, + edits: {}, record: undefined, save: expect.any( Function ), hasResolved: false, @@ -64,6 +65,7 @@ describe( 'useEntityRecord', () => { edit: expect.any( Function ), editedRecord: { hello: 'world', id: 1 }, hasEdits: false, + edits: {}, record: { hello: 'world', id: 1 }, save: expect.any( Function ), hasResolved: true, @@ -92,6 +94,7 @@ describe( 'useEntityRecord', () => { edit: expect.any( Function ), editedRecord: { hello: 'world', id: 1 }, hasEdits: false, + edits: {}, record: { hello: 'world', id: 1 }, save: expect.any( Function ), hasResolved: true, @@ -108,5 +111,6 @@ describe( 'useEntityRecord', () => { expect( widget.record ).toEqual( { hello: 'world', id: 1 } ); expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } ); + expect( widget.edits ).toEqual( { hello: 'foo' } ); } ); } ); diff --git a/packages/core-data/src/hooks/use-entity-record.ts b/packages/core-data/src/hooks/use-entity-record.ts index 310c013ce0b877..01eee562644cf1 100644 --- a/packages/core-data/src/hooks/use-entity-record.ts +++ b/packages/core-data/src/hooks/use-entity-record.ts @@ -19,6 +19,9 @@ export interface EntityRecordResolution< RecordType > { /** The edited entity record */ editedRecord: Partial< RecordType >; + /** The edits to the edited entity record */ + edits: Partial< RecordType >; + /** Apply local (in-browser) edits to the edited entity record */ edit: ( diff: Partial< RecordType > ) => void; @@ -163,7 +166,7 @@ export default function useEntityRecord< RecordType >( [ editEntityRecord, kind, name, recordId, saveEditedEntityRecord ] ); - const { editedRecord, hasEdits } = useSelect( + const { editedRecord, hasEdits, edits } = useSelect( ( select ) => ( { editedRecord: select( coreStore ).getEditedEntityRecord( kind, @@ -175,6 +178,11 @@ export default function useEntityRecord< RecordType >( name, recordId ), + edits: select( coreStore ).getEntityRecordNonTransientEdits( + kind, + name, + recordId + ), } ), [ kind, name, recordId ] ); @@ -195,6 +203,7 @@ export default function useEntityRecord< RecordType >( record, editedRecord, hasEdits, + edits, ...querySelectRest, ...mutations, };