Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset the persisted selected menu ID if there are no longer any menus #32306

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/edit-navigation/src/hooks/use-navigation-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ export default function useNavigationEditor() {
}
}, [ 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 );
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like null might better represent the absence of something. I know WordPress likes to count from 1, but 0 still seems too much like a legit menu id.

Copy link
Contributor Author

@getdave getdave May 28, 2021

Choose a reason for hiding this comment

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

I tend to agree. I was basing it on the default state:

export function selectedMenuId( state = 0, action ) {

But I guess if we pass null then it will reset to 0 anyway.

Alternatively, we could create a new action CLEAR_SELECTED_MENU_ID which might make it more explicit about what the intention is.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, 404 originates from the Header component when it tried to fetch menuName. This code won't prevent that but will fix the broken screen issue.

We currently don't have a way to reset the selected menu ID if menus were deleted by any other means, e.g., WP CLI.

I agree that changing the default/cleared state to be null makes more sense now. I think @getdave mentioned this when reviewing original PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have an alternative PR that we also might want to consider. It's incoming...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternative approach available #32313

}
}, [ hasLoadedMenus, menus, selectedMenuId ] );

const navigationPost = useSelect(
( select ) => {
if ( ! selectedMenuId ) {
Expand Down