From 860d351323eb9f3284b0f6d8b6f6a5e5cd8fc34c Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Fri, 8 Jan 2021 14:56:18 +0100 Subject: [PATCH] Extract common code for table loading Preselect dashboard station in table selection model --- .../app/dashboard/table/table.component.ts | 52 +++++++------------ 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/projects/project-3/frontend/src/app/dashboard/table/table.component.ts b/projects/project-3/frontend/src/app/dashboard/table/table.component.ts index 6509a30..03c5ab6 100644 --- a/projects/project-3/frontend/src/app/dashboard/table/table.component.ts +++ b/projects/project-3/frontend/src/app/dashboard/table/table.component.ts @@ -16,8 +16,8 @@ import {ActivatedRoute} from '@angular/router'; export class TableComponent implements OnInit { displayedColumnsTo: string[] = ['select', 'endStationName', 'number', 'avgDuration', 'marker']; displayedColumnsFrom: string[] = ['select', 'startStationName', 'number', 'avgDuration', 'marker']; - stationToSource = new MatTableDataSource(); + stationToSource = new MatTableDataSource(); iterableToSource: any[]; stationFromSource = new MatTableDataSource(); iterableFromSource: any[]; @@ -37,8 +37,6 @@ export class TableComponent implements OnInit { } ngOnInit(): void { - this.isLoadingToSource = true; - this.isLoadingFromSource = true; this.route.params.subscribe(params => { this.colors = ['black', 'gray', 'green', 'orange', 'purple', 'red']; this.service.fetchDashboardInit(params.id).then(data => { @@ -50,33 +48,30 @@ export class TableComponent implements OnInit { }); } - async initTable(): Promise { + initTable(): void { this.selectionModel.clear(); this.map.removeOverlayOnMiniMap(); const initDate = this.maxEndDate.toISOString().substring(0, 10); - - const [stationTo, stationFrom] = await Promise.all([ - this.service.fetchDashboardStationTo(this.bikePoint.id, initDate, initDate), - this.service.fetchDashboardStationFrom(this.bikePoint.id, initDate, initDate) - ]); - - this.isLoadingToSource = false; - this.isLoadingFromSource = false; - - this.stationToSource = this.setBikePointColorToSource(stationTo); - this.iterableToSource = stationTo; - this.iterableToSource.forEach(bikePoint => bikePoint.polyLineColor = 'red'); - - this.stationFromSource = this.setBikePointColorFromSource(stationFrom); - this.iterableFromSource = stationFrom; - this.iterableFromSource.forEach(bikePoint => bikePoint.polyLineColor = 'green'); + this.loadData(initDate, initDate); } - async onSubmit(actualStartDate: string, actualEndDate: string): Promise { + onSubmit(actualStartDate: string, actualEndDate: string): void { this.resetTableSourcesToDisplaySpinner(); this.selectionModel.clear(); this.map.removeOverlayOnMiniMap(); + this.loadData(actualStartDate, actualEndDate); + } + resetTableSourcesToDisplaySpinner(): void { + this.isLoadingToSource = true; + this.isLoadingFromSource = true; + this.stationToSource = null; + this.stationFromSource = null; + this.iterableToSource = []; + this.iterableFromSource = []; + } + + async loadData(actualStartDate: string, actualEndDate: string): Promise { this.isLoadingToSource = true; this.isLoadingFromSource = true; @@ -97,15 +92,8 @@ export class TableComponent implements OnInit { this.iterableFromSource = stationFrom; this.iterableFromSource.forEach(bikePoint => bikePoint.polyLineColor = 'red'); - } - - resetTableSourcesToDisplaySpinner(): void { - this.isLoadingToSource = true; - this.isLoadingFromSource = true; - this.stationToSource = null; - this.stationFromSource = null; - this.iterableToSource = []; - this.iterableFromSource = []; + this.selectionModel.select(...this.iterableFromSource.filter(bikePoint => bikePoint.stationId === this.bikePoint.id)); + this.selectionModel.select(...this.iterableToSource.filter(bikePoint => bikePoint.stationId === this.bikePoint.id)); } public drawIconInTable(bikePoint: any): string { @@ -137,9 +125,7 @@ export class TableComponent implements OnInit { changePolyLineColorForDuplicateBikePoints(array: any[]): any[] { const id = array.map(item => item.stationId); - const duplicates = id.filter((value, index) => { - return id.indexOf(value) !== index; - }); + const duplicates = id.filter((value, index) => id.indexOf(value) !== index); duplicates.forEach(stationId => { array.forEach(bikePoint => { if (bikePoint.stationId === stationId) {