Skip to content

Commit

Permalink
Navigation: Don't create a fallback navigation menu if there are inne…
Browse files Browse the repository at this point in the history
…r blocks (#48585)

* Navigation: Don't create a fallback navigation menu if there are inner blocks

* move const declarations higher
  • Loading branch information
scruffian authored Mar 1, 2023
1 parent 65794bf commit 4dc050f
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand All @@ -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.
Expand Down Expand Up @@ -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;

Expand Down

1 comment on commit 4dc050f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 4dc050f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4302025088
📝 Reported issues:

Please sign in to comment.