From e7ac34e5e9fdb3051d9d093fad5d4ab73833ba0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 14 Feb 2022 14:01:11 +0100 Subject: [PATCH] Propose useEntityRecords --- .../core-data/src/hooks/use-entity-records.ts | 1 + .../core-data/src/hooks/use-query-select.ts | 30 +++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/hooks/use-entity-records.ts b/packages/core-data/src/hooks/use-entity-records.ts index 29e7fd2a9a3fe..36c5c2130f1cc 100644 --- a/packages/core-data/src/hooks/use-entity-records.ts +++ b/packages/core-data/src/hooks/use-entity-records.ts @@ -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 diff --git a/packages/core-data/src/hooks/use-query-select.ts b/packages/core-data/src/hooks/use-query-select.ts index 4de52a28d8ba1..338c98c68e4eb 100644 --- a/packages/core-data/src/hooks/use-query-select.ts +++ b/packages/core-data/src/hooks/use-query-select.ts @@ -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; @@ -122,7 +140,9 @@ const enrichSelectors = memoize( ( selectors ) => { data, status, isResolving, - hasResolved, + hasResolved: + ! isResolving && + hasFinishedResolution( selectorName, args ), }; }, } );