1
0
Fork 0
mirror of https://github.com/Findus23/acronomy.git synced 2024-09-19 15:33:45 +02:00
acronomy/static/js/autocomplete.ts
2024-07-02 22:37:09 +02:00

34 lines
898 B
TypeScript

import Autocomplete from "@trevoreyre/autocomplete-js"
interface AutocompleteResult {
url: string
name: string
full_name: string
slug: string
}
new Autocomplete('#autocomplete', {
search: (input: string) => {
const acroSearch = input.split(':')[0];
const url = "/api/acronym/?search=" + acroSearch
return new Promise(resolve => {
if (acroSearch.length < 1) {
return resolve([])
}
fetch(url)
.then(response => response.json())
.then((data:AutocompleteResult[]) => {
resolve(data)
})
})
},
getResultValue: (result:AutocompleteResult) => (result.name + ": " + result.full_name),
onSubmit: (result:AutocompleteResult) => {
window.location.href = "/acronym/" + result.slug
},
autoSelect: true,
})