diff --git a/frontend/src/app/components/search-input/search-input.component.ts b/frontend/src/app/components/search-input/search-input.component.ts index 1fa694c..d584094 100644 --- a/frontend/src/app/components/search-input/search-input.component.ts +++ b/frontend/src/app/components/search-input/search-input.component.ts @@ -5,6 +5,7 @@ import {objToBase64} from '../../utils/base64conversion'; import {PresetService} from '../../services/preset.service'; import {Preset} from '../../interfaces/preset.interface'; import {formatDate} from '@angular/common'; +import {SearchService} from '../../services/search.service'; @Component({ selector: 'app-search-input', @@ -23,7 +24,7 @@ export class SearchInputComponent implements OnInit { singlePresetSelection = {}; multiPresetSelection = {}; - constructor(private router: Router, private ps: PresetService) { + constructor(private router: Router, private ps: PresetService, private ss: SearchService) { const from = new Date(); const to = new Date(); to.setDate(from.getDate() + 7); @@ -40,8 +41,13 @@ export class SearchInputComponent implements OnInit { this.multiPresets = this.ps.multiPresets; this.multiPresetsKeys = [...this.multiPresets.keys()]; - for (const preset of this.singlePresets) { - this.singlePresetSelection[preset.preset_id] = false; + const prevInput = this.ss.loadSearchInput(); + + if (prevInput) { + this.from = prevInput.from; + this.to = prevInput.to; + this.singlePresetSelection = prevInput.singlePresetSelection; + this.multiPresetSelection = prevInput.multiPresetSelection; } } @@ -63,6 +69,14 @@ export class SearchInputComponent implements OnInit { } } + this.ss.saveSearchInput({ + from: this.from, + to: this.to, + singlePresetSelection: this.singlePresetSelection, + multiPresetSelection: this.multiPresetSelection + }); + + await this.router.navigate(['/search'], {queryParams: {q: objToBase64(query)}}); } } diff --git a/frontend/src/app/containers/search/search.component.ts b/frontend/src/app/containers/search/search.component.ts index fbedc78..6950761 100644 --- a/frontend/src/app/containers/search/search.component.ts +++ b/frontend/src/app/containers/search/search.component.ts @@ -1,7 +1,7 @@ import {Component, ElementRef, OnInit, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Result} from '../../interfaces/result.interface'; -import {DataService} from '../../services/data.service'; +import {SearchService} from '../../services/search.service'; @Component({ selector: 'app-search', @@ -16,7 +16,7 @@ export class SearchComponent implements OnInit { @ViewChild('result', {static: false}) resultDiv: ElementRef; - constructor(private route: ActivatedRoute, private ds: DataService, private router: Router) { + constructor(private route: ActivatedRoute, private ss: SearchService, private router: Router) { } async ngOnInit() { @@ -29,7 +29,7 @@ export class SearchComponent implements OnInit { return; } - this.results = await this.ds.searchRegions(this.queryString); + this.results = await this.ss.executeSearch(this.queryString); this.resultDiv.nativeElement.scrollIntoView({behavior: 'smooth', block: 'start'}); }); } diff --git a/frontend/src/app/interfaces/search-request.interface.ts b/frontend/src/app/interfaces/search-request.interface.ts index e96f3c1..576db17 100644 --- a/frontend/src/app/interfaces/search-request.interface.ts +++ b/frontend/src/app/interfaces/search-request.interface.ts @@ -8,10 +8,10 @@ export interface Query { sun_hours?: number[]; alcohol_costs?: number[]; food_costs?: number[]; - local_transportation_costs: number[]; - entertainment_costs: number[]; - accommodation_costs: number[]; - average_per_day_costs: number[]; + local_transportation_costs?: number[]; + entertainment_costs?: number[]; + accommodation_costs?: number[]; + average_per_day_costs?: number[]; } export enum SearchParameter { diff --git a/frontend/src/app/services/search.service.spec.ts b/frontend/src/app/services/search.service.spec.ts new file mode 100644 index 0000000..9496323 --- /dev/null +++ b/frontend/src/app/services/search.service.spec.ts @@ -0,0 +1,12 @@ +import {TestBed} from '@angular/core/testing'; + +import {SearchService} from './search.service'; + +describe('SearchService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: SearchService = TestBed.get(SearchService); + expect(service).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/services/search.service.ts b/frontend/src/app/services/search.service.ts new file mode 100644 index 0000000..72bba81 --- /dev/null +++ b/frontend/src/app/services/search.service.ts @@ -0,0 +1,33 @@ +import {Injectable} from '@angular/core'; +import {Result} from '../interfaces/result.interface'; +import {DataService} from './data.service'; + +export interface SearchInput { + from: string; + to: string; + singlePresetSelection: object; + multiPresetSelection: object; +} + +@Injectable({ + providedIn: 'root' +}) +export class SearchService { + + private searchInput: SearchInput; + + constructor(private ds: DataService) { + } + + public executeSearch(query: string): Promise { + return this.ds.searchRegions(query); + } + + public saveSearchInput(input: SearchInput) { + this.searchInput = input; + } + + public loadSearchInput(): SearchInput { + return this.searchInput; + } +}