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 authored and tellthemachines committed Mar 24, 2020
1 parent be554b6 commit 1c47636
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function Navigation({
clientId
);

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

// Builds navigation links from default Pages.
const defaultPagesNavigationItems = useMemo(() => {
Expand All @@ -125,29 +126,43 @@ function Navigation({
}, [pages]);

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 @@ -174,7 +189,7 @@ function Navigation({
selectBlock(clientId);
}

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

const blockClassNames = classnames(className, {
[`items-justified-${attributes.itemsJustification}`]: attributes.itemsJustification,
Expand All @@ -190,6 +205,23 @@ function Navigation({
if (!hasExistingNavItems) {
return (
<Block.div>
<InspectorControls>
{hasPages && (
<PanelBody title={__('Navigation Settings')}>
<CheckboxControl
value={attributes.automaticallyAdd}
onChange={(automaticallyAdd) => {
setAttributes({ automaticallyAdd });
handleCreateFromExistingPages();
}}
label={__('Automatically add new pages')}
help={__(
'Automatically add new top level pages to this navigation.'
)}
/>
</PanelBody>
)}
</InspectorControls>
<Placeholder
className="wp-block-navigation-placeholder"
icon={menu}
Expand Down Expand Up @@ -330,29 +362,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,
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 1c47636

Please sign in to comment.