Skip to content

Commit

Permalink
Conditionally trigger fallback creation if no menus are found
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jun 23, 2023
1 parent c807b20 commit 1b22268
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 1b22268

Please sign in to comment.