optimize autocomplete

This commit is contained in:
Pete Matsyburka
2024-02-27 15:20:34 +02:00
parent 801abd197b
commit 6ca44a5cc3
2 changed files with 7 additions and 3 deletions
@@ -24,9 +24,9 @@ module Api
if SELECT_COLUMNS.include?(params[:field])
column = Submitter.arel_table[params[:field].to_sym]
term = "%#{params[:q].downcase}%"
term = "#{params[:q].downcase}%"
submitters.where(column.lower.matches(term))
submitters.where(column.matches(term, false, true))
else
Submitters.search(submitters, params[:q])
end
@@ -37,7 +37,9 @@ export default class extends HTMLElement {
if (q) {
const queryParams = new URLSearchParams({ q, field: this.dataset.field })
fetch('/api/submitters_autocomplete?' + queryParams).then(async (resp) => {
this.currentFetch ||= fetch('/api/submitters_autocomplete?' + queryParams)
this.currentFetch.then(async (resp) => {
const items = await resp.json()
if (q.length < 3) {
@@ -47,6 +49,8 @@ export default class extends HTMLElement {
}
}).catch(() => {
resolve([])
}).finally(() => {
this.currentFetch = null
})
} else {
resolve([])