Product Block Editor: load and create shipping classes optimistically (#47848)

* define ans use shippingClassRequestQuery

* do not invalidate getProductShippingClasses

* changelog
This commit is contained in:
Damián Suárez 2024-05-29 19:59:24 +01:00 committed by GitHub
parent 8cdd549038
commit 6f8bce8bb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 8 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Product Block Editor: load and create shipping classes optimistically

View File

@ -51,6 +51,11 @@ function mapShippingClassToSelectOption(
} ) );
}
/*
* Query to fetch shipping classes.
*/
const shippingClassRequestQuery = {};
function extractDefaultShippingClassFromProduct(
categories?: PartialProduct[ 'categories' ],
shippingClasses?: ProductShippingClass[]
@ -78,7 +83,7 @@ export function Edit( {
const blockProps = useWooBlockProps( attributes );
const { createProductShippingClass, invalidateResolution } = useDispatch(
const { createProductShippingClass } = useDispatch(
EXPERIMENTAL_PRODUCT_SHIPPING_CLASSES_STORE_NAME
);
@ -130,9 +135,9 @@ export function Edit( {
return {
shippingClasses:
( isInSelectedTab &&
getProductShippingClasses<
ProductShippingClass[]
>() ) ||
getProductShippingClasses< ProductShippingClass[] >(
shippingClassRequestQuery
) ) ||
[],
};
},
@ -214,14 +219,13 @@ export function Edit( {
onAdd={ ( shippingClassValues ) =>
createProductShippingClass<
Promise< ProductShippingClass >
>( shippingClassValues )
>( shippingClassValues, {
optimisticQueryUpdate: shippingClassRequestQuery,
} )
.then( ( value ) => {
recordEvent(
'product_new_shipping_class_modal_add_button_click'
);
invalidateResolution(
'getProductShippingClasses'
);
setShippingClass( value.slug );
return value;
} )