From e706a2633eef8096d1012f0ef5860296c8f3a7bf Mon Sep 17 00:00:00 2001 From: Patrick Gebhardt Date: Thu, 18 Jun 2020 00:52:27 +0200 Subject: [PATCH] Fix preset loading --- frontend/src/app/app.component.ts | 5 ++--- .../search-input/search-input.component.ts | 17 +++++++++-------- .../app/containers/search/search.component.ts | 18 +++++++----------- frontend/src/app/services/preset.service.ts | 4 ++++ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 8335b68..3ceca05 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -1,5 +1,4 @@ import {Component, OnInit} from '@angular/core'; -import {PresetService} from './services/preset.service'; @Component({ selector: 'app-root', @@ -8,10 +7,10 @@ import {PresetService} from './services/preset.service'; }) export class AppComponent implements OnInit { - constructor(private ps: PresetService) { + constructor() { } ngOnInit(): void { - this.ps.initialize().catch(e => console.log(e)); + } } 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 9586af6..1fa694c 100644 --- a/frontend/src/app/components/search-input/search-input.component.ts +++ b/frontend/src/app/components/search-input/search-input.component.ts @@ -24,26 +24,27 @@ export class SearchInputComponent implements OnInit { multiPresetSelection = {}; constructor(private router: Router, private ps: PresetService) { - this.presets = ps.presets; - this.singlePresets = ps.singlePresets; - this.multiPresets = ps.multiPresets; - this.multiPresetsKeys = [...ps.multiPresets.keys()]; - const from = new Date(); const to = new Date(); to.setDate(from.getDate() + 7); this.from = formatDate(from, 'yyyy-MM-dd', 'en-GB'); this.to = formatDate(to, 'yyyy-MM-dd', 'en-GB'); + } + + async ngOnInit() { + await this.ps.initialize(); + + this.presets = this.ps.presets; + this.singlePresets = this.ps.singlePresets; + this.multiPresets = this.ps.multiPresets; + this.multiPresetsKeys = [...this.multiPresets.keys()]; for (const preset of this.singlePresets) { this.singlePresetSelection[preset.preset_id] = false; } } - ngOnInit() { - } - async onSearch() { const query: Query = { from: new Date(this.from).getTime(), diff --git a/frontend/src/app/containers/search/search.component.ts b/frontend/src/app/containers/search/search.component.ts index 2205b4e..60f5996 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} from '@angular/router'; import {Result} from '../../interfaces/result.interface'; -import {MOCK_RESULT} from '../../mock/mock-data'; +import {DataService} from '../../services/data.service'; @Component({ selector: 'app-search', @@ -16,19 +16,15 @@ export class SearchComponent implements OnInit { @ViewChild('result', {static: false}) resultDiv: ElementRef; - constructor(private route: ActivatedRoute) { + constructor(private route: ActivatedRoute, private ds: DataService) { } - ngOnInit() { - this.route.queryParams.subscribe(params => { + async ngOnInit() { + this.route.queryParams.subscribe(async params => { this.queryString = params.q; - }); - - // Mock results - setTimeout(() => { - this.results = MOCK_RESULT; + this.results = undefined; + this.results = await this.ds.searchRegions(this.queryString); this.resultDiv.nativeElement.scrollIntoView({behavior: 'smooth', block: 'start'}); - }, 1000); - + }); } } diff --git a/frontend/src/app/services/preset.service.ts b/frontend/src/app/services/preset.service.ts index 5caea3a..da8f0da 100644 --- a/frontend/src/app/services/preset.service.ts +++ b/frontend/src/app/services/preset.service.ts @@ -15,6 +15,10 @@ export class PresetService { } async initialize() { + if (this.presets) { + return; + } + this.presets = await this.ds.getAllPresets(); const presetMap = new Map();