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' ;
2
4
import { ALERT_TAGS_FIELD } from '../../../../../../shared/constants/alert/alert-field.constant' ;
3
5
import { ALERT_INDEX_PATTERN } from '../../../../../../shared/constants/main-index-pattern.constant' ;
4
6
import { ElasticDataTypesEnum } from '../../../../../../shared/enums/elastic-data-types.enum' ;
@@ -14,7 +16,7 @@ import {AlertUpdateTagBehavior} from '../../../behavior/alert-update-tag.behavio
14
16
templateUrl : './alert-generic-filter.component.html' ,
15
17
styleUrls : [ './alert-generic-filter.component.scss' ]
16
18
} )
17
- export class AlertGenericFilterComponent implements OnInit {
19
+ export class AlertGenericFilterComponent implements OnInit , OnDestroy {
18
20
@Output ( ) filterGenericChange = new EventEmitter < ElasticFilterType > ( ) ;
19
21
@Input ( ) fieldFilter : UtmFieldType ;
20
22
activeFilters : ElasticFilterType [ ] = [ ] ;
@@ -27,6 +29,7 @@ export class AlertGenericFilterComponent implements OnInit {
27
29
top = 6 ;
28
30
filter : ElasticFilterType ;
29
31
sort : { orderByCount : boolean , sortAsc : boolean } = { orderByCount : true , sortAsc : false } ;
32
+ destroy$ : Subject < void > = new Subject < void > ( ) ;
30
33
31
34
constructor ( private elasticSearchIndexService : ElasticSearchIndexService ,
32
35
private alertFiltersBehavior : AlertFiltersBehavior ,
@@ -39,7 +42,9 @@ export class AlertGenericFilterComponent implements OnInit {
39
42
* If filter is tags subscribe to changes to reload data on add new tag on alert
40
43
*/
41
44
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 => {
43
48
if ( tagUpdate ) {
44
49
this . getFieldValues ( ) ;
45
50
}
@@ -48,12 +53,16 @@ export class AlertGenericFilterComponent implements OnInit {
48
53
/**
49
54
* Reset all values of selected filter
50
55
*/
51
- this . alertFiltersBehavior . $resetFilter . subscribe ( reset => {
56
+ this . alertFiltersBehavior . $resetFilter
57
+ . pipe ( takeUntil ( this . destroy$ ) )
58
+ . subscribe ( reset => {
52
59
if ( reset ) {
53
60
this . selected = [ ] ;
54
61
}
55
62
} ) ;
56
- this . alertFiltersBehavior . $deleteFilterValue . subscribe ( deleteFilter => {
63
+ this . alertFiltersBehavior . $deleteFilterValue
64
+ . pipe ( takeUntil ( this . destroy$ ) )
65
+ . subscribe ( deleteFilter => {
57
66
if ( deleteFilter ) {
58
67
const deleteField = deleteFilter . field . replace ( '.keyword' , '' ) ;
59
68
if ( this . fieldFilter . field === deleteField ) {
@@ -64,7 +73,9 @@ export class AlertGenericFilterComponent implements OnInit {
64
73
}
65
74
}
66
75
} ) ;
67
- this . alertFiltersBehavior . $filters . subscribe ( ( filters : ElasticFilterType [ ] ) => {
76
+ this . alertFiltersBehavior . $filters
77
+ . pipe ( takeUntil ( this . destroy$ ) )
78
+ . subscribe ( ( filters : ElasticFilterType [ ] ) => {
68
79
if ( filters ) {
69
80
this . activeFilters = filters ;
70
81
this . getFieldValues ( ) ;
@@ -160,4 +171,9 @@ export class AlertGenericFilterComponent implements OnInit {
160
171
this . sort = $event ;
161
172
this . getFieldValues ( ) ;
162
173
}
174
+
175
+ ngOnDestroy ( ) {
176
+ this . destroy$ . next ( ) ;
177
+ this . destroy$ . complete ( ) ;
178
+ }
163
179
}
0 commit comments