Skip to content

Commit

Permalink
Revert changes to Nav Block data fetching mechanics (#21721)
Browse files Browse the repository at this point in the history
* Partially Revert "Fix Nav Block bug as Contributor user via fix to `*loadPostTypeEntities` (#18669)"

This partially reverts commit f2b6778.

Only reverts the changes to utilise useApiFetch Hook.

* Remove unneeded eslint disable rule
  • Loading branch information
getdave committed Apr 21, 2020
1 parent 6c01837 commit 8885eec
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { menu } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';
import { useApiFetch } from '@wordpress/api-fetch';

/**
* Internal dependencies
*/
Expand All @@ -47,6 +46,9 @@ function Navigation( {
clientId,
fontSize,
hasExistingNavItems,
hasResolvedPages,
isRequestingPages,
pages,
setAttributes,
setFontSize,
updateNavItemBlocks,
Expand All @@ -55,6 +57,7 @@ function Navigation( {
//
// HOOKS
//

const ref = useRef();
const { selectBlock } = useDispatch( 'core/block-editor' );
const { TextColor, BackgroundColor, ColorPanel } = __experimentalUseColors(
Expand Down Expand Up @@ -82,31 +85,9 @@ function Navigation( {
clientId
);

const baseUrl = '/wp/v2/pages';

// "view" is required to ensure Pages are returned by REST API
// for users with lower capabilities such as "Contributor" otherwise
// Pages are not returned in the request if "edit" context is set
const context = 'view';

const filterDefaultPages = {
parent: 0,
order: 'asc',
orderby: 'id',
context,
};

const queryPath = addQueryArgs( baseUrl, filterDefaultPages );

const { isLoading: isRequestingPages, data: pages } = useApiFetch(
queryPath
);

const hasPages = !! pages;

// Builds navigation links from default Pages.
const defaultPagesNavigationItems = useMemo( () => {
if ( ! hasPages ) {
if ( ! pages ) {
return null;
}

Expand Down Expand Up @@ -146,6 +127,8 @@ function Navigation( {
selectBlock( clientId );
}

const hasPages = hasResolvedPages && pages && pages.length;

const blockInlineStyles = {
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
};
Expand Down Expand Up @@ -307,8 +290,31 @@ export default compose( [
withSelect( ( select, { clientId } ) => {
const innerBlocks = select( 'core/block-editor' ).getBlocks( clientId );

const filterDefaultPages = {
parent: 0,
order: 'asc',
orderby: 'id',
};

const pagesSelect = [
'core',
'getEntityRecords',
[ 'postType', 'page', filterDefaultPages ],
];

return {
hasExistingNavItems: !! innerBlocks.length,
pages: select( 'core' ).getEntityRecords(
'postType',
'page',
filterDefaultPages
),
isRequestingPages: select( 'core/data' ).isResolving(
...pagesSelect
),
hasResolvedPages: select( 'core/data' ).hasFinishedResolution(
...pagesSelect
),
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
Expand Down

0 comments on commit 8885eec

Please sign in to comment.