Merge branch '18966-collection-not-found-ui'. Closes #18966
[arvados-workbench2.git] / src / services / search-service / search-service.ts
index 1fc61dd2199366c2e64b30c36da3c8d5a93dc192..726af8954112208c5b9913c007618bf1de6ecd30 100644 (file)
@@ -2,20 +2,43 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export class SearchQueriesService {
-    private recentQueries: string[] = this.getRecentQueries();
+import { SearchBarAdvancedFormData } from 'models/search-bar';
+
+export class SearchService {
+    private recentQueries = this.getRecentQueries();
+    private savedQueries: SearchBarAdvancedFormData[] = this.getSavedQueries();
 
     saveRecentQuery(query: string) {
-        if (this.recentQueries.length >= 5) {
+        if (this.recentQueries.length >= MAX_NUMBER_OF_RECENT_QUERIES) {
             this.recentQueries.shift();
-            this.recentQueries.push(query);
-        } else {
-            this.recentQueries.push(query);
         }
+        this.recentQueries.push(query);
         localStorage.setItem('recentQueries', JSON.stringify(this.recentQueries));
     }
 
-    getRecentQueries() {
-        return JSON.parse(localStorage.getItem('recentQueries') || '[]') as string[];
+    getRecentQueries(): string[] {
+        return JSON.parse(localStorage.getItem('recentQueries') || '[]');
+    }
+
+    saveQuery(data: SearchBarAdvancedFormData) {
+        this.savedQueries.push({...data});
+        localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
+    }
+
+    editSavedQueries(data: SearchBarAdvancedFormData) {
+        const itemIndex = this.savedQueries.findIndex(item => item.queryName === data.queryName);
+        this.savedQueries[itemIndex] = {...data};
+        localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
     }
-}
\ No newline at end of file
+
+    getSavedQueries() {
+        return JSON.parse(localStorage.getItem('savedQueries') || '[]') as SearchBarAdvancedFormData[];
+    }
+
+    deleteSavedQuery(id: number) {
+        this.savedQueries.splice(id, 1);
+        localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
+    }
+}
+
+const MAX_NUMBER_OF_RECENT_QUERIES = 5;