Skip to content

Commit

Permalink
Improves handling of API request and adds comments
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Nov 21, 2019
1 parent 184e45a commit ad5b2da
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ import BlockColorsStyleSelector from './block-colors-selector';
function Navigation( {
attributes,
clientId,

isRequestingPages,
hasResolvedPages,
setAttributes,
hasExistingNavItems,
updateNavItemBlocks,
Expand All @@ -61,7 +58,8 @@ function Navigation( {
/* eslint-enable @wordpress/no-unused-vars-before-return */
const { navigatorToolbarButton, navigatorModal } = useBlockNavigator( clientId );

let isStillMounted = true;
let isMounted = true;
let isRequestingPages = false;

// Builds navigation links from default Pages.
const defaultPagesNavigationItems = useMemo(
Expand All @@ -87,31 +85,45 @@ function Navigation( {
);

useEffect( () => {
// Indicate the fetching status
isRequestingPages = true;

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: 'view',
context,
};

apiFetch( {
path: addQueryArgs( baseUrl, filterDefaultPages ),
} ).then(
( pagesList ) => {
if ( isStillMounted ) {
if ( isMounted ) {
setPages( pagesList );
}
// We've stopped fetching
isRequestingPages = false;
}
).catch(
() => {
if ( isStillMounted ) {
if ( isMounted ) {
setPages( [] );
}
// We've stopped fetching
isRequestingPages = false;
}
);

return () => {
isStillMounted = false;
isMounted = false;
};
}, [] );

Expand All @@ -128,7 +140,7 @@ function Navigation( {
updateNavItemBlocks( defaultPagesNavigationItems );
};

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

// If we don't have existing items or the User hasn't
// indicated they want to automatically add top level Pages
Expand All @@ -137,7 +149,7 @@ function Navigation( {
return (
<Fragment>
<InspectorControls>
{ hasResolvedPages && (
{ hasPages && (
<PanelBody
title={ __( 'Navigation Settings' ) }
>
Expand Down Expand Up @@ -234,18 +246,8 @@ 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,
isRequestingPages: select( 'core/data' ).isResolving( ...pagesSelect ),
hasResolvedPages: select( 'core/data' ).hasFinishedResolution( ...pagesSelect ),
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
Expand Down

0 comments on commit ad5b2da

Please sign in to comment.