adrema/resources/js/composables/useSearch.js

50 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2024-07-03 17:31:06 +02:00
import {inject, computed, ref} from 'vue';
2024-01-28 11:42:32 +01:00
2024-07-03 17:31:06 +02:00
export default function useSearch(params = null, options = null) {
params = params === null ? [] : params;
options = options === null ? {} : options;
2024-01-28 11:42:32 +01:00
const axios = inject('axios');
2024-07-03 17:31:06 +02:00
const results = ref({hits: []});
const realSearchString = ref('');
2024-01-29 01:37:28 +01:00
async function search(text, filters = [], options = {}) {
2024-01-28 11:42:32 +01:00
var response = await axios.post(
2024-07-03 17:03:53 +02:00
import.meta.env.MODE === 'development' ? 'http://localhost:7700/indexes/members/search' : '/indexes/members/search',
2024-01-28 11:42:32 +01:00
{
q: text,
filter: filters,
sort: ['lastname:asc', 'firstname:asc'],
2024-01-29 01:37:28 +01:00
...options,
2024-01-28 11:42:32 +01:00
},
{headers: {Authorization: 'Bearer ' + document.querySelector('meta[name="meilisearch_key"]').content}}
);
return response.data;
}
2024-07-03 17:31:06 +02:00
function clearSearch() {
searchString.value = '';
}
const searchString = computed({
get: () => realSearchString.value,
set: async (v) => {
realSearchString.value = v;
if (!v.length) {
results.value = {hits: []};
return;
}
results.value = await search(v, params, options);
},
});
2024-01-28 11:42:32 +01:00
return {
search,
2024-07-03 17:31:06 +02:00
searchString,
results,
clearSearch,
2024-01-28 11:42:32 +01:00
};
}