diff --git a/resources/js/search.js b/resources/js/search.js index 6e329b248..769f0e5c3 100644 --- a/resources/js/search.js +++ b/resources/js/search.js @@ -2,29 +2,54 @@ import algoliasearch from 'algoliasearch/lite'; const client = algoliasearch(process.env.MIX_ALGOLIA_APP_ID, process.env.MIX_ALGOLIA_SECRET); -window.search = (event) => { - // If the input is empty, return no results. - if (event.target.value.length === 0) { - return Promise.resolve({ hits: [] }); - } - - // Perform the search using the provided input. - return client.multipleQueries([ - { - indexName: process.env.MIX_ALGOLIA_THREADS_INDEX, - query: event.target.value, - params: { - hitsPerPage: 5, - attributesToSnippet: ['body:10'], +window.searchConfig = () => { + return { + show: false, + threads: { + total: 0, + formattedTotal: function () { + return `${this.total} ${this.total === 1 ? 'Result' : 'Results'}`; }, + threads: [], }, - { - indexName: process.env.MIX_ALGOLIA_ARTICLES_INDEX, - query: event.target.value, - params: { - hitsPerPage: 5, - attributesToSnippet: ['body:10'], + articles: { + total: 0, + formattedTotal: function () { + return `${this.total} ${this.total === 1 ? 'Result' : 'Results'}`; }, + articles: [], + }, + search: async function (query) { + // If the input is empty, return no results. + if (query.length === 0) { + return Promise.resolve({ hits: [] }); + } + + // Perform the search using the provided input. + const { results } = await client.multipleQueries([ + { + indexName: process.env.MIX_ALGOLIA_THREADS_INDEX, + query: query, + params: { + hitsPerPage: 5, + attributesToSnippet: ['body:10'], + }, + }, + { + indexName: process.env.MIX_ALGOLIA_ARTICLES_INDEX, + query: query, + params: { + hitsPerPage: 5, + attributesToSnippet: ['body:10'], + }, + }, + ]); + + this.show = true; + this.threads.total = results[0].nbHits; + this.threads.threads = results[0].hits; + this.articles.total = results[1].nbHits; + this.articles.articles = results[1].hits; }, - ]); + }; }; diff --git a/resources/views/_partials/_search.blade.php b/resources/views/_partials/_search.blade.php index e04939797..6a457b77e 100644 --- a/resources/views/_partials/_search.blade.php +++ b/resources/views/_partials/_search.blade.php @@ -1,4 +1,4 @@ -
+
@@ -7,8 +7,9 @@
- + + + + +