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 {
|
|
|
|
ProductQueryBlock,
|
2022-11-21 18:21:58 +00:00
|
|
|
ProductQueryBlockQuery,
|
2022-08-18 08:02:21 +00:00
|
|
|
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
|
|
|
|
*
|
2022-11-21 18:21:58 +00:00
|
|
|
* Shorthand for setting new nested query parameters.
|
2022-08-18 08:02:21 +00:00
|
|
|
*/
|
2022-11-21 18:21:58 +00:00
|
|
|
export function setQueryAttribute(
|
2022-08-18 08:02:21 +00:00
|
|
|
block: ProductQueryBlock,
|
2022-11-21 18:21:58 +00:00
|
|
|
queryParams: Partial< ProductQueryBlockQuery >
|
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
|
|
|
|
2022-12-12 15:06:25 +00:00
|
|
|
// This is a feature flag to enable the custom inherit Global Query implementation.
|
|
|
|
// This is not intended to be a permanent feature flag, but rather a temporary.
|
|
|
|
// https://github.com/woocommerce/woocommerce-blocks/pull/7382
|
|
|
|
export const isCustomInheritGlobalQueryImplementationEnabled = false;
|
|
|
|
|
|
|
|
export function isWooInheritQueryEnabled(
|
|
|
|
attributes: ProductQueryBlock[ 'attributes' ]
|
|
|
|
) {
|
|
|
|
return isCustomInheritGlobalQueryImplementationEnabled
|
|
|
|
? attributes.query.__woocommerceInherit
|
|
|
|
: attributes.query.inherit;
|
|
|
|
}
|
|
|
|
|
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' ]
|
|
|
|
) {
|
2022-12-12 15:06:25 +00:00
|
|
|
const isSiteEditor = useSelect( 'core/edit-site' ) !== undefined;
|
|
|
|
|
|
|
|
const controls = useSelect(
|
2022-09-23 13:07:44 +00:00
|
|
|
( 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 ]
|
|
|
|
);
|
2022-12-12 15:06:25 +00:00
|
|
|
|
|
|
|
if ( ! isSiteEditor ) {
|
|
|
|
return controls.filter( ( control ) => control !== 'wooInherit' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return isWooInheritQueryEnabled( attributes )
|
|
|
|
? controls.filter( ( control ) => control === 'wooInherit' )
|
|
|
|
: controls;
|
2022-09-23 13:07:44 +00:00
|
|
|
}
|