Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Product Rating block: Refactor the block to use useIsDescendentOfSingleProductBlock hook #9489

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions assets/js/atomic/blocks/product-elements/rating/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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
Expand All @@ -20,6 +19,7 @@ import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';
import { BlockAttributes } from './types';
import './editor.scss';
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';

const Edit = ( {
attributes,
Expand All @@ -34,16 +34,10 @@ 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,
};
} );
const { isDescendentOfSingleProductBlock } =
useIsDescendentOfSingleProductBlock( {
blockClientId: blockProps?.id,
} );

useEffect( () => {
setAttributes( { isDescendentOfQueryLoop } );
Expand Down