X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/0ee5928db8864a0f7af0c2727defdf800f1604cc..33a21c007118a9dc2cefe2c1e103dd57dc8a0094:/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 c394da5e..5817275e 100644 --- a/src/services/search-service/search-service.ts +++ b/src/services/search-service/search-service.ts @@ -2,12 +2,14 @@ // // SPDX-License-Identifier: AGPL-3.0 -export class SearchQueriesService { +import { SearchBarAdvanceFormData } from '~/models/search-bar'; + +export class SearchService { private recentQueries: string[] = this.getRecentQueries(); - private savedQueries: string[] = this.getSavedQueries(); + 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 { @@ -20,17 +22,25 @@ export class SearchQueriesService { return JSON.parse(localStorage.getItem('recentQueries') || '[]') as string[]; } - saveQuery(query: string) { - this.savedQueries.push(query); + saveQuery(data: SearchBarAdvanceFormData) { + this.savedQueries.push({...data}); + localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries)); + } + + editSavedQueries(data: SearchBarAdvanceFormData) { + const itemIndex = this.savedQueries.findIndex(item => item.searchQuery === data.searchQuery); + this.savedQueries[itemIndex] = {...data}; localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries)); } getSavedQueries() { - return JSON.parse(localStorage.getItem('savedQueries') || '[]') as string[]; + return JSON.parse(localStorage.getItem('savedQueries') || '[]') as SearchBarAdvanceFormData[]; } deleteSavedQuery(id: number) { this.savedQueries.splice(id, 1); localStorage.setItem('savedQueries', JSON.stringify(this.savedQueries)); } -} \ No newline at end of file +} + +const MAX_NUMBER_OF_RECENT_QUERIES = 5; \ No newline at end of file