(fetchCall: (search: string) => Promise<T[]>)
| 5 | import { debounce } from '~/utils/helper.utils'; |
| 6 | |
| 7 | export const useAutocomplete = <T>(fetchCall: (search: string) => Promise<T[]>) => { |
| 8 | const search = ref<string>(''); |
| 9 | const loading = ref<boolean>(false); |
| 10 | const results: Ref<T[]> = ref([]); |
| 11 | |
| 12 | const triggerSearch = debounce(async () => { |
| 13 | try { |
| 14 | loading.value = true; |
| 15 | const fetchResults = await fetchCall(search.value); |
| 16 | |
| 17 | results.value = fetchResults; |
| 18 | } catch (err) { |
| 19 | logger.error(`Failed to search`, { err }); |
| 20 | |
| 21 | results.value = []; |
| 22 | } finally { |
| 23 | loading.value = false; |
| 24 | } |
| 25 | }, 500); |
| 26 | |
| 27 | const searchItems = async (searchTerm?: string): Promise<void> => { |
| 28 | search.value = searchTerm || ''; |
| 29 | |
| 30 | return triggerSearch(); |
| 31 | }; |
| 32 | |
| 33 | return { |
| 34 | search, |
| 35 | loading, |
| 36 | results, |
| 37 | searchItems, |
| 38 | }; |
| 39 | }; |
| 40 | |
| 41 | export const useUserGroupsAutocomplete = () => { |
| 42 | const station = useStationStore(); |
no test coverage detected