woocommerce/plugins/woocommerce-blocks/assets/js/base/hooks/use-collection-header.js

91 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { COLLECTIONS_STORE_KEY as storeKey } from '@woocommerce/block-data';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { useShallowEqual } from './use-shallow-equal';
/**
* This is a custom hook that is wired up to the `wc/store/collections` data
* store. Given a header key and a collections option object, this will ensure a
* component is kept up to date with the collection header value matching that
* query in the store state.
*
* @param {string} headerKey Used to indicate which header value to
* return for the given collection query.
* Example: `'x-wp-total'`
* @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'`
Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
* @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
* (optional). Example:
* `{ order: 'ASC', order_by: 'price' }`
*
* @return {Object} This hook will return an object with two properties:
* - value Whatever value is attached to the specified
* header.
* - isLoading A boolean indicating whether the header is
* loading (true) or not.
*/
export const useCollectionHeader = ( headerKey, options ) => {
Filter all products block by attribute (https://github.com/woocommerce/woocommerce-blocks/pull/1127) * Block setup * Working filtering and intersection of arrays * Implement block settings and no attribute placeholder * Correctly toggle counts * Implement filtering * Fix price slider constraints * Fix price slider constraints * Edit mode * Rename ProductAttributeControl to ProductAttributeTermControl * Attribute ID saving * fix incorrect test fixtures * fix incorrect regex for parsing model (or resource names) from the route. * Fix query classes for some endpoints * Style improvements * Update inline comments * use previous tests * Show attribute control in sidebar * Remove displayStyle option * Sort attributes by name * Show more/less toggle * Use renderFrontend * Only sort when adding values * Rename memo placeholder * More specific CSS for pointer * Update assets/js/base/hooks/use-query-state.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Remove always true taxonomy check * Update assets/js/blocks/attribute-filter/block.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Remove lodash join * native js for string casting * Move internal deps * hyphenate attributes * Correct data set names * Remove unwanted dependency * Moving imports * Missing deps * replace yoda conditonal * Missing deps * Missing deps * Check value exists * Remove undefined filter * renderedOptions usememo * Set defaults in checkbox list * Show more/less refactor * Use getAdminLink * Fix object length check * Correct AND query handling for counts * useQueryStateByContext * Add store rest endpoints * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/blocks/attribute-filter/block.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Feedback * feedback * API readme * Fix API relation queries for multiple attributes * Prevent all options flashing visible during loads * null check * Improve loading state * Remove null options change - it's no longer needed
2019-11-11 10:32:56 +00:00
const {
namespace,
resourceName,
resourceValues = [],
query = {},
} = options;
if ( ! namespace || ! resourceName ) {
throw new Error(
'The options object must have valid values for the namespace and ' +
'the resource name properties.'
);
}
// ensure we feed the previous reference if it's equivalent
const currentQuery = useShallowEqual( query );
const currentResourceValues = useShallowEqual( resourceValues );
const { value, isLoading = true } = useSelect(
( select ) => {
const store = select( storeKey );
// filter out query if it is undefined.
const args = [
headerKey,
namespace,
resourceName,
currentQuery,
currentResourceValues,
Filter all products block by attribute (https://github.com/woocommerce/woocommerce-blocks/pull/1127) * Block setup * Working filtering and intersection of arrays * Implement block settings and no attribute placeholder * Correctly toggle counts * Implement filtering * Fix price slider constraints * Fix price slider constraints * Edit mode * Rename ProductAttributeControl to ProductAttributeTermControl * Attribute ID saving * fix incorrect test fixtures * fix incorrect regex for parsing model (or resource names) from the route. * Fix query classes for some endpoints * Style improvements * Update inline comments * use previous tests * Show attribute control in sidebar * Remove displayStyle option * Sort attributes by name * Show more/less toggle * Use renderFrontend * Only sort when adding values * Rename memo placeholder * More specific CSS for pointer * Update assets/js/base/hooks/use-query-state.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Remove always true taxonomy check * Update assets/js/blocks/attribute-filter/block.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Remove lodash join * native js for string casting * Move internal deps * hyphenate attributes * Correct data set names * Remove unwanted dependency * Moving imports * Missing deps * replace yoda conditonal * Missing deps * Missing deps * Check value exists * Remove undefined filter * renderedOptions usememo * Set defaults in checkbox list * Show more/less refactor * Use getAdminLink * Fix object length check * Correct AND query handling for counts * useQueryStateByContext * Add store rest endpoints * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/base/components/checkbox-list/index.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update assets/js/blocks/attribute-filter/block.js Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Feedback * feedback * API readme * Fix API relation queries for multiple attributes * Prevent all options flashing visible during loads * null check * Improve loading state * Remove null options change - it's no longer needed
2019-11-11 10:32:56 +00:00
];
return {
value: store.getCollectionHeader( ...args ),
isLoading: store.hasFinishedResolution(
'getCollectionHeader',
args
),
};
},
[
headerKey,
namespace,
resourceName,
currentResourceValues,
currentQuery,
]
);
return {
value,
isLoading,
};
};