diff --git a/src/app/geosource/components/dataset-detail/dataset-table-map/dataset-table-map.component.ts b/src/app/geosource/components/dataset-detail/dataset-table-map/dataset-table-map.component.ts index a04e00a4df03ac042f48a0d5f68864cee2985404..baf1c883e3179160ba8726131253704bf3c1af77 100644 --- a/src/app/geosource/components/dataset-detail/dataset-table-map/dataset-table-map.component.ts +++ b/src/app/geosource/components/dataset-detail/dataset-table-map/dataset-table-map.component.ts @@ -1,17 +1,18 @@ -import { Component, OnInit, ViewChild } from '@angular/core'; +import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core'; import { DatasetDetailService } from '../../../services'; import { DatasetMapComponent } from '../dataset-map/dataset-map.component'; import { FormControl } from '@angular/forms'; import { Router, ActivatedRoute } from '@angular/router'; import { AppRoutes } from '../../../../routes'; import { Data } from '../../../models'; +import { Subscription } from 'rxjs'; @Component({ selector: 'app-dataset-table-map', templateUrl: './dataset-table-map.component.html', styleUrls: ['./dataset-table-map.component.scss'], }) -export class DatasetTableMapComponent implements OnInit { +export class DatasetTableMapComponent implements OnInit, OnDestroy { @ViewChild(DatasetMapComponent) datasetMapComponent: DatasetMapComponent; @@ -27,6 +28,8 @@ export class DatasetTableMapComponent implements OnInit { selectedData: Data | mapboxgl.MapboxGeoJSONFeature = null; // Contains the properties of the selected feature dataDetailsShouldBeAnimated = false; // Used to prevent animation on first load of the component + subscriptions: Subscription[] = []; + // search searchInput = new FormControl(''); @@ -37,16 +40,26 @@ export class DatasetTableMapComponent implements OnInit { ) { } ngOnInit() { - // If the dataset has at least a table or a map then initialize the component - // else redirect to the info tab - if (this.hasTable || this.hasMap) { - this.properties = this._datasetDetailService.dataset.fields.list; - this.selectedProperties = Array.from(this.properties); - // Intialize the search value with the value from the service in order to keep it when navigating in tabs - this.searchInput.setValue(this._datasetDetailService.searchString); - } else { - this._router.navigate([`./${AppRoutes.info.uri}`], { relativeTo: this._route.parent }); - } + this.subscriptions.push( + // If the dataset has at least a table or a map then initialize the component + // else redirect to the info tab + this._datasetDetailService.dataset$.subscribe(() => { + if (this.hasTable || this.hasMap) { + this.properties = this._datasetDetailService.dataset.fields.list; + this.selectedProperties = Array.from(this.properties); + // Intialize the search value with the value from the service in order to keep it when navigating in tabs + this.searchInput.setValue(this._datasetDetailService.searchString); + } else { + this._router.navigate([`./${AppRoutes.info.uri}`], { relativeTo: this._route.parent }); + } + }), + ); + } + + ngOnDestroy() { + this.subscriptions.forEach((sub) => { + sub.unsubscribe(); + }); } get isSample(): boolean {