1- import { Component , EventEmitter , Input , OnInit , Output , ViewChild } from '@angular/core' ;
1+ import { Component , EventEmitter , Input , OnDestroy , OnInit , Output , ViewChild } from '@angular/core' ;
22import { NgbActiveModal , NgbModal , NgbPopover } from '@ng-bootstrap/ng-bootstrap' ;
33import { FILTER_OPERATORS } from '../../../../constants/filter-operators.const' ;
44import { ElasticOperatorsEnum } from '../../../../enums/elastic-operators.enum' ;
@@ -8,13 +8,15 @@ import {OperatorsType} from '../../../../types/filter/operators.type';
88import { TimeFilterType } from '../../../../types/time-filter.type' ;
99import { ElasticFilterDefaultTime } from '../elastic-filter-time/elastic-filter-time.component' ;
1010import { UtmFilterBehavior } from './shared/behavior/utm-filter.behavior' ;
11+ import { Subject } from "rxjs" ;
12+ import { filter , takeUntil } from "rxjs/operators" ;
1113
1214@Component ( {
1315 selector : 'app-utm-elastic-filter' ,
1416 templateUrl : './elastic-filter.component.html' ,
1517 styleUrls : [ './elastic-filter.component.scss' ]
1618} )
17- export class ElasticFilterComponent implements OnInit {
19+ export class ElasticFilterComponent implements OnInit , OnDestroy {
1820 @Output ( ) filterChange = new EventEmitter < ElasticFilterType [ ] > ( ) ;
1921 @Input ( ) pattern : string ;
2022 @Input ( ) filters : ElasticFilterType [ ] = [ ] ;
@@ -25,6 +27,7 @@ export class ElasticFilterComponent implements OnInit {
2527 filterSelected : ElasticFilterType ;
2628 indexEdit : number ;
2729 editMode : boolean ;
30+ destroy$ : Subject < void > = new Subject < void > ( ) ;
2831
2932 constructor ( public modalService : NgbModal ,
3033 private activeModal : NgbActiveModal ,
@@ -33,11 +36,18 @@ export class ElasticFilterComponent implements OnInit {
3336
3437 ngOnInit ( ) {
3538 this . filters = this . filters ? this . filters : [ ] ;
36- this . utmFilterBehavior . $filterChange . subscribe ( filter => {
37- if ( filter ) {
38- this . filters . push ( filter ) ;
39- this . filterChange . emit ( this . filters ) ;
39+
40+ this . utmFilterBehavior . $filterChange
41+ . pipe ( takeUntil ( this . destroy$ ) ,
42+ filter ( filterType => ! ! filterType ) )
43+ . subscribe ( filterType => {
44+ if ( filterType . status === 'ACTIVE' ) {
45+ this . filters . push ( filterType ) ;
46+ } else {
47+ this . filters = this . filters . filter ( f => f . value !== filterType . value ) ;
4048 }
49+
50+ this . filterChange . emit ( this . filters ) ;
4151 } ) ;
4252 }
4353
@@ -105,4 +115,9 @@ export class ElasticFilterComponent implements OnInit {
105115 resolveFilters ( ) : ElasticFilterType [ ] {
106116 return this . filters . filter ( value => value . operator !== ElasticOperatorsEnum . IS_IN_FIELD ) ;
107117 }
118+
119+ ngOnDestroy ( ) : void {
120+ this . destroy$ . next ( ) ;
121+ this . destroy$ . complete ( ) ;
122+ }
108123}
0 commit comments