From bedbd1f154e4bcc3a74b61b9ff88e6a46ee64965 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Tue, 19 Sep 2023 22:55:25 +0300 Subject: [PATCH] limit initial suggestions to pages --- .../__experimental-fetch-link-suggestions.js | 157 ++++++++++-------- 1 file changed, 90 insertions(+), 67 deletions(-) diff --git a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js index bb1bf41d9e2cd..ec0cafb671c45 100644 --- a/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js +++ b/packages/core-data/src/fetch/__experimental-fetch-link-suggestions.js @@ -88,7 +88,7 @@ const fetchLinkSuggestions = async ( type = undefined, subtype = undefined, page = undefined, - perPage = isInitialSuggestions ? 3 : 20, + perPage = 20, } = searchOptions; const { disablePostFormats = false } = settings; @@ -96,7 +96,7 @@ const fetchLinkSuggestions = async ( /** @type {Promise[]} */ const queries = []; - if ( ! type || type === 'post' ) { + if ( isInitialSuggestions ) { queries.push( apiFetch( { path: addQueryArgs( '/wp/v2/search', { @@ -104,7 +104,7 @@ const fetchLinkSuggestions = async ( page, per_page: perPage, type: 'post', - subtype, + subtype: 'page', } ), } ) .then( ( results ) => { @@ -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 ) => {