travopti/frontend/src/app/components/search-input/search-input.component.ts
2020-06-19 17:50:37 +02:00

111 lines
3.2 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Query} from '../../interfaces/search-request.interface';
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',
templateUrl: './search-input.component.html',
styleUrls: ['./search-input.component.scss']
})
export class SearchInputComponent implements OnInit {
presets: Preset[];
singlePresets: Preset[];
multiPresets: Map<string, Preset[]>;
multiPresetsKeys: string[];
from: string;
to: string;
singlePresetSelection = {};
multiPresetSelection = {};
readonly today = this.from = formatDate(new Date(), 'yyyy-MM-dd', 'en-GB');
constructor(private router: Router, private ps: PresetService, private ss: SearchService) {
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()];
const prevInput = this.ss.loadSearchInput();
if (prevInput) {
this.from = prevInput.from;
this.to = prevInput.to;
this.singlePresetSelection = prevInput.singlePresetSelection;
this.multiPresetSelection = prevInput.multiPresetSelection;
}
}
async onSearch() {
const query: Query = {
from: new Date(this.from).getTime(),
to: new Date(this.to).getTime(),
};
for (const preset of this.singlePresets) {
if (this.singlePresetSelection[preset.preset_id]) {
query[preset.parameter] = preset.value;
}
}
for (const key of this.multiPresetsKeys) {
if (this.multiPresetSelection[key]) {
query[key] = this.presets.find(preset => preset.preset_id === this.multiPresetSelection[key]).value;
}
}
this.ss.saveSearchInput({
from: this.from,
to: this.to,
singlePresetSelection: this.singlePresetSelection,
multiPresetSelection: this.multiPresetSelection
});
await this.router.navigate(['/search'], {queryParams: {q: objToBase64(query)}});
}
/**
* Handles a (multi) preset click.
* @param preset The clicked preset
* @return If the button is selected
*/
onMultiPresetSelect(preset: Preset) {
if (this.multiPresetSelection[preset.parameter] === preset.preset_id) {
this.multiPresetSelection[preset.parameter] = undefined;
return false;
} else {
this.multiPresetSelection[preset.parameter] = preset.preset_id;
return true;
}
}
checkDates() {
const fromDate = new Date(this.from);
const toDate = new Date(this.to);
if (toDate <= fromDate) {
const newToDate = new Date(this.from);
newToDate.setDate(fromDate.getDate() + 1);
this.to = formatDate(newToDate, 'yyyy-MM-dd', 'en-GB');
}
}
}