Code Review Friday: Let's Improve your code, ask Corvid master

Thanks for the live link and collection and fieldkey details!
you can remove comments start with “comment :”, just to explain
why we did it.

This is exactly with in the scope :slight_smile:

import wixData from 'wix-data';

$w.onReady(function () {
  filterRepeater();

 // event
 // comment : onInput is better than onKeypress it will 
 // comment : run after the key is register, so we don't need to use a delay
 // comment : but we do need a debouncer so, we can limit the function call

 let debouncer;
  $w('#iLocation').onInput(()=>{
 if( debouncer ) return ;
    debouncer = setTimeout(()=>{
      debouncer = undefined;
      filterRepeater();
    }, 200);
  });


  $w('#tags').onChange((event) => {
    filterRepeater();
  });

 // comment : you don't need to use a function to call
 // comment : everytime when the data change, you can just put it on page ready
  $w('#csRepeater').onItemReady(($item, itemData) => {
    $item('#csRepeaterSpecies').text = itemData.species;
  })
});



async function filterRepeater() {
 const location = $w('#iLocation').value;
 const categories = $w('#tags').value;

 if(location)

 let filter = wixData.filter();

 if( location.length > 0 ) {
 // comment : onInput runs only on input change (i think) unlike keypress
 // comment : where we need to check if the input value is change
 // comment : using the lastFilterLocation
    filter = filter.contains('overviewRange', location);
  }
 
 if ( categories.length > 0 ) {
    filter = filter.hasAll("caresheetTags", categories)
  }
 
  console.log("filter object : " , filter);
 await $w('#csdb').setFilter(filter);

 let ttl = $w('#csdb').getTotalCount();
  console.log("total item found : " , ttl);

 if( ttl > 0 ) {
 // $w('#noResultsFound').show()
  } else {
 // $w('#noResultsFound').hide()
  }
}