Skip to content

Commit

Permalink
Simplify useNavigationMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed May 18, 2022
1 parent 31de2ce commit 160f5f7
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* WordPress dependencies
*/
import { store as coreStore } from '@wordpress/core-data';
import {
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function useNavigationMenu( ref ) {
return useSelect(
const entityDetails = useSelect(
( select ) => {
const {
getEntityRecord,
getEditedEntityRecord,
getEntityRecords,
hasFinishedResolution,
isResolving,
canUser,
} = select( coreStore );

const navigationMenuSingleArgs = [
Expand Down Expand Up @@ -70,31 +72,26 @@ export default function useNavigationMenu( ref ) {
),
navigationMenu,
navigationMenus,
canUserUpdateNavigationMenu: ref
? canUser( 'update', 'navigation', ref )
: undefined,
hasResolvedCanUserUpdateNavigationMenu: hasFinishedResolution(
'canUser',
[ 'update', 'navigation', ref ]
),
canUserDeleteNavigationMenu: ref
? canUser( 'delete', 'navigation', ref )
: undefined,
hasResolvedCanUserDeleteNavigationMenu: hasFinishedResolution(
'canUser',
[ 'delete', 'navigation', ref ]
),
canUserCreateNavigationMenu: canUser( 'create', 'navigation' ),
isResolvingCanUserCreateNavigationMenu: isResolving(
'canUser',
[ 'create', 'navigation' ]
),
hasResolvedCanUserCreateNavigationMenu: hasFinishedResolution(
'canUser',
[ 'create', 'navigation' ]
),
};
},
[ ref ]
);

const [
hasResolvedPermissions,
{ canCreate, canUpdate, canDelete, isResolving },
] = useResourcePermissions( 'navigation', ref );

return {
...entityDetails,
canUserCreateNavigationMenu: canCreate,
hasResolvedCanUserCreateNavigationMenu: hasResolvedPermissions,
isResolvingCanUserCreateNavigationMenu: isResolving,

canUserDeleteNavigationMenu: canDelete,
hasResolvedCanUserDeleteNavigationMenu: hasResolvedPermissions,

canUserUpdateNavigationMenu: canUpdate,
hasResolvedCanUserUpdateNavigationMenu: hasResolvedPermissions,
};
}

0 comments on commit 160f5f7

Please sign in to comment.