Skip to content

Commit

Permalink
Revert "Fix Nav Block bug as Contributor user via fix to `*loadPostTy…
Browse files Browse the repository at this point in the history
…peEntities` (#18669)"

This reverts commit f2b6778.

# Conflicts:
#	package-lock.json
#	packages/api-fetch/package.json
#	packages/block-library/src/navigation/edit.js
  • Loading branch information
getdave committed Apr 17, 2020
1 parent 63f708a commit d16fe43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 64 deletions.
39 changes: 0 additions & 39 deletions packages/api-fetch/src/index.js
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -157,42 +156,6 @@ function apiFetch( options ) {
} );
}

/**
* Function that fetches data using apiFetch, and updates the status.
*
* @param {string} path Query path.
*/
function useApiFetch( path ) {
// Indicate the fetching status
const [ isLoading, setIsLoading ] = useState( true );
const [ data, setData ] = useState( null );
const [ error, setError ] = useState( null );

useEffect( () => {
setIsLoading( true );
setData( null );
setError( null );

apiFetch( { path } )
.then( ( fetchedData ) => {
setData( fetchedData );
// We've stopped fetching
setIsLoading( false );
} )
.catch( ( err ) => {
setError( err );
// We've stopped fetching
setIsLoading( false );
} );
}, [ path ] );

return {
isLoading,
data,
error,
};
}

apiFetch.use = registerMiddleware;
apiFetch.setFetchHandler = setFetchHandler;

Expand All @@ -202,6 +165,4 @@ apiFetch.createRootURLMiddleware = createRootURLMiddleware;
apiFetch.fetchAllMiddleware = fetchAllMiddleware;
apiFetch.mediaUploadMiddleware = mediaUploadMiddleware;

apiFetch.useApiFetch = useApiFetch;

export default apiFetch;
57 changes: 32 additions & 25 deletions packages/block-library/src/navigation/edit.js
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
//
/* eslint-disable @wordpress/no-unused-vars-before-return */
const ref = useRef();
const { selectBlock } = useDispatch( 'core/block-editor' );
const { TextColor, BackgroundColor, ColorPanel } = __experimentalUseColors(
Expand All @@ -78,35 +81,14 @@ function Navigation( {
[ fontSize.size ]
);

/* eslint-enable @wordpress/no-unused-vars-before-return */
const { navigatorToolbarButton, navigatorModal } = useBlockNavigator(
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 +128,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 +291,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 d16fe43

Please sign in to comment.