Skip to content

Commit

Permalink
Switching area pull in correct menu
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Oct 28, 2021
1 parent 6fa4577 commit 5e1a894
Showing 1 changed file with 51 additions and 42 deletions.
93 changes: 51 additions & 42 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ function Navigation( {
const hasExistingNavItems = !! innerBlocks.length;
const { selectBlock } = useDispatch( blockEditorStore );

/* eslint-disable no-unused-vars */
const { editEntityRecord, saveEditedEntityRecord } = useDispatch(
coreStore
);
/* eslint-enable no-unused-vars */

const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
Expand All @@ -134,8 +136,20 @@ function Navigation( {
isNavigationMenuMissing,
canSwitchNavigationMenu,
hasResolvedNavigationMenu,
navigationMenu,
} = useNavigationMenu( navigationMenuId, navigationAreaId );

useEffect( () => {
// If there is a area ID then the retrieved menu will be
// that which is associated with the area. Update the reference
// on the block to mirror that.
if ( navigationAreaId ) {
setAttributes( {
navigationMenuId: navigationMenu?.id,
} );
}
}, [ navigationMenu?.id ] );

const {
navigationAreas,
isRequestingAreas,
Expand Down Expand Up @@ -214,34 +228,31 @@ function Navigation( {
}, [ isEntityAvailable ] );

useEffect( () => {
const updateNavEntityArea = async () => {
const noTerms = [];

// navigationAreaId is of type "number". Therefore
// ID: 0 is used to represent a value of "none".
const maybeNewAreaTermId =
navigationAreaId > 0 ? [ navigationAreaId ] : noTerms;

// Toggle the active navigaiton area term.
await editEntityRecord(
'postType',
'wp_navigation',
navigationMenuId,
{
wp_navigation_area: maybeNewAreaTermId,
}
);

// Persist the change.
await saveEditedEntityRecord(
'postType',
'wp_navigation',
navigationMenuId
);
};
if ( isEntityAvailable ) {
updateNavEntityArea( navigationAreaId );
}
// const updateNavEntityArea = async () => {
// const noTerms = [];
// // navigationAreaId is of type "number". Therefore
// // ID: 0 is used to represent a value of "none".
// const maybeNewAreaTermId =
// navigationAreaId > 0 ? [ navigationAreaId ] : noTerms;
// // Toggle the active navigaiton area term.
// await editEntityRecord(
// 'postType',
// 'wp_navigation',
// navigationMenuId,
// {
// wp_navigation_area: maybeNewAreaTermId,
// }
// );
// // Persist the change.
// await saveEditedEntityRecord(
// 'postType',
// 'wp_navigation',
// navigationMenuId
// );
// };
// if ( isEntityAvailable ) {
// updateNavEntityArea( navigationAreaId );
// }
}, [ navigationAreaId ] );

// If the block has inner blocks, but no menu id, this was an older
Expand Down Expand Up @@ -363,20 +374,18 @@ function Navigation( {
</PanelBody>
) }

{ !! hasResolvedAreas && (
<PanelBody title={ __( 'Navigation Area' ) }>
<NavigationAreaSelector
navigationAreaId={ navigationAreaId }
navigationAreas={ navigationAreas }
isRequestingAreas={ isRequestingAreas }
onSelect={ ( _id ) => {
setAttributes( {
navigationAreaId: _id,
} );
} }
/>
</PanelBody>
) }
<PanelBody title={ __( 'Navigation Area' ) }>
<NavigationAreaSelector
navigationAreaId={ navigationAreaId }
navigationAreas={ navigationAreas }
isRequestingAreas={ isRequestingAreas }
onSelect={ ( _id ) => {
setAttributes( {
navigationAreaId: _id,
} );
} }
/>
</PanelBody>

{ hasSubmenuIndicatorSetting && (
<PanelBody title={ __( 'Display' ) }>
Expand Down

0 comments on commit 5e1a894

Please sign in to comment.