Product template not updating in the UI when it is changed (#48288)

* Fix Product template not updating in the UI when it is changed

* Add changelog file
This commit is contained in:
Maikel Perez 2024-06-07 16:00:15 -04:00 committed by GitHub
parent 442adcc395
commit 3d0b9993be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 35 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix Product template not updating in the UI when it is changed

View File

@ -242,19 +242,36 @@ export function BlockEditor( {
( postType !== 'product_variation' && ! productTemplate ) || ( postType !== 'product_variation' && ! productTemplate ) ||
productId === -1; productId === -1;
useLayoutEffect( () => { const productFormTemplate = useMemo(
if ( isEditorLoading ) { function pickAndParseTheProductFormTemplate() {
return; if (
! isProductEditorTemplateSystemEnabled ||
! selectedProductFormId
) {
return undefined;
} }
// PFT: pick and parse the product form template, if it exists.
const productFormPost = productForms.find( const productFormPost = productForms.find(
( form ) => form.id === selectedProductFormId ( form ) => form.id === selectedProductFormId
); );
let productFormTemplate; if ( productFormPost ) {
if ( isProductEditorTemplateSystemEnabled && productFormPost ) { return parse( productFormPost.content.raw );
productFormTemplate = parse( productFormPost.content.raw ); }
return undefined;
},
[
isProductEditorTemplateSystemEnabled,
productForms,
selectedProductFormId,
]
);
useLayoutEffect(
function setupEditor() {
if ( isEditorLoading ) {
return;
} }
const blockInstances = synchronizeBlocksWithTemplate( const blockInstances = synchronizeBlocksWithTemplate(
@ -275,17 +292,22 @@ export function BlockEditor( {
productTemplate, productTemplate,
} as Partial< ProductEditorSettings > ); } as Partial< ProductEditorSettings > );
setIsEditorLoading( isEditorLoading ); // We don't need to include onChange in the dependencies, since we get new
// instances of it on every render, which would cause an infinite loop.
// We don't need to include onChange or updateEditorSettings in the dependencies,
// since we get new instances of them on every render, which would cause an infinite loop.
//
// We include productId in the dependencies to make sure that the effect is run when the
// product is changed, since we need to synchronize the blocks with the template and update
// the blocks by calling onChange.
//
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [ isEditorLoading, productId, productForms, selectedProductFormId ] ); },
[
isEditorLoading,
layoutTemplate,
settings,
productTemplate,
productFormTemplate,
]
);
useEffect( () => {
setIsEditorLoading( isEditorLoading );
}, [ isEditorLoading ] );
// Check if the Modal editor is open from the store. // Check if the Modal editor is open from the store.
const isModalEditorOpen = useSelect( ( selectCore ) => { const isModalEditorOpen = useSelect( ( selectCore ) => {

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix edit variable product test