From 4dc050fbad1ed6e8ad7cba7750c5eb7ce1c435d4 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 1 Mar 2023 09:28:15 +0000 Subject: [PATCH] Navigation: Don't create a fallback navigation menu if there are inner blocks (#48585) * Navigation: Don't create a fallback navigation menu if there are inner blocks * move const declarations higher --- .../src/navigation/edit/index.js | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/navigation/edit/index.js b/packages/block-library/src/navigation/edit/index.js index f13a7e603c83b5..7e07b0d6cde415 100644 --- a/packages/block-library/src/navigation/edit/index.js +++ b/packages/block-library/src/navigation/edit/index.js @@ -277,12 +277,24 @@ function Navigation( { hasUncontrolledInnerBlocks, ] ); + const isEntityAvailable = + ! isNavigationMenuMissing && isNavigationMenuResolved; + + // If the block has inner blocks, but no menu id, then these blocks are either: + // - inserted via a pattern. + // - inserted directly via Code View (or otherwise). + // - from an older version of navigation block added before the block used a wp_navigation entity. + // Consider this state as 'unsaved' and offer an uncontrolled version of inner blocks, + // that automatically saves the menu as an entity when changes are made to the inner blocks. + const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable; + useEffect( () => { if ( ref || ! hasResolvedNavigationMenus || isConvertingClassicMenu || - fallbackNavigationMenus?.length > 0 + fallbackNavigationMenus?.length > 0 || + hasUnsavedBlocks ) { return; } @@ -323,13 +335,14 @@ function Navigation( { if ( getBlockType( 'core/page-list' ) ) { defaultBlocks = [ createBlock( 'core/page-list' ) ]; } + createNavigationMenu( 'Navigation', // TODO - use the template slug in future defaultBlocks, 'publish' ); } - }, [ hasResolvedNavigationMenus ] ); + }, [ hasResolvedNavigationMenus, hasUnsavedBlocks ] ); const navRef = useRef(); @@ -349,9 +362,6 @@ function Navigation( { classicMenus?.length === 0 && ! hasUncontrolledInnerBlocks; - const isEntityAvailable = - ! isNavigationMenuMissing && isNavigationMenuResolved; - // "loading" state: // - there is a menu creation process in progress. // - there is a classic menu conversion process in progress. @@ -728,14 +738,6 @@ function Navigation( { ); - // If the block has inner blocks, but no menu id, then these blocks are either: - // - inserted via a pattern. - // - inserted directly via Code View (or otherwise). - // - from an older version of navigation block added before the block used a wp_navigation entity. - // Consider this state as 'unsaved' and offer an uncontrolled version of inner blocks, - // that automatically saves the menu as an entity when changes are made to the inner blocks. - const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable; - const isManageMenusButtonDisabled = ! hasManagePermissions || ! hasResolvedNavigationMenus;