Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Simplify the useSelect for useNavigationMenus #51977

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/block-library/src/navigation/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export const PRIORITIZED_INSERTER_BLOCKS = [
'core/navigation-link',
];

// These parameters must be kept aligned with those in
// lib/compat/wordpress-6.3/navigation-block-preloading.php
// and
// edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js
export const PRELOADED_NAVIGATION_MENUS_QUERY = {
per_page: 100,
status: [ 'publish', 'draft' ],
order: 'desc',
orderby: 'date',
};
getdave marked this conversation as resolved.
Show resolved Hide resolved

export const SELECT_NAVIGATION_MENUS_ARGS = [
'postType',
'wp_navigation',
{
per_page: 100,
status: [ 'publish', 'draft' ],
order: 'desc',
orderby: 'date',
},
PRELOADED_NAVIGATION_MENUS_QUERY,
];
102 changes: 39 additions & 63 deletions packages/block-library/src/navigation/use-navigation-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,61 @@
import {
store as coreStore,
useResourcePermissions,
useEntityRecords,
} from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { SELECT_NAVIGATION_MENUS_ARGS } from './constants';
import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants';

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

return useSelect(
const {
navigationMenu,
isNavigationMenuResolved,
isNavigationMenuMissing,
} = useSelect(
( select ) => {
const {
canCreate,
canUpdate,
canDelete,
isResolving,
hasResolved,
} = permissions;

const {
navigationMenus,
isResolvingNavigationMenus,
hasResolvedNavigationMenus,
} = selectNavigationMenus( select );

const {
navigationMenu,
isNavigationMenuResolved,
isNavigationMenuMissing,
} = selectExistingMenu( select, ref );

return {
navigationMenus,
isResolvingNavigationMenus,
hasResolvedNavigationMenus,

navigationMenu,
isNavigationMenuResolved,
isNavigationMenuMissing,

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

canUserCreateNavigationMenu: canCreate,
isResolvingCanUserCreateNavigationMenu: isResolving,
hasResolvedCanUserCreateNavigationMenu: hasResolved,

canUserUpdateNavigationMenu: canUpdate,
hasResolvedCanUserUpdateNavigationMenu: ref
? hasResolved
: undefined,

canUserDeleteNavigationMenu: canDelete,
hasResolvedCanUserDeleteNavigationMenu: ref
? hasResolved
: undefined,
};
return selectExistingMenu( select, ref );
},
[ ref, permissions ]
[ ref ]
);
}

function selectNavigationMenus( select ) {
const { getEntityRecords, hasFinishedResolution, isResolving } =
select( coreStore );
const { canCreate, canUpdate, canDelete, isResolving, hasResolved } =
permissions;

const {
records: navigationMenus,
isResolving: isResolvingNavigationMenus,
hasResolved: hasResolvedNavigationMenus,
} = useEntityRecords(
'postType',
`wp_navigation`,
PRELOADED_NAVIGATION_MENUS_QUERY
);

const canSwitchNavigationMenu = ref
? navigationMenus?.length > 1
: navigationMenus?.length > 0;

return {
navigationMenus: getEntityRecords( ...SELECT_NAVIGATION_MENUS_ARGS ),
isResolvingNavigationMenus: isResolving(
'getEntityRecords',
SELECT_NAVIGATION_MENUS_ARGS
),
hasResolvedNavigationMenus: hasFinishedResolution(
'getEntityRecords',
SELECT_NAVIGATION_MENUS_ARGS
),
navigationMenu,
isNavigationMenuResolved,
isNavigationMenuMissing,
navigationMenus,
isResolvingNavigationMenus,
hasResolvedNavigationMenus,
canSwitchNavigationMenu,
canUserCreateNavigationMenu: canCreate,
isResolvingCanUserCreateNavigationMenu: isResolving,
hasResolvedCanUserCreateNavigationMenu: hasResolved,
canUserUpdateNavigationMenu: canUpdate,
hasResolvedCanUserUpdateNavigationMenu: ref ? hasResolved : undefined,
canUserDeleteNavigationMenu: canDelete,
hasResolvedCanUserDeleteNavigationMenu: ref ? hasResolved : undefined,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// on apiFetch this query is limited to 100.
// These parameters must be kept aligned with those in
// lib/compat/wordpress-6.3/navigation-block-preloading.php
// and
// block-library/src/navigation/constants.js
export const PRELOADED_NAVIGATION_MENUS_QUERY = {
per_page: 100,
status: [ 'publish', 'draft' ],
Expand Down