@@ -2,20 +2,20 @@ import { Component, EventEmitter, Input, OnChanges, OnDestroy, Output, SimpleCha
22import { Subject , takeUntil } from 'rxjs' ;
33import { AppAuthService } from 'src/app/app-auth.service' ;
44import { UserPermissionModel } from 'src/app/admin/users/models/user-permission.model' ;
5- import { PagesDataService , ToastEventTypeEnum } from 'src/app/core/services/pages-data.service' ;
5+ import { PagesDataService , ToastEventTypeEnum } from 'src/app/core/services/pages-data.service' ;
66import { DateUtils } from 'src/app/shared/utils/date.utils' ;
77import { CachedMetadataService } from 'src/app/metadata/metadata-updates/cached-metadata.service' ;
88import { StringUtils } from 'src/app/shared/utils/string.utils' ;
99import { DurationTypeEnum } from '../../models/duration-type.enum' ;
1010
1111export interface DataAvailabilityFilterModel {
12- stationIds ?: string [ ] ;
13- elementIds ?: number [ ] ;
14- interval ?: number ;
15- level ?: number ;
16- durationType : DurationTypeEnum ;
17- fromDate : string ;
18- toDate : string ;
12+ stationIds ?: string [ ] ;
13+ elementIds ?: number [ ] ;
14+ interval ?: number ;
15+ level ?: number ;
16+ durationType : DurationTypeEnum ;
17+ fromDate : string ;
18+ toDate : string ;
1919}
2020
2121@Component ( {
@@ -52,7 +52,7 @@ export class DataAvailabilityFilterSelectionGeneralComponent implements OnChange
5252
5353 constructor (
5454 private pagesDataService : PagesDataService ,
55- private appAuthService : AppAuthService ,
55+ private appAuthService : AppAuthService ,
5656 private cachedMetadataService : CachedMetadataService ,
5757 ) {
5858
@@ -70,6 +70,14 @@ export class DataAvailabilityFilterSelectionGeneralComponent implements OnChange
7070
7171 ngOnChanges ( changes : SimpleChanges ) : void {
7272 if ( changes [ 'inputFilter' ] && this . inputFilter && this . inputFilter !== this . outputFilter ) {
73+ if ( this . outputFilter ) {
74+ if ( this . areFiltersEqual ( this . inputFilter , this . outputFilter ) ) {
75+ console . log ( 'inputFilter are equal' ) ;
76+ return ;
77+ }
78+ }
79+
80+ console . log ( 'setting outputFilter' ) ;
7381 this . outputFilter = { ...this . inputFilter } ;
7482 this . setSelectionsFromQuery ( ) ;
7583 }
@@ -80,12 +88,45 @@ export class DataAvailabilityFilterSelectionGeneralComponent implements OnChange
8088 this . destroy$ . complete ( ) ;
8189 }
8290
91+ private areFiltersEqual (
92+ filter1 : DataAvailabilityFilterModel ,
93+ filter2 : DataAvailabilityFilterModel
94+ ) : boolean {
95+ // Compare primitive values
96+ if (
97+ filter1 . interval !== filter2 . interval ||
98+ filter1 . level !== filter2 . level ||
99+ filter1 . durationType !== filter2 . durationType ||
100+ filter1 . fromDate !== filter2 . fromDate ||
101+ filter1 . toDate !== filter2 . toDate
102+ ) {
103+ return false ;
104+ }
105+
106+ // Compare arrays
107+ return (
108+ this . areArraysEqualUnordered ( filter1 . stationIds , filter2 . stationIds ) &&
109+ this . areArraysEqualUnordered ( filter1 . elementIds , filter2 . elementIds )
110+ ) ;
111+ }
112+
113+ private areArraysEqualUnordered < T > ( arr1 ?: T [ ] , arr2 ?: T [ ] ) : boolean {
114+ if ( arr1 === arr2 ) return true ;
115+ if ( ! arr1 || ! arr2 ) return false ;
116+ if ( arr1 . length !== arr2 . length ) return false ;
117+
118+ const sorted1 = [ ...arr1 ] . sort ( ) ;
119+ const sorted2 = [ ...arr2 ] . sort ( ) ;
120+
121+ return sorted1 . every ( ( val , index ) => val === sorted2 [ index ] ) ;
122+ }
123+
83124 private setSelectionsFromQuery ( ) {
84125 if ( this . outputFilter . stationIds ) this . stationIds = this . outputFilter . stationIds ;
85126 if ( this . outputFilter . elementIds ) this . elementIds = this . outputFilter . elementIds ;
86127 if ( this . outputFilter . level ) this . level = this . outputFilter . level ;
87128 if ( this . outputFilter . interval ) this . interval = this . outputFilter . interval ;
88-
129+
89130 this . durationType = this . outputFilter . durationType ;
90131 const fromDate = DateUtils . getDatetimesBasedOnUTCOffset ( this . outputFilter . fromDate , this . cachedMetadataService . utcOffSet , 'add' ) . split ( 'T' ) [ 0 ] ;
91132 const toDate = DateUtils . getDatetimesBasedOnUTCOffset ( this . outputFilter . toDate , this . cachedMetadataService . utcOffSet , 'add' ) . split ( 'T' ) [ 0 ] ;
@@ -102,7 +143,7 @@ export class DataAvailabilityFilterSelectionGeneralComponent implements OnChange
102143 this . durationYear = Number ( fromYear ) ;
103144 break ;
104145 case DurationTypeEnum . YEARS :
105- this . durationYears = [ Number ( fromYear ) , Number ( toDate [ 0 ] ) ] ;
146+ this . durationYears = [ Number ( fromYear ) , Number ( toDate . split ( '-' ) [ 0 ] ) ] ;
106147 break ;
107148 default :
108149 throw new Error ( 'Developer error. Duration type not supported' ) ;
@@ -203,7 +244,7 @@ export class DataAvailabilityFilterSelectionGeneralComponent implements OnChange
203244 this . outputFilter = {
204245 durationType : this . durationType ,
205246 fromDate : fromDate ,
206- toDate : toDate ,
247+ toDate : toDate ,
207248 } ;
208249
209250 if ( this . stationIds . length > 0 ) this . outputFilter . stationIds = this . stationIds ;
0 commit comments