1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { SearchBarAdvancedFormData } from 'models/search-bar';
7 export class SearchService {
8 private recentQueries = this.getRecentQueries();
9 private savedQueries: SearchBarAdvancedFormData[] = this.getSavedQueries();
11 saveRecentQuery(query: string) {
12 if (this.recentQueries.length >= MAX_NUMBER_OF_RECENT_QUERIES) {
13 this.recentQueries.shift();
15 this.recentQueries.push(query);
16 localStorage.setItem('recentQueries', JSON.stringify(this.recentQueries));
19 getRecentQueries(): string[] {
20 return JSON.parse(localStorage.getItem('recentQueries') || '[]');
23 saveQuery(data: SearchBarAdvancedFormData) {
24 this.savedQueries.push({...data});
25 localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
28 editSavedQueries(data: SearchBarAdvancedFormData) {
29 const itemIndex = this.savedQueries.findIndex(item => item.queryName === data.queryName);
30 this.savedQueries[itemIndex] = {...data};
31 localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
35 return JSON.parse(localStorage.getItem('savedQueries') || '[]') as SearchBarAdvancedFormData[];
38 deleteSavedQuery(id: number) {
39 this.savedQueries.splice(id, 1);
40 localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries));
44 const MAX_NUMBER_OF_RECENT_QUERIES = 5;