Fix Product Rating Block not showing when manually inserted to Single Product block (https://github.com/woocommerce/woocommerce-blocks/pull/9413)

This commit is contained in:
Alexandre Lara 2023-05-16 15:28:11 -03:00 committed by GitHub
parent 90573156fe
commit f22036af89
2 changed files with 20 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import {
import type { BlockEditProps } from '@wordpress/blocks';
import { useEffect } from '@wordpress/element';
import { ProductQueryContext as Context } from '@woocommerce/blocks/product-query/types';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
@ -33,11 +34,25 @@ const Edit = ( {
...context,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
const { isDescendentOfSingleProductBlock } = useSelect( ( select ) => {
const { getBlockParentsByBlockName } = select( 'core/block-editor' );
const blockParentBlocksIds = getBlockParentsByBlockName(
blockProps?.id?.replace( 'block-', '' ),
[ 'woocommerce/single-product' ]
);
return {
isDescendentOfSingleProductBlock: blockParentBlocksIds.length > 0,
};
} );
useEffect(
() => setAttributes( { isDescendentOfQueryLoop } ),
[ setAttributes, isDescendentOfQueryLoop ]
);
useEffect( () => {
setAttributes( { isDescendentOfQueryLoop } );
setAttributes( { isDescendentOfSingleProductBlock } );
}, [
setAttributes,
isDescendentOfQueryLoop,
isDescendentOfSingleProductBlock,
] );
return (
<>

View File

@ -1,5 +1,6 @@
export interface BlockAttributes {
productId: number;
isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductBlock: boolean;
textAlign: string;
}