1- import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ;
1+ import { Component , EventEmitter , Input , OnDestroy , OnInit , Output } from '@angular/core' ;
2+ import { Subject } from 'rxjs' ;
3+ import { takeUntil } from 'rxjs/operators' ;
24import { ALERT_TAGS_FIELD } from '../../../../../../shared/constants/alert/alert-field.constant' ;
35import { ALERT_INDEX_PATTERN } from '../../../../../../shared/constants/main-index-pattern.constant' ;
46import { ElasticDataTypesEnum } from '../../../../../../shared/enums/elastic-data-types.enum' ;
@@ -14,7 +16,7 @@ import {AlertUpdateTagBehavior} from '../../../behavior/alert-update-tag.behavio
1416 templateUrl : './alert-generic-filter.component.html' ,
1517 styleUrls : [ './alert-generic-filter.component.scss' ]
1618} )
17- export class AlertGenericFilterComponent implements OnInit {
19+ export class AlertGenericFilterComponent implements OnInit , OnDestroy {
1820 @Output ( ) filterGenericChange = new EventEmitter < ElasticFilterType > ( ) ;
1921 @Input ( ) fieldFilter : UtmFieldType ;
2022 activeFilters : ElasticFilterType [ ] = [ ] ;
@@ -27,6 +29,7 @@ export class AlertGenericFilterComponent implements OnInit {
2729 top = 6 ;
2830 filter : ElasticFilterType ;
2931 sort : { orderByCount : boolean , sortAsc : boolean } = { orderByCount : true , sortAsc : false } ;
32+ destroy$ : Subject < void > = new Subject < void > ( ) ;
3033
3134 constructor ( private elasticSearchIndexService : ElasticSearchIndexService ,
3235 private alertFiltersBehavior : AlertFiltersBehavior ,
@@ -39,7 +42,9 @@ export class AlertGenericFilterComponent implements OnInit {
3942 * If filter is tags subscribe to changes to reload data on add new tag on alert
4043 */
4144 if ( this . fieldFilter . field === ALERT_TAGS_FIELD ) {
42- this . alertUpdateTagBehavior . $tagRefresh . subscribe ( tagUpdate => {
45+ this . alertUpdateTagBehavior . $tagRefresh
46+ . pipe ( takeUntil ( this . destroy$ ) )
47+ . subscribe ( tagUpdate => {
4348 if ( tagUpdate ) {
4449 this . getFieldValues ( ) ;
4550 }
@@ -48,12 +53,16 @@ export class AlertGenericFilterComponent implements OnInit {
4853 /**
4954 * Reset all values of selected filter
5055 */
51- this . alertFiltersBehavior . $resetFilter . subscribe ( reset => {
56+ this . alertFiltersBehavior . $resetFilter
57+ . pipe ( takeUntil ( this . destroy$ ) )
58+ . subscribe ( reset => {
5259 if ( reset ) {
5360 this . selected = [ ] ;
5461 }
5562 } ) ;
56- this . alertFiltersBehavior . $deleteFilterValue . subscribe ( deleteFilter => {
63+ this . alertFiltersBehavior . $deleteFilterValue
64+ . pipe ( takeUntil ( this . destroy$ ) )
65+ . subscribe ( deleteFilter => {
5766 if ( deleteFilter ) {
5867 const deleteField = deleteFilter . field . replace ( '.keyword' , '' ) ;
5968 if ( this . fieldFilter . field === deleteField ) {
@@ -64,7 +73,9 @@ export class AlertGenericFilterComponent implements OnInit {
6473 }
6574 }
6675 } ) ;
67- this . alertFiltersBehavior . $filters . subscribe ( ( filters : ElasticFilterType [ ] ) => {
76+ this . alertFiltersBehavior . $filters
77+ . pipe ( takeUntil ( this . destroy$ ) )
78+ . subscribe ( ( filters : ElasticFilterType [ ] ) => {
6879 if ( filters ) {
6980 this . activeFilters = filters ;
7081 this . getFieldValues ( ) ;
@@ -160,4 +171,9 @@ export class AlertGenericFilterComponent implements OnInit {
160171 this . sort = $event ;
161172 this . getFieldValues ( ) ;
162173 }
174+
175+ ngOnDestroy ( ) {
176+ this . destroy$ . next ( ) ;
177+ this . destroy$ . complete ( ) ;
178+ }
163179}
0 commit comments