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

Fix Product Rating Block not showing when manually inserted to Single Product Block #9413

Merged
Show file tree
Hide file tree
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
23 changes: 19 additions & 4 deletions assets/js/atomic/blocks/product-elements/rating/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,11 +34,25 @@ const Edit = ( {
...context,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
const { isDescendentOfSingleProductBlock } = useSelect( ( select ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth having this logic stored as a React hook for shared usage across all inner blocks for the Single Product? Could other blocks, such as price and add to cart, also benefit from this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I agree with this, today I opened another PR to an issue that I was investigating and I found out that it was being caused by the same thing, so I created a hook in that PR that I'll be using to refactor this and other blocks that need it

Copy link
Contributor

@nefeline nefeline May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Looking forward to having #9446 merged so this PR can benefit from this update.

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 (
<>
Expand Down
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/product-elements/rating/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface BlockAttributes {
productId: number;
isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductBlock: boolean;
textAlign: string;
}