Skip to content

Commit

Permalink
Request shipping classes conditionally (woocommerce#47196)
Browse files Browse the repository at this point in the history
* Request shipping classes conditionally

* Add changelog

* Fix getProdcutShippingClasses return

* Refactor useSelect
  • Loading branch information
octaedro authored May 8, 2024
1 parent e7aff94 commit 9ad1321
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Request shipping classes conditionally #47196
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"lock": false,
"__experimentalToolbar": false
},
"usesContext": [ "postType" ]
"usesContext": [ "postType", "isInSelectedTab" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function extractDefaultShippingClassFromProduct(

export function Edit( {
attributes,
context,
context: { postType, isInSelectedTab },
}: ProductEditorBlockEditProps< ShippingClassBlockAttributes > ) {
const [ showShippingClassModal, setShowShippingClassModal ] =
useState( false );
Expand All @@ -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'
);

Expand All @@ -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,
Expand Down

0 comments on commit 9ad1321

Please sign in to comment.