diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 6437b759976901..32baa691ddd822 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -585,10 +585,14 @@ export const getNavigationFallbackId = dispatch.receiveNavigationFallbackId( fallback?.id ); if ( record ) { + const invalidateNavigationQueries = true; + dispatch.receiveEntityRecords( 'postType', 'wp_navigation', - record + record, + undefined, + invalidateNavigationQueries ); // Resolve to avoid further network requests. diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js index 2234f1e1bbdeea..099612151f7747 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/index.js @@ -2,7 +2,8 @@ * WordPress dependencies */ import { __, sprintf } from '@wordpress/i18n'; -import { useEntityRecords } from '@wordpress/core-data'; +import { useEntityRecords, store as coreStore } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { @@ -41,15 +42,35 @@ function buildMenuLabel( title, id, status ) { } export default function SidebarNavigationScreenNavigationMenus() { - const { records: navigationMenus, isResolving: isLoading } = - useEntityRecords( - 'postType', - `wp_navigation`, - PRELOADED_NAVIGATION_MENUS_QUERY - ); + const { + records: navigationMenus, + isResolving: isResolvingNavigationMenus, + hasResolved: hasResolvedNavigationMenus, + } = useEntityRecords( + 'postType', + `wp_navigation`, + PRELOADED_NAVIGATION_MENUS_QUERY + ); + + const isLoading = + isResolvingNavigationMenus && ! hasResolvedNavigationMenus; + + const getNavigationFallbackId = useSelect( + ( select ) => select( coreStore ).getNavigationFallbackId + ); const firstNavigationMenu = navigationMenus?.[ 0 ]; + // If there is no navigation menu found + // then trigger fallback algorithm to create one. + if ( + ! firstNavigationMenu && + ! isResolvingNavigationMenus && + hasResolvedNavigationMenus + ) { + getNavigationFallbackId(); + } + const { handleSave, handleDelete, handleDuplicate } = useNavigationMenuHandlers();