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

Commit cb2ece0

Browse files
Refactor: Add useIsDescendentOfSingleProductTemplate hook (#9657)
* Add useIsDescendentOfSingleProductTemplate hook * Replace logic with useIsDescendentOfSingleProductTemplate hook
1 parent cf6df66 commit cb2ece0

File tree

5 files changed

+36
-50
lines changed

5 files changed

+36
-50
lines changed

assets/js/atomic/blocks/product-elements/price/edit.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
} from '@wordpress/block-editor';
99
import { useEffect } from '@wordpress/element';
1010
import type { BlockAlignment } from '@wordpress/blocks';
11-
import { useSelect } from '@wordpress/data';
1211

1312
/**
1413
* Internal dependencies
1514
*/
1615
import Block from './block';
16+
import { useIsDescendentOfSingleProductTemplate } from '../shared/use-is-descendent-of-single-product-template';
1717

1818
type UnsupportedAligments = 'wide' | 'full';
1919
type AllowedAlignments = Exclude< BlockAlignment, UnsupportedAligments >;
@@ -53,18 +53,12 @@ const PriceEdit = ( {
5353
};
5454
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
5555

56-
const isDescendentOfSingleProductTemplate = useSelect(
57-
( select ) => {
58-
const store = select( 'core/edit-site' );
59-
const postId = store?.getEditedPostId< string | undefined >();
56+
let { isDescendentOfSingleProductTemplate } =
57+
useIsDescendentOfSingleProductTemplate( { isDescendentOfQueryLoop } );
6058

61-
return (
62-
postId?.includes( '//single-product' ) &&
63-
! isDescendentOfQueryLoop
64-
);
65-
},
66-
[ isDescendentOfQueryLoop ]
67-
);
59+
if ( isDescendentOfQueryLoop ) {
60+
isDescendentOfSingleProductTemplate = false;
61+
}
6862

6963
useEffect(
7064
() =>

assets/js/atomic/blocks/product-elements/product-meta/edit.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
*/
44
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
55
import { InnerBlockTemplate } from '@wordpress/blocks';
6-
import { useSelect } from '@wordpress/data';
76

87
/**
98
* Internal dependencies
109
*/
1110
import './editor.scss';
11+
import { useIsDescendentOfSingleProductTemplate } from '../shared/use-is-descendent-of-single-product-template';
1212

1313
const Edit = () => {
14-
const isDescendentOfSingleProductTemplate = useSelect( ( select ) => {
15-
const store = select( 'core/edit-site' );
16-
const postId = store?.getEditedPostId< string | undefined >();
17-
18-
return postId?.includes( '//single-product' );
19-
}, [] );
14+
const isDescendentOfSingleProductTemplate =
15+
useIsDescendentOfSingleProductTemplate();
2016

2117
const TEMPLATE: InnerBlockTemplate[] = [
2218
[

assets/js/atomic/blocks/product-elements/rating/edit.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import type { BlockEditProps } from '@wordpress/blocks';
1010
import { useEffect } from '@wordpress/element';
1111
import { ProductQueryContext as Context } from '@woocommerce/blocks/product-query/types';
12-
import { useSelect } from '@wordpress/data';
1312

1413
/**
1514
* Internal dependencies
@@ -18,6 +17,7 @@ import Block from './block';
1817
import { BlockAttributes } from './types';
1918
import './editor.scss';
2019
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';
20+
import { useIsDescendentOfSingleProductTemplate } from '../shared/use-is-descendent-of-single-product-template';
2121

2222
const Edit = (
2323
props: BlockEditProps< BlockAttributes > & { context: Context }
@@ -36,21 +36,12 @@ const Edit = (
3636
useIsDescendentOfSingleProductBlock( {
3737
blockClientId: blockProps?.id,
3838
} );
39-
const { isDescendentOfSingleProductTemplate } = useSelect(
40-
( select ) => {
41-
const store = select( 'core/edit-site' );
42-
const postId = store?.getEditedPostId< string | undefined >();
39+
let { isDescendentOfSingleProductTemplate } =
40+
useIsDescendentOfSingleProductTemplate();
4341

44-
return {
45-
isDescendentOfSingleProductTemplate: Boolean(
46-
postId?.includes( '//single-product' ) &&
47-
! isDescendentOfQueryLoop &&
48-
! isDescendentOfSingleProductBlock
49-
),
50-
};
51-
},
52-
[ isDescendentOfQueryLoop, isDescendentOfSingleProductBlock ]
53-
);
42+
if ( isDescendentOfQueryLoop || isDescendentOfSingleProductBlock ) {
43+
isDescendentOfSingleProductTemplate = false;
44+
}
5445

5546
useEffect( () => {
5647
setAttributes( {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { useSelect } from '@wordpress/data';
5+
6+
export const useIsDescendentOfSingleProductTemplate = () => {
7+
const isDescendentOfSingleProductTemplate = useSelect( ( select ) => {
8+
const store = select( 'core/edit-site' );
9+
const postId = store?.getEditedPostId< string | undefined >();
10+
11+
return Boolean( postId?.includes( '//single-product' ) );
12+
}, [] );
13+
14+
return { isDescendentOfSingleProductTemplate };
15+
};

assets/js/atomic/blocks/product-elements/sku/edit.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import type { BlockEditProps } from '@wordpress/blocks';
66
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
77
import { ProductQueryContext as Context } from '@woocommerce/blocks/product-query/types';
88
import { useEffect } from '@wordpress/element';
9-
import { useSelect } from '@wordpress/data';
109

1110
/**
1211
* Internal dependencies
1312
*/
1413
import Block from './block';
1514
import type { Attributes } from './types';
1615
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';
16+
import { useIsDescendentOfSingleProductTemplate } from '../shared/use-is-descendent-of-single-product-template';
1717

1818
const Edit = ( {
1919
attributes,
@@ -32,22 +32,12 @@ const Edit = ( {
3232
const { isDescendentOfSingleProductBlock } =
3333
useIsDescendentOfSingleProductBlock( { blockClientId: blockProps.id } );
3434

35-
const isDescendentOfSingleProductTemplate = useSelect(
36-
( select ) => {
37-
const store = select( 'core/edit-site' );
38-
const postId = store?.getEditedPostId< string | undefined >();
35+
let { isDescendentOfSingleProductTemplate } =
36+
useIsDescendentOfSingleProductTemplate();
3937

40-
if ( ! postId ) {
41-
return false;
42-
}
43-
44-
return (
45-
postId.includes( '//single-product' ) &&
46-
! isDescendentOfQueryLoop
47-
);
48-
},
49-
[ isDescendentOfQueryLoop ]
50-
);
38+
if ( isDescendentOfQueryLoop ) {
39+
isDescendentOfSingleProductTemplate = false;
40+
}
5141

5242
useEffect(
5343
() =>

0 commit comments

Comments
 (0)