Skip to content

Commit

Permalink
limit initial suggestions to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Sep 19, 2023
1 parent 47f56bc commit 1866bb0
Showing 1 changed file with 90 additions and 67 deletions.
157 changes: 90 additions & 67 deletions packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ const fetchLinkSuggestions = async (
type = undefined,
subtype = undefined,
page = undefined,
perPage = isInitialSuggestions ? 3 : 20,
perPage = 20,
} = searchOptions;

const { disablePostFormats = false } = settings;

/** @type {Promise<WPLinkSearchResult>[]} */
const queries = [];

if ( ! type || type === 'post' ) {
if ( isInitialSuggestions ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post',
subtype,
subtype: 'page',
} ),
} )
.then( ( results ) => {
Expand All @@ -117,73 +117,96 @@ const fetchLinkSuggestions = async (
} )
.catch( () => [] ) // Fail by returning no results.
);
}

if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'term',
subtype,
} ),
} )
.then( ( results ) => {
return results.map( ( result ) => {
return {
...result,
meta: { kind: 'taxonomy', subtype },
};
} );
} else {
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.
);
}

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

if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'term',
subtype,
} ),
} )
.catch( () => [] ) // Fail by returning no results.
);
}

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

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

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

return Promise.all( queries ).then( ( results ) => {
Expand Down

0 comments on commit 1866bb0

Please sign in to comment.