Skip to content

Commit

Permalink
sync fetch-link-suggestions code
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Mar 11, 2021
1 parent b89c920 commit af813c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
74 changes: 48 additions & 26 deletions packages/edit-navigation/src/utils/fetch-link-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,35 @@ import { __ } from '@wordpress/i18n';
* @return {Promise<Object[]>} List of suggestions
*/

export default function fetchLinkSuggestions(
export default async function fetchLinkSuggestions(
search,
{ isInitialSuggestions, type, subtype } = {},
{ isInitialSuggestions, type, subtype, page, perPage: perPageArg } = {},
{ disablePostFormats = false } = {}
) {
const perPage = isInitialSuggestions ? 3 : 20;
const perPage = perPageArg || isInitialSuggestions ? 3 : 20;

const linkTypes = [ 'post', 'term', 'post-format' ];

linkTypes.forEach( ( linkType ) => {
if ( ! type || type === linkType ) {
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ); // fail by returning no results
}
} );
const queries = [];

if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ) // fail by returning no results
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'post-type', subtype },
};
} );
} )
.catch( () => [] ) // fail by returning no results
);
}

Expand All @@ -69,11 +65,21 @@ export default function fetchLinkSuggestions(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'term',
subtype,
} ),
} ).catch( () => [] )
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'taxonomy', subtype },
};
} );
} )
.catch( () => [] )
);
}

Expand All @@ -82,20 +88,36 @@ export default function fetchLinkSuggestions(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post-format',
subtype,
} ),
} ).catch( () => [] )
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'post-format', subtype },
};
} );
} )
.catch( () => [] )
);
}

return Promise.all( queries ).then( ( results ) => {
return map( flatten( results ).slice( 0, perPage ), ( result ) => ( {
id: result.id,
url: result.url,
title: decodeEntities( result.title ) || __( '(no title)' ),
type: result.subtype || result.type,
} ) );
return map(
flatten( results )
.filter( ( result ) => !! result.id )
.slice( 0, perPage ),
( result ) => ( {
id: result.id,
url: result.url,
title: decodeEntities( result.title ) || __( '(no title)' ),
type: result.subtype || result.type,
kind: result?.meta?.kind,
} )
);
} );
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { store as editorStore } from '../../store';
/**
* Fetches link suggestions from the API. This function is an exact copy of a function found at:
*
* packages/edit-navigation/src/index.js
* packages/edit-navigation/src/utils/fetch-link-suggestions
*
* 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 af813c4

Please sign in to comment.