From 6aa7159644a4d405594c316ac2f9ecc38d9bf438 Mon Sep 17 00:00:00 2001
From: Henrik Askjer <henrik.askjer@uib.no>
Date: Tue, 7 Sep 2021 10:48:36 +0200
Subject: [PATCH] Use timestamp to evaluate the need for verbatim suggestion

---
 src/components/Autocomplete.vue | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/src/components/Autocomplete.vue b/src/components/Autocomplete.vue
index d02cf27..1ac5fac 100644
--- a/src/components/Autocomplete.vue
+++ b/src/components/Autocomplete.vue
@@ -52,10 +52,11 @@
     },
     watch: {
       search (val) {
+        const time = Date.now()
         if (! val) {
           this.items = []
         } else {
-          this.run_query(val)
+          this.run_query(val, time)
         }
       },
       select(item) {
@@ -81,17 +82,17 @@
       }
     },
     methods: {
-      run_query(q) {
+      run_query(q, time) {
         this.suggesting = true
         // Keep full text search in the list while requesting suggestions
         if (this.items[0]) {
-          if (this.items[0].lang) {
-          this.items.unshift({q: q, label: q})
-        }
-          else {
-            this.items[0] = {q: q, label: q}
+          if (this.items[0].time < time) {
+            this.items.splice(0,1, {q: q, label: q, time: time})
           }
         }
+        else {
+          this.items.push({q: q, label: q, time: time})
+        }
         let self = this
         self.api.get('suggest?', {params: {q: q, dict: self.$parent.lang, n: 9}})
             .then(async (response) => {
@@ -100,7 +101,7 @@
                           let hits = []
                           response.data.forEach((item, i) => {
                             let match = item[0]
-                            let hit = {q: q, match: match, label: match}
+                            let hit = {q: q, match: match, label: match, time: time}
 
                             hit.article_promise = self.api.get('articles?', {params: {lord: match, dict: self.$parent.lang}})
 
@@ -109,7 +110,7 @@
 
                           });
                           // whitespace necessary because duplicates aren't allowed in the dropdown
-                          hits.push({q: q, label: q + ' '})
+                          hits.push({q: q, label: q + ' ', time: time})
                           self.items = hits
                         }
                       self.loading = false
-- 
GitLab