adrema/resources/livewire-js/searchHelper.js

59 lines
1.3 KiB
JavaScript

export default (params = [], options = {}) => ({
params: params,
options: options,
results: {
hits: [],
},
realSearchString: '',
async search(text, filters = [], options = {}) {
var response = await axios.post(
import.meta.env.MODE === 'development' ? 'http://localhost:7700/indexes/members/search' : '/indexes/members/search',
{
q: text,
filter: filters,
sort: ['lastname:asc', 'firstname:asc'],
...options,
},
{headers: {Authorization: 'Bearer ' + document.querySelector('meta[name="meilisearch_key"]').content}}
);
return response.data;
},
async setInput(v) {
this.realSearchString = v;
if (!v.length) {
this.results = {hits: []};
return;
}
this.results = await this.search(v, this.params, this.options);
},
getInput() {
return this.realSearchString;
},
clear() {
this.setInput('');
},
getHits() {
return this.results.hits;
},
hasHits() {
return this.results.hits.length > 0;
},
hasNoHits() {
return !this.hasHits();
},
firstHit() {
return this.results.hits[0];
},
});