Skip to content

Commit

Permalink
Propose useEntityRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Feb 15, 2022
1 parent 8b8886c commit e7ac34e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core-data/src/hooks/use-entity-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface EntityRecordsResolution< RecordType > {
* @param kind Kind of the requested entities.
* @param name Name of the requested entities.
* @param queryArgs HTTP query for the requested entities.
* Resolves the specified entity record.
*
* @example
* ```js
Expand Down
30 changes: 25 additions & 5 deletions packages/core-data/src/hooks/use-query-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,28 @@ interface QuerySelectResponse {
* @return {QuerySelectResponse} Queried data.
*/
export default function __experimentalUseQuerySelect( mapQuerySelect, deps ) {
return useSelect( ( select, registry ) => {
const resolve = ( store ) => enrichSelectors( select( store ) );
return mapQuerySelect( resolve, registry );
}, deps );
const { data, isResolving, hasResolved, ...rest } = useSelect(
( select, registry ) => {
const resolve = ( store ) => enrichSelectors( select( store ) );
return mapQuerySelect( resolve, registry );
},
deps
);

let status;
if ( isResolving ) {
status = Status.Resolving;
} else if ( hasResolved ) {
if ( data ) {
status = Status.Success;
} else {
status = Status.Error;
}
} else {
status = Status.Idle;
}

return { data, isResolving, hasResolved, status, ...rest };
}

type QuerySelector = ( ...args ) => QuerySelectResponse;
Expand Down Expand Up @@ -122,7 +140,9 @@ const enrichSelectors = memoize( ( selectors ) => {
data,
status,
isResolving,
hasResolved,
hasResolved:
! isResolving &&
hasFinishedResolution( selectorName, args ),
};
},
} );
Expand Down

0 comments on commit e7ac34e

Please sign in to comment.