Skip to content

Commit

Permalink
Navigation: Update fetchLinkSuggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Aug 21, 2020
1 parent 64142c4 commit b4a1023
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
77 changes: 50 additions & 27 deletions packages/edit-navigation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,72 @@ function disableInsertingNonNavigationBlocks( settings, name ) {
/**
* Fetches link suggestions from the API. This function is an exact copy of a function found at:
*
* wordpress/editor/src/components/provider/index.js
* packages/editor/src/components/provider/index.js
*
* It seems like there is no suitable package to import this from. Ideally it would be either part of core-data.
* Until we refactor it, just copying the code is the simplest solution.
*
* @param {Object} search
* @param {number} perPage
* @param {string} search
* @param {Object} query
* @param {boolean} [query.isInitialSuggestions]
* @param {string} [query.type='post']
* @param {string} [query.subtype]
* @return {Promise<Object[]>} List of suggestions
*/
async function fetchLinkSuggestions( search, { perPage = 20 } = {} ) {
const posts = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
} ),
} );
const fetchLinkSuggestions = async (
search,
{ isInitialSuggestions, type, subtype } = {}
) => {
const queries = [];

const terms = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'term',
} ),
} );
if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: isInitialSuggestions ? 3 : 20,
type: 'post',
subtype,
} ),
} )
);
}

const formats = apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post-format',
} ),
} );
if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: isInitialSuggestions ? 3 : 20,
type: 'term',
subtype,
} ),
} )
);
}

return Promise.all( [ posts, terms, formats ] ).then( ( results ) => {
if ( ! type || type === 'post-format' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: isInitialSuggestions ? 3 : 20,
type: 'post-format',
subtype,
} ),
} )
);
}

return Promise.all( queries ).then( ( results ) => {
return map( flatten( results ), ( post ) => ( {
id: post.id,
url: post.url,
title: decodeEntities( post.title ) || __( '(no title)' ),
type: post.subtype || post.type,
} ) );
} );
}
};

export function initialize( id, settings ) {
if ( ! settings.blockNavMenus ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import ConvertToGroupButtons from '../convert-to-group-buttons';
/**
* Fetches link suggestions from the API. This function is an exact copy of a function found at:
*
* wordpress/editor/src/components/provider/index.js
* packages/edit-navigation/src/index.js
*
* It seems like there is no suitable package to import this from. Ideally it would be either part of core-data.
* Until we refactor it, just copying the code is the simplest solution.
Expand Down

0 comments on commit b4a1023

Please sign in to comment.