From 03dfa59acb1765f3e1cc49305903da4e10009acd Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 28 May 2021 10:15:44 +0100 Subject: [PATCH 1/2] Reset the persisted selected menu ID if there are no longer any menus --- packages/edit-navigation/src/hooks/use-navigation-editor.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/edit-navigation/src/hooks/use-navigation-editor.js b/packages/edit-navigation/src/hooks/use-navigation-editor.js index cec78cf3a59dc2..7d7e9d15c1e5fa 100644 --- a/packages/edit-navigation/src/hooks/use-navigation-editor.js +++ b/packages/edit-navigation/src/hooks/use-navigation-editor.js @@ -47,6 +47,10 @@ export default function useNavigationEditor() { useEffect( () => { if ( hasLoadedMenus ) { setHasFinishedInitialLoad( true ); + + if ( ! menus?.length ) { + setSelectedMenuId( 0 ); + } } }, [ hasLoadedMenus ] ); From 718f0662eb948c144b8bb9d5a22daf6aabfc2936 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 28 May 2021 10:34:41 +0100 Subject: [PATCH 2/2] Test for menus and selected menu availability otherwise reset selected menu ID --- .../src/hooks/use-navigation-editor.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/edit-navigation/src/hooks/use-navigation-editor.js b/packages/edit-navigation/src/hooks/use-navigation-editor.js index 7d7e9d15c1e5fa..b5baf9a236cc66 100644 --- a/packages/edit-navigation/src/hooks/use-navigation-editor.js +++ b/packages/edit-navigation/src/hooks/use-navigation-editor.js @@ -47,13 +47,23 @@ export default function useNavigationEditor() { useEffect( () => { if ( hasLoadedMenus ) { setHasFinishedInitialLoad( true ); - - if ( ! menus?.length ) { - setSelectedMenuId( 0 ); - } } }, [ hasLoadedMenus ] ); + useEffect( () => { + // Don't reset selected menu before we can check whether + // it is still available. + if ( ! hasLoadedMenus ) { + return; + } + + const selectedMenu = menus?.find( ( { id } ) => id === selectedMenuId ); + + if ( ! menus?.length || ! selectedMenu ) { + setSelectedMenuId( 0 ); + } + }, [ hasLoadedMenus, menus, selectedMenuId ] ); + const navigationPost = useSelect( ( select ) => { if ( ! selectedMenuId ) {