2019-10-29 12:01:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { COLLECTIONS_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2020-03-13 20:05:45 +00:00
|
|
|
import { useRef } from '@wordpress/element';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useShallowEqual, useThrowError } from '@woocommerce/base-hooks';
|
2023-01-01 23:22:08 +00:00
|
|
|
import { isError, isObject } from '@woocommerce/types';
|
2019-10-29 12:01:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a custom hook that is wired up to the `wc/store/collections` data
|
|
|
|
* store. Given a collections option object, this will ensure a component is
|
|
|
|
* kept up to date with the collection matching that query in the store state.
|
|
|
|
*
|
2020-01-31 13:01:34 +00:00
|
|
|
* @throws {Object} Throws an exception object if there was a problem with the
|
|
|
|
* API request, to be picked up by BlockErrorBoundry.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} options An object declaring the various
|
|
|
|
* collection arguments.
|
|
|
|
* @param {string} options.namespace The namespace for the collection.
|
|
|
|
* Example: `'/wc/blocks'`
|
|
|
|
* @param {string} options.resourceName The name of the resource for the
|
|
|
|
* collection. Example:
|
|
|
|
* `'products/attributes'`
|
|
|
|
* @param {Array} [options.resourceValues] An array of values (in correct order)
|
|
|
|
* that are substituted in the route
|
|
|
|
* placeholders for the collection route.
|
|
|
|
* Example: `[10, 20]`
|
|
|
|
* @param {Object} [options.query] An object of key value pairs for the
|
|
|
|
* query to execute on the collection
|
|
|
|
* Example:
|
|
|
|
* `{ order: 'ASC', order_by: 'price' }`
|
|
|
|
* @param {boolean} [options.shouldSelect] If false, the previous results will be
|
|
|
|
* returned and internal selects will not
|
|
|
|
* fire.
|
2019-10-29 12:01:13 +00:00
|
|
|
*
|
|
|
|
* @return {Object} This hook will return an object with two properties:
|
|
|
|
* - results An array of collection items returned.
|
|
|
|
* - isLoading A boolean indicating whether the collection is
|
|
|
|
* loading (true) or not.
|
|
|
|
*/
|
2022-02-07 09:34:30 +00:00
|
|
|
|
|
|
|
export interface useCollectionOptions {
|
|
|
|
namespace: string;
|
|
|
|
resourceName: string;
|
|
|
|
resourceValues?: number[];
|
2022-06-15 12:50:42 +00:00
|
|
|
query?: Record< string, unknown >;
|
2022-02-07 09:34:30 +00:00
|
|
|
shouldSelect?: boolean;
|
2023-01-01 23:22:08 +00:00
|
|
|
isEditor?: boolean;
|
2022-02-07 09:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useCollection = (
|
|
|
|
options: useCollectionOptions
|
|
|
|
): {
|
|
|
|
results: unknown;
|
|
|
|
isLoading: boolean;
|
|
|
|
} => {
|
2019-11-11 10:32:56 +00:00
|
|
|
const {
|
|
|
|
namespace,
|
|
|
|
resourceName,
|
|
|
|
resourceValues = [],
|
|
|
|
query = {},
|
2019-11-15 16:11:48 +00:00
|
|
|
shouldSelect = true,
|
2023-01-01 23:22:08 +00:00
|
|
|
isEditor = false,
|
2019-11-11 10:32:56 +00:00
|
|
|
} = options;
|
2019-10-29 12:01:13 +00:00
|
|
|
if ( ! namespace || ! resourceName ) {
|
|
|
|
throw new Error(
|
|
|
|
'The options object must have valid values for the namespace and ' +
|
2019-11-14 16:10:50 +00:00
|
|
|
'the resource properties.'
|
2019-10-29 12:01:13 +00:00
|
|
|
);
|
|
|
|
}
|
2022-02-07 09:34:30 +00:00
|
|
|
const currentResults = useRef< { results: unknown; isLoading: boolean } >( {
|
|
|
|
results: [],
|
|
|
|
isLoading: true,
|
|
|
|
} );
|
2019-10-29 12:01:13 +00:00
|
|
|
// ensure we feed the previous reference if it's equivalent
|
|
|
|
const currentQuery = useShallowEqual( query );
|
|
|
|
const currentResourceValues = useShallowEqual( resourceValues );
|
2020-03-13 20:05:45 +00:00
|
|
|
const throwError = useThrowError();
|
2019-11-15 16:11:48 +00:00
|
|
|
const results = useSelect(
|
2019-10-29 12:01:13 +00:00
|
|
|
( select ) => {
|
2023-01-01 23:22:08 +00:00
|
|
|
if (
|
|
|
|
isEditor &&
|
|
|
|
isObject( currentResults?.current?.results ) &&
|
|
|
|
Object.keys( currentResults.current.results ).length > 0
|
|
|
|
) {
|
|
|
|
return {
|
|
|
|
results: currentResults.current.results,
|
|
|
|
isLoading: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:11:48 +00:00
|
|
|
if ( ! shouldSelect ) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-01 23:22:08 +00:00
|
|
|
|
2019-10-29 12:01:13 +00:00
|
|
|
const store = select( storeKey );
|
|
|
|
const args = [
|
|
|
|
namespace,
|
|
|
|
resourceName,
|
|
|
|
currentQuery,
|
|
|
|
currentResourceValues,
|
2019-11-11 10:32:56 +00:00
|
|
|
];
|
2020-01-31 13:01:34 +00:00
|
|
|
const error = store.getCollectionError( ...args );
|
2020-02-18 11:37:39 +00:00
|
|
|
|
2020-01-31 13:01:34 +00:00
|
|
|
if ( error ) {
|
2022-02-07 09:34:30 +00:00
|
|
|
if ( isError( error ) ) {
|
|
|
|
throwError( error );
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
'TypeError: `error` object is not an instance of Error constructor'
|
|
|
|
);
|
|
|
|
}
|
2020-01-31 13:01:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 12:01:13 +00:00
|
|
|
return {
|
2022-02-07 09:34:30 +00:00
|
|
|
results: store.getCollection< T >( ...args ),
|
2019-10-30 09:57:39 +00:00
|
|
|
isLoading: ! store.hasFinishedResolution(
|
|
|
|
'getCollection',
|
|
|
|
args
|
|
|
|
),
|
2019-10-29 12:01:13 +00:00
|
|
|
};
|
|
|
|
},
|
2019-11-15 16:11:48 +00:00
|
|
|
[
|
|
|
|
namespace,
|
|
|
|
resourceName,
|
|
|
|
currentResourceValues,
|
|
|
|
currentQuery,
|
|
|
|
shouldSelect,
|
|
|
|
]
|
2019-10-29 12:01:13 +00:00
|
|
|
);
|
2019-11-15 16:11:48 +00:00
|
|
|
// if selector was not bailed, then update current results. Otherwise return
|
|
|
|
// previous results
|
|
|
|
if ( results !== null ) {
|
|
|
|
currentResults.current = results;
|
|
|
|
}
|
|
|
|
return currentResults.current;
|
2019-10-29 12:01:13 +00:00
|
|
|
};
|