X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/d92b067614b451c27a8bd702253e87278defa7a3..06fc8f6a97afa24144cb435f5af228fec6162a6b:/src/services/search-service/search-service.ts diff --git a/src/services/search-service/search-service.ts b/src/services/search-service/search-service.ts index 1fc61dd2..84d120a8 100644 --- a/src/services/search-service/search-service.ts +++ b/src/services/search-service/search-service.ts @@ -2,20 +2,43 @@ // // SPDX-License-Identifier: AGPL-3.0 -export class SearchQueriesService { - private recentQueries: string[] = this.getRecentQueries(); +import { SearchBarAdvanceFormData } from '~/models/search-bar'; + +export class SearchService { + private recentQueries = this.getRecentQueries(); + private savedQueries: SearchBarAdvanceFormData[] = 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: SearchBarAdvanceFormData) { + this.savedQueries.push({...data}); + localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries)); + } + + editSavedQueries(data: SearchBarAdvanceFormData) { + 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 SearchBarAdvanceFormData[]; + } + + deleteSavedQuery(id: number) { + this.savedQueries.splice(id, 1); + localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries)); + } +} + +const MAX_NUMBER_OF_RECENT_QUERIES = 5;