Skip to content

Commit

Permalink
UX to allow creating new menu if associated Nav post not found
Browse files Browse the repository at this point in the history
Previously you could get into an error state if the Nav block referenced a Nav Post which had been deleted. You could not recover from this as the UI provided no way to "start over". This addition affords that ability.
  • Loading branch information
getdave authored and adamziel committed Nov 24, 2021
1 parent 1263ad3 commit 4808aaf
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions packages/block-library/src/navigation/edit/index.js
Expand Up @@ -34,6 +34,7 @@ import {
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
ToolbarGroup,
ToolbarDropdownMenu,
Button,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

Expand Down Expand Up @@ -280,6 +281,17 @@ function Navigation( {
setIsPlaceholderShown( ! isEntityAvailable );
}, [ isEntityAvailable ] );

function createNewMenu() {
replaceInnerBlocks( clientId, [] );
if ( navigationArea ) {
setAreaMenu( 0 );
}
setAttributes( {
navigationMenuId: undefined,
} );
setIsPlaceholderShown( true );
}

// If the block has inner blocks, but no menu id, this was an older
// navigation block added before the block used a wp_navigation entity.
// Either this block was saved in the content or inserted by a pattern.
Expand All @@ -305,6 +317,23 @@ function Navigation( {
);
}

// Show a warning if the selected menu is no longer available.
// TODO - the user should be able to select a new one?
if ( navigationMenuId && isNavigationMenuMissing ) {
return (
<div { ...blockProps }>
<Warning>
{ __(
'Navigation menu has been deleted or is unavailable. '
) }
<Button onClick={ createNewMenu } variant="link">
{ __( 'Create a new menu?' ) }
</Button>
</Warning>
</div>
);
}

if ( isEntityAvailable && hasAlreadyRendered ) {
return (
<div { ...blockProps }>
Expand Down Expand Up @@ -340,16 +369,7 @@ function Navigation( {
setNavigationMenuId( id );
onClose();
} }
onCreateNew={ () => {
replaceInnerBlocks( clientId, [] );
if ( navigationArea ) {
setAreaMenu( 0 );
}
setAttributes( {
navigationMenuId: undefined,
} );
setIsPlaceholderShown( true );
} }
onCreateNew={ createNewMenu }
/>
) }
</ToolbarDropdownMenu>
Expand Down

0 comments on commit 4808aaf

Please sign in to comment.