From 9ad1321c66822bac312c66a9c3da68176266bf36 Mon Sep 17 00:00:00 2001 From: Fernando Marichal Date: Wed, 8 May 2024 08:19:32 -0300 Subject: [PATCH] Request shipping classes conditionally (#47196) * Request shipping classes conditionally * Add changelog * Fix getProdcutShippingClasses return * Refactor useSelect --- ...195_request_shipping_classes_conditionally | 4 +++ .../product-fields/shipping-class/block.json | 2 +- .../product-fields/shipping-class/edit.tsx | 33 +++++++++++-------- 3 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 packages/js/product-editor/changelog/dev-47195_request_shipping_classes_conditionally diff --git a/packages/js/product-editor/changelog/dev-47195_request_shipping_classes_conditionally b/packages/js/product-editor/changelog/dev-47195_request_shipping_classes_conditionally new file mode 100644 index 0000000000000..a9a6625989e69 --- /dev/null +++ b/packages/js/product-editor/changelog/dev-47195_request_shipping_classes_conditionally @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Request shipping classes conditionally #47196 diff --git a/packages/js/product-editor/src/blocks/product-fields/shipping-class/block.json b/packages/js/product-editor/src/blocks/product-fields/shipping-class/block.json index a8677d06e88ab..3ff8fcaccd577 100644 --- a/packages/js/product-editor/src/blocks/product-fields/shipping-class/block.json +++ b/packages/js/product-editor/src/blocks/product-fields/shipping-class/block.json @@ -26,5 +26,5 @@ "lock": false, "__experimentalToolbar": false }, - "usesContext": [ "postType" ] + "usesContext": [ "postType", "isInSelectedTab" ] } diff --git a/packages/js/product-editor/src/blocks/product-fields/shipping-class/edit.tsx b/packages/js/product-editor/src/blocks/product-fields/shipping-class/edit.tsx index d35bdd56d5b74..e341242fee1d1 100644 --- a/packages/js/product-editor/src/blocks/product-fields/shipping-class/edit.tsx +++ b/packages/js/product-editor/src/blocks/product-fields/shipping-class/edit.tsx @@ -71,7 +71,7 @@ function extractDefaultShippingClassFromProduct( export function Edit( { attributes, - context, + context: { postType, isInSelectedTab }, }: ProductEditorBlockEditProps< ShippingClassBlockAttributes > ) { const [ showShippingClassModal, setShowShippingClassModal ] = useState( false ); @@ -86,17 +86,17 @@ export function Edit( { const [ categories ] = useEntityProp< PartialProduct[ 'categories' ] >( 'postType', - context.postType, + postType, 'categories' ); const [ shippingClass, setShippingClass ] = useEntityProp< string >( 'postType', - context.postType, + postType, 'shipping_class' ); const [ virtual ] = useEntityProp< boolean >( 'postType', - context.postType, + postType, 'virtual' ); @@ -122,15 +122,22 @@ export function Edit( { throw error; } - const { shippingClasses } = useSelect( ( select ) => { - const { getProductShippingClasses } = select( - EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME - ); - return { - shippingClasses: - getProductShippingClasses< ProductShippingClass[] >() ?? [], - }; - }, [] ); + const { shippingClasses } = useSelect( + ( select ) => { + const { getProductShippingClasses } = select( + EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME + ); + return { + shippingClasses: + ( isInSelectedTab && + getProductShippingClasses< + ProductShippingClass[] + >() ) || + [], + }; + }, + [ isInSelectedTab ] + ); const shippingClassControlId = useInstanceId( BaseControl,