Skip to content

Commit

Permalink
Migrate useNavigationMenu to useResourcePermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jun 15, 2022
1 parent c250798 commit c1914d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 84 deletions.
105 changes: 22 additions & 83 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
/**
* WordPress dependencies
*/
import { store as coreStore } from '@wordpress/core-data';
import {
store as coreStore,
__experimentalUseResourcePermissions as useResourcePermissions,
} from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

export default function useNavigationMenu( ref ) {
const permissions = useResourcePermissions( 'navigation', ref );

return useSelect(
( select ) => {
const [
hasResolvedPermissions,
{ canCreate, canUpdate, canDelete, isResolving },
] = permissions;

const {
navigationMenus,
isResolvingNavigationMenus,
Expand All @@ -19,22 +29,6 @@ export default function useNavigationMenu( ref ) {
isNavigationMenuMissing,
} = selectExistingMenu( select, ref );

const {
canUserCreateNavigationMenu,
isResolvingCanUserCreateNavigationMenu,
hasResolvedCanUserCreateNavigationMenu,
} = selectMenuCreatePermissions( select );

const {
canUserUpdateNavigationMenu,
hasResolvedCanUserUpdateNavigationMenu,
} = selectMenuUpdatePermissions( select, ref );

const {
canUserDeleteNavigationMenu,
hasResolvedCanUserDeleteNavigationMenu,
} = selectMenuDeletePermissions( select, ref );

return {
navigationMenus,
isResolvingNavigationMenus,
Expand All @@ -44,22 +38,22 @@ export default function useNavigationMenu( ref ) {
isNavigationMenuResolved,
isNavigationMenuMissing,

canUserCreateNavigationMenu,
isResolvingCanUserCreateNavigationMenu,
hasResolvedCanUserCreateNavigationMenu,

canUserUpdateNavigationMenu,
hasResolvedCanUserUpdateNavigationMenu,

canUserDeleteNavigationMenu,
hasResolvedCanUserDeleteNavigationMenu,

canSwitchNavigationMenu: ref
? navigationMenus?.length > 1
: navigationMenus?.length > 0,

canUserCreateNavigationMenu: canCreate,
isResolvingCanUserCreateNavigationMenu: isResolving,
hasResolvedCanUserCreateNavigationMenu: hasResolvedPermissions,

canUserUpdateNavigationMenu: canUpdate,
hasResolvedCanUserUpdateNavigationMenu: hasResolvedPermissions,

canUserDeleteNavigationMenu: canDelete,
hasResolvedCanUserDeleteNavigationMenu: hasResolvedPermissions,
};
},
[ ref ]
[ ref, permissions ]
);
}

Expand Down Expand Up @@ -113,58 +107,3 @@ function selectExistingMenu( select, ref ) {
: null,
};
}

function selectMenuCreatePermissions( select ) {
const { hasFinishedResolution, isResolving, canUser } = select( coreStore );

const args = [ 'create', 'navigation' ];
return {
canUserCreateNavigationMenu: !! canUser( ...args ),
isResolvingCanUserCreateNavigationMenu: !! isResolving(
'canUser',
args
),
hasResolvedCanUserCreateNavigationMenu: !! hasFinishedResolution(
'canUser',
args
),
};
}

function selectMenuUpdatePermissions( select, ref ) {
if ( ! ref ) {
return {
canUserUpdateNavigationMenu: false,
hasResolvedCanUserUpdateNavigationMenu: false,
};
}

const { hasFinishedResolution, canUser } = select( coreStore );
const args = [ 'update', 'navigation', ref ];
return {
canUserUpdateNavigationMenu: !! canUser( ...args ),
hasResolvedCanUserUpdateNavigationMenu: !! hasFinishedResolution(
'canUser',
args
),
};
}

function selectMenuDeletePermissions( select, ref ) {
if ( ! ref ) {
return {
canUserDeleteNavigationMenu: false,
hasResolvedCanUserDeleteNavigationMenu: false,
};
}

const { hasFinishedResolution, canUser } = select( coreStore );
const args = [ 'delete', 'navigation', ref ];
return {
canUserDeleteNavigationMenu: !! canUser( ...args ),
hasResolvedCanUserDeleteNavigationMenu: !! hasFinishedResolution(
'canUser',
args
),
};
}
2 changes: 1 addition & 1 deletion packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ register( store );
export { default as EntityProvider } from './entity-provider';
export { default as useEntityRecord } from './hooks/use-entity-record';
export { default as useEntityRecords } from './hooks/use-entity-records';
export { default as useResourcePermissions } from './hooks/use-resource-permissions';
export { default as __experimentalUseResourcePermissions } from './hooks/use-resource-permissions';
export * from './entity-provider';
export * from './entity-types';
export * from './fetch';
Expand Down

0 comments on commit c1914d2

Please sign in to comment.