Add a generic arg to useCollection to improve typing of results. (https://github.com/woocommerce/woocommerce-blocks/pull/10696)

Closes woocommerce/woocommerce-blocks#5726
This commit is contained in:
Sam Seay 2023-08-24 12:49:41 +08:00 committed by GitHub
parent e20c605408
commit a753a0f1d2
4 changed files with 14 additions and 11 deletions

View File

@ -49,10 +49,10 @@ export interface useCollectionOptions {
isEditor?: boolean; isEditor?: boolean;
} }
export const useCollection = ( export const useCollection = < T >(
options: useCollectionOptions options: useCollectionOptions
): { ): {
results: unknown; results: T[];
isLoading: boolean; isLoading: boolean;
} => { } => {
const { const {
@ -68,7 +68,7 @@ export const useCollection = (
'the resource properties.' 'the resource properties.'
); );
} }
const currentResults = useRef< { results: unknown; isLoading: boolean } >( { const currentResults = useRef< { results: T[]; isLoading: boolean } >( {
results: [], results: [],
isLoading: true, isLoading: true,
} ); } );
@ -102,7 +102,7 @@ export const useCollection = (
} }
return { return {
results: store.getCollection< T >( ...args ), results: store.getCollection< T[] >( ...args ),
isLoading: ! store.hasFinishedResolution( isLoading: ! store.hasFinishedResolution(
'getCollection', 'getCollection',
args args

View File

@ -35,16 +35,17 @@ export const useStoreProducts = (
namespace: '/wc/store/v1', namespace: '/wc/store/v1',
resourceName: 'products', resourceName: 'products',
}; };
const { results: products, isLoading: productsLoading } = useCollection( { const { results: products, isLoading: productsLoading } =
...collectionOptions, useCollection< ProductResponseItem >( {
query, ...collectionOptions,
} ); query,
} );
const { value: totalProducts } = useCollectionHeader( 'x-wp-total', { const { value: totalProducts } = useCollectionHeader( 'x-wp-total', {
...collectionOptions, ...collectionOptions,
query, query,
} ); } );
return { return {
products: products as ProductResponseItem[], // TODO: Remove this once getCollection selector and resolver is converted to TS. products,
totalProducts: parseInt( totalProducts as string, 10 ), totalProducts: parseInt( totalProducts as string, 10 ),
productsLoading, productsLoading,
}; };

View File

@ -11,6 +11,7 @@ import { __ } from '@wordpress/i18n';
import { getSettingWithCoercion } from '@woocommerce/settings'; import { getSettingWithCoercion } from '@woocommerce/settings';
import { import {
AttributeObject, AttributeObject,
AttributeTerm,
isAttributeQueryCollection, isAttributeQueryCollection,
isAttributeTermCollection, isAttributeTermCollection,
isBoolean, isBoolean,
@ -47,7 +48,7 @@ const ActiveAttributeFilters = ( {
displayStyle, displayStyle,
isLoadingCallback, isLoadingCallback,
}: ActiveAttributeFiltersProps ) => { }: ActiveAttributeFiltersProps ) => {
const { results, isLoading } = useCollection( { const { results, isLoading } = useCollection< AttributeTerm >( {
namespace: '/wc/store/v1', namespace: '/wc/store/v1',
resourceName: 'products/attributes/terms', resourceName: 'products/attributes/terms',
resourceValues: [ attributeObject.id ], resourceValues: [ attributeObject.id ],

View File

@ -19,6 +19,7 @@ import { getSettingWithCoercion } from '@woocommerce/settings';
import { getQueryArgs, removeQueryArgs } from '@wordpress/url'; import { getQueryArgs, removeQueryArgs } from '@wordpress/url';
import { import {
AttributeQuery, AttributeQuery,
AttributeTerm,
isAttributeQueryCollection, isAttributeQueryCollection,
isBoolean, isBoolean,
isString, isString,
@ -124,7 +125,7 @@ const AttributeFilterBlock = ( {
useQueryStateByKey( 'attributes', [] ); useQueryStateByKey( 'attributes', [] );
const { results: attributeTerms, isLoading: attributeTermsLoading } = const { results: attributeTerms, isLoading: attributeTermsLoading } =
useCollection( { useCollection< AttributeTerm >( {
namespace: '/wc/store/v1', namespace: '/wc/store/v1',
resourceName: 'products/attributes/terms', resourceName: 'products/attributes/terms',
resourceValues: [ attributeObject?.id || 0 ], resourceValues: [ attributeObject?.id || 0 ],