Skip to content

Commit

Permalink
Add mutations data and helper functions to __experimentalUseEntityRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Mar 19, 2022
1 parent 3b82c06 commit 06f1c6d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,5 @@ _Parameters_
Action triggered to undo the last edit to
an entity record, if any.


<!-- END TOKEN(Autogenerated actions|../../../packages/core-data/src/actions.js) -->
1 change: 1 addition & 0 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ _Returns_

- `boolean`: Whether the entity record is saving or not.


<!-- END TOKEN(Autogenerated selectors|src/selectors.js) -->

## Contributing to this package
Expand Down
58 changes: 52 additions & 6 deletions packages/core-data/src/hooks/use-entity-record.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand All @@ -9,11 +15,25 @@ interface EntityRecordResolution< RecordType > {
/** The requested entity record */
record: RecordType | null;

/** The edited entity record */
editedRecord: Partial< RecordType >;

/** Apply edits to the edited entity record */
edit: ( diff: Partial< RecordType > ) => void;

/** Persist the edits to the server */
save: () => Promise< void >;

/**
* Is the record still being resolved?
*/
isResolving: boolean;

/**
* Does the record have any edits?
*/
hasEdits: boolean;

/**
* Is the record resolved by now?
*/
Expand All @@ -30,10 +50,10 @@ interface Options {
/**
* Resolves the specified entity record.
*
* @param kind Kind of the requested entity.
* @param name Name of the requested entity.
* @param recordId Record ID of the requested entity.
* @param options Hook options.
* @param kind Kind of the requested entity.
* @param name Name of the requested entity.
* @param recordId Record ID of the requested entity.
* @param options Hook options.
* @param [options.enabled=true] Whether to run the query or short-circuit and return null. Defaults to true.
* @example
* ```js
Expand Down Expand Up @@ -66,7 +86,30 @@ export default function __experimentalUseEntityRecord< RecordType >(
recordId: string | number,
options: Options = { enabled: true }
): EntityRecordResolution< RecordType > {
const { data: record, ...rest } = useQuerySelect(
const { editEntityRecord, saveEditedEntityRecord } = useDispatch(
coreStore
);

const mutations = useMemo(
() => ( {
edit: ( record ) =>
editEntityRecord( kind, name, recordId, record ),
save: () => saveEditedEntityRecord( kind, name, recordId ),
} ),
[ recordId ]
);

const { editedRecord, hasEdits } = useSelect(
( select ) => ( {
editedRecord: select( coreStore ).getEditedEntityRecord(),
hasEdits: select( coreStore ).hasEditsForEntityRecord(),
} ),
kind,
name,
recordId
);

const { data: record, ...querySelectRest } = useQuerySelect(
( query ) => {
if ( ! options.enabled ) {
return null;
Expand All @@ -78,6 +121,9 @@ export default function __experimentalUseEntityRecord< RecordType >(

return {
record,
...rest,
editedRecord,
hasEdits,
...querySelectRest,
...mutations,
};
}

0 comments on commit 06f1c6d

Please sign in to comment.