@@ -13,16 +13,13 @@ import { SearchHits } from './SearchHits';
1313
1414import TypesenseInstantsearchAdapter from "typesense-instantsearch-adapter" ;
1515
16- // window.$ = $;
17-
18- /* Algolia Search Bar */
1916const ClickOutHandler = require ( 'react-onclickout' ) ;
2017
2118// Create the Typesense InstantSearch Adapter instance
2219
2320// @ts -ignore
24- console . log ( process . env . TYPESENSE_SEARCH_API_KEY ) ;
25- console . log ( process . env . TYPESENSE_HOST ) ;
21+ // console.log(process.env.TYPESENSE_SEARCH_API_KEY);
22+ // console.log(process.env.TYPESENSE_HOST);
2623const typesenseInstantsearchAdapter = new TypesenseInstantsearchAdapter ( {
2724 server : {
2825 apiKey : process . env . TYPESENSE_SEARCH_API_KEY , // Use your Typesense search-only API key here
@@ -43,6 +40,16 @@ const typesenseInstantsearchAdapter = new TypesenseInstantsearchAdapter({
4340
4441export const searchClient = typesenseInstantsearchAdapter . searchClient ;
4542
43+ const debounce = ( func , delay ) => {
44+ let timer ;
45+ return function ( ...args ) {
46+ clearTimeout ( timer ) ;
47+ timer = setTimeout ( ( ) => {
48+ func . apply ( this , args ) ;
49+ } , delay ) ;
50+ } ;
51+ } ;
52+
4653class SearchInputBox extends React . Component {
4754 constructor ( props ) {
4855 super ( props ) ;
@@ -52,29 +59,47 @@ class SearchInputBox extends React.Component {
5259 cookie : '' ,
5360 hasInput : false ,
5461 refresh : false ,
62+ searchQuery : ''
5563 } ;
64+
65+ this . debouncedSearch = debounce ( this . handleSearch , 300 ) ;
5666 }
5767
58- // Algolia - clicking out exits searchbox
5968 onClickOut = ( event ) => {
6069 const searchInput = document . getElementsByClassName (
6170 'ais-SearchBox-input' ,
6271 ) [ 0 ] . value ;
6372 const domNode = ReactDOM . findDOMNode ( this ) ;
6473 if ( searchInput === '' || ! domNode || ! domNode . contains ( event . target ) )
6574 this . setState ( { hasInput : false } ) ;
66- } // end onClickOut
75+ }
6776
77+ handleKeyUp = ( event ) => {
78+ const query = event . currentTarget . value ;
79+ this . setState ( {
80+ hasInput : query . length > 2 ,
81+ } ) ;
82+
83+ this . setState ( { searchQuery : '' } , ( ) => {
84+ this . debouncedSearch ( query ) ;
85+ } ) ;
86+ }
87+
88+ handleSearch = ( query ) => {
89+ console . log ( 'Searching for:' , query ) ;
90+ this . setState ( {
91+ refresh : ! this . state . refresh ,
92+ searchQuery : query
93+ } ) ;
94+ }
6895
69- /* eslint-enabe class-methods-use-this */
7096
7197 render ( ) {
7298 const {
73- refresh, hasInput
99+ refresh, hasInput, searchQuery
74100 } = this . state ;
75101 return (
76102 < >
77-
78103 < div className = { ! hasInput ? 'form-inline flex w-1/5 items-center pl-4' : 'form-inline flex w-1/5 items-center pl-4 float-searchBox' } >
79104 < label htmlFor = "search-lc" />
80105
@@ -84,7 +109,7 @@ class SearchInputBox extends React.Component {
84109 indexName = { process . env . TYPESENSE_COLLECTION }
85110 refresh = { refresh }
86111 >
87- < Configure hitsPerPage = { 5 } />
112+ < Configure hitsPerPage = { 10 } />
88113
89114 { /* forcefeed className because component does not accept natively as prop */ }
90115 < div className = "search-icon" >
@@ -107,23 +132,19 @@ class SearchInputBox extends React.Component {
107132 translations = { {
108133 placeholder : 'Search' ,
109134 } }
110- onKeyUp = { ( event ) => {
111- this . setState ( {
112- hasInput : event . currentTarget . value . length > 2 ,
113- } ) ;
114- } }
135+ onKeyUp = { this . handleKeyUp }
115136 />
116137
117138 < div className = { ! hasInput ? 'input-empty' : '-mt-1.5 absolute background-white bg-white search_results border h-full top-20 w-2/8' } >
118139 < div className = "container" >
119140 < div className = "row" >
120141 < div className = "col-12" >
121- < SearchHits hitComponent = { Hits } />
142+ { searchQuery && < SearchHits hitComponent = { Hits } /> }
122143 </ div >
123144 </ div >
124145 < div className = "row" >
125146 < div className = "col-12" >
126- < Pagination />
147+ { searchQuery && < Pagination /> }
127148 </ div >
128149 </ div >
129150 </ div >
0 commit comments