1
- import { Component , EventEmitter , Input , OnInit , Output } from '@angular/core' ;
1
+ import {
2
+ AfterViewChecked ,
3
+ AfterViewInit ,
4
+ Component ,
5
+ EventEmitter ,
6
+ Input ,
7
+ OnChanges ,
8
+ OnDestroy ,
9
+ OnInit ,
10
+ Output ,
11
+ SimpleChanges
12
+ } from '@angular/core' ;
2
13
import { IFilterField , IFilters } from './interfaces/filters.interface' ;
3
- import { Observable } from 'rxjs' ;
14
+ import { BehaviorSubject , Observable , Subscription } from 'rxjs' ;
4
15
import { FormArray , FormBuilder , FormControl , FormGroup } from '@angular/forms' ;
16
+ import { filter } from 'rxjs/operators' ;
5
17
6
18
@Component ( {
7
19
selector : 't4e-filters' ,
8
20
templateUrl : './filters.component.html' ,
9
21
styleUrls : [ './filters.component.scss' ]
10
22
} )
11
- export class FiltersComponent implements OnInit {
23
+ export class FiltersComponent implements OnInit , OnChanges , OnDestroy {
12
24
13
25
@Input ( ) title : string ;
14
26
@Input ( ) fields : Observable < IFilters > ;
15
- @Input ( ) initialFilters : string [ ] = [ ] ;
27
+ @Input ( ) selectedFilters : BehaviorSubject < string [ ] > ;
16
28
@Output ( ) onSelectFields : EventEmitter < string [ ] > = new EventEmitter < string [ ] > ( ) ;
17
29
30
+ selectedFiltersSubscription : Subscription ;
31
+
18
32
filterForm : FormGroup
19
33
filterFields : IFilterField [ ] ;
20
34
@@ -34,9 +48,7 @@ export class FiltersComponent implements OnInit {
34
48
35
49
result . filterFields . forEach ( ( field , index ) => {
36
50
let state = '' ;
37
- console . log ( 'initial filters' , this . initialFilters )
38
- console . log ( 'field value' , field . value )
39
- if ( this . initialFilters . length > 0 && this . initialFilters . includes ( field . value ) ) {
51
+ if ( this . selectedFilters && this . selectedFilters . getValue ( ) . length > 0 && this . selectedFilters . getValue ( ) . includes ( field . value ) ) {
40
52
state = field . value ;
41
53
}
42
54
( this . filterForm . controls . filters as FormArray ) . push (
@@ -52,7 +64,6 @@ export class FiltersComponent implements OnInit {
52
64
53
65
this . onSelectFields . emit ( result )
54
66
} )
55
-
56
67
}
57
68
58
69
toggleFieldValue ( index : number , newValue : string ) {
@@ -76,4 +87,23 @@ export class FiltersComponent implements OnInit {
76
87
return ( this . filterForm . get ( 'filters' ) as FormArray ) . controls
77
88
}
78
89
90
+ ngOnChanges ( changes : SimpleChanges ) : void {
91
+ if ( this . selectedFilters && ! this . selectedFiltersSubscription ) {
92
+ this . selectedFiltersSubscription = this . selectedFilters . subscribe ( ( result ) => {
93
+ for ( let filterControl of ( this . filterForm . get ( 'filters' ) as FormArray ) . controls ) {
94
+ if ( ! result . includes ( filterControl . value ) ) {
95
+ filterControl . setValue ( "" ) ;
96
+ }
97
+ }
98
+ } )
99
+ }
100
+
101
+ }
102
+
103
+ ngOnDestroy ( ) : void {
104
+ if ( this . selectedFiltersSubscription ) {
105
+ this . selectedFiltersSubscription . unsubscribe ( ) ;
106
+ }
107
+ }
108
+
79
109
}
0 commit comments