Skip to content

Commit

Permalink
Fix bug with destructuring a potentially null property
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave authored and Alex Lende committed Mar 3, 2023
1 parent 1f01231 commit 97006f2
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ function Navigation( {
isError: createNavigationMenuIsError,
} = useCreateNavigationMenu( clientId );

const { id: createNavigationMenuPostId } = createNavigationMenuPost;

const createUntitledEmptyNavigationMenu = () => {
createNavigationMenu( '' );
};
Expand Down Expand Up @@ -215,6 +213,17 @@ function Navigation( {
[ navigationMenus ]
);

const handleUpdateMenu = useCallback(
( menuId, options = { focusNavigationBlock: false } ) => {
const { focusNavigationBlock } = options;
setRef( menuId );
if ( focusNavigationBlock ) {
selectBlock( clientId );
}
},
[ selectBlock, clientId, setRef ]
);

// This useEffect adds snackbar and speak status notices when menus are created.
// If there are no fallback navigation menus then we don't show these messages,
// because this means that we are creating the first, fallback navigation menu.
Expand All @@ -226,7 +235,7 @@ function Navigation( {
}

if ( createNavigationMenuIsSuccess ) {
handleUpdateMenu( createNavigationMenuPostId, {
handleUpdateMenu( createNavigationMenuPost?.id, {
focusNavigationBlock: true,
} );

Expand All @@ -249,7 +258,7 @@ function Navigation( {
hideNavigationMenuStatusNotice,
isCreatingNavigationMenu,
showNavigationMenuStatusNotice,
createNavigationMenuPostId,
createNavigationMenuPost?.id,
fallbackNavigationMenus,
] );

Expand Down Expand Up @@ -433,17 +442,6 @@ function Navigation( {
] = useState();
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

const handleUpdateMenu = useCallback(
( menuId, options = { focusNavigationBlock: false } ) => {
const { focusNavigationBlock } = options;
setRef( menuId );
if ( focusNavigationBlock ) {
selectBlock( clientId );
}
},
[ selectBlock, clientId, setRef ]
);

const onSelectClassicMenu = async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
Expand All @@ -469,7 +467,7 @@ function Navigation( {
}

if ( createNavigationMenuIsSuccess ) {
handleUpdateMenu( createNavigationMenuPostId, {
handleUpdateMenu( createNavigationMenuPost?.id, {
focusNavigationBlock: true,
} );

Expand All @@ -486,7 +484,7 @@ function Navigation( {
}, [
createNavigationMenuStatus,
createNavigationMenuError,
createNavigationMenuPostId,
createNavigationMenuPost?.id,
createNavigationMenuIsError,
createNavigationMenuIsSuccess,
isCreatingNavigationMenu,
Expand Down

0 comments on commit 97006f2

Please sign in to comment.