2022-09-23 13:07:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { store as WP_BLOCKS_STORE } from '@wordpress/blocks';
|
|
|
|
|
2022-08-18 08:02:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-10-27 17:40:10 +00:00
|
|
|
import { QUERY_LOOP_ID } from './constants';
|
2022-08-18 08:02:21 +00:00
|
|
|
import {
|
|
|
|
ProductQueryArguments,
|
|
|
|
ProductQueryBlock,
|
|
|
|
QueryVariation,
|
|
|
|
} from './types';
|
|
|
|
|
2022-09-23 13:07:44 +00:00
|
|
|
/**
|
|
|
|
* Creates an array that is the symmetric difference of the given arrays
|
|
|
|
*/
|
|
|
|
export function ArrayXOR< T extends Array< unknown > >( a: T, b: T ) {
|
|
|
|
return a.filter( ( el ) => ! b.includes( el ) );
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:02:21 +00:00
|
|
|
/**
|
|
|
|
* Identifies if a block is a Query block variation from our conventions
|
|
|
|
*
|
|
|
|
* We are extending Gutenberg's core Query block with our variations, and
|
|
|
|
* also adding extra namespaced attributes. If those namespaced attributes
|
|
|
|
* are present, we can be fairly sure it is our own registered variation.
|
|
|
|
*/
|
|
|
|
export function isWooQueryBlockVariation( block: ProductQueryBlock ) {
|
|
|
|
return (
|
2022-10-27 17:40:10 +00:00
|
|
|
block.name === QUERY_LOOP_ID &&
|
2022-08-18 08:02:21 +00:00
|
|
|
Object.values( QueryVariation ).includes(
|
2022-09-23 13:07:44 +00:00
|
|
|
block.attributes.namespace as QueryVariation
|
2022-08-18 08:02:21 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the new query arguments of a Product Query block
|
|
|
|
*
|
|
|
|
* Because we add a new set of deeply nested attributes to the query
|
|
|
|
* block, this utility function makes it easier to change just the
|
|
|
|
* options relating to our custom query, while keeping the code
|
|
|
|
* clean.
|
|
|
|
*/
|
|
|
|
export function setCustomQueryAttribute(
|
|
|
|
block: ProductQueryBlock,
|
2022-09-23 13:07:44 +00:00
|
|
|
queryParams: Partial< ProductQueryArguments >
|
2022-08-18 08:02:21 +00:00
|
|
|
) {
|
2022-09-23 13:07:44 +00:00
|
|
|
const { query } = block.attributes;
|
2022-08-18 08:02:21 +00:00
|
|
|
|
|
|
|
block.setAttributes( {
|
2022-09-23 13:07:44 +00:00
|
|
|
query: {
|
|
|
|
...query,
|
|
|
|
...queryParams,
|
2022-08-18 08:02:21 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
2022-09-23 13:07:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook that returns the query properties' names defined by the active
|
|
|
|
* block variation, to determine which block inspector controls to show.
|
|
|
|
*
|
|
|
|
* @param {Object} attributes Block attributes.
|
|
|
|
* @return {string[]} An array of the controls keys.
|
|
|
|
*/
|
|
|
|
export function useAllowedControls(
|
|
|
|
attributes: ProductQueryBlock[ 'attributes' ]
|
|
|
|
) {
|
|
|
|
return useSelect(
|
|
|
|
( select ) =>
|
|
|
|
select( WP_BLOCKS_STORE ).getActiveBlockVariation(
|
2022-10-27 17:40:10 +00:00
|
|
|
QUERY_LOOP_ID,
|
2022-09-23 13:07:44 +00:00
|
|
|
attributes
|
2022-10-11 14:04:54 +00:00
|
|
|
)?.allowedControls,
|
2022-09-23 13:07:44 +00:00
|
|
|
|
|
|
|
[ attributes ]
|
|
|
|
);
|
|
|
|
}
|