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,50 +242,72 @@ 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 ); }
}
const blockInstances = synchronizeBlocksWithTemplate( return undefined;
[], },
layoutTemplate.blockTemplates [
); isProductEditorTemplateSystemEnabled,
productForms,
selectedProductFormId,
]
);
/* useLayoutEffect(
* If the product form template is not available, use the block instances. function setupEditor() {
* ToDo: Remove this fallback once the product form template is stable/available. if ( isEditorLoading ) {
*/ return;
const editorTemplate = productFormTemplate ?? blockInstances; }
onChange( editorTemplate, {} ); const blockInstances = synchronizeBlocksWithTemplate(
[],
layoutTemplate.blockTemplates
);
dispatch( 'core/editor' ).updateEditorSettings( { /*
...settings, * If the product form template is not available, use the block instances.
* ToDo: Remove this fallback once the product form template is stable/available.
*/
const editorTemplate = productFormTemplate ?? blockInstances;
onChange( editorTemplate, {} );
dispatch( 'core/editor' ).updateEditorSettings( {
...settings,
productTemplate,
} as Partial< ProductEditorSettings > );
// 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.
// eslint-disable-next-line react-hooks/exhaustive-deps
},
[
isEditorLoading,
layoutTemplate,
settings,
productTemplate, productTemplate,
} as Partial< ProductEditorSettings > ); productFormTemplate,
]
);
useEffect( () => {
setIsEditorLoading( isEditorLoading ); setIsEditorLoading( isEditorLoading );
}, [ isEditorLoading ] );
// 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
}, [ isEditorLoading, productId, productForms, selectedProductFormId ] );
// 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