1
1
import { Component , Input , OnInit , OnDestroy } from '@angular/core' ;
2
- import { ActivatedRoute , Router } from '@angular/router' ;
2
+ import { Location , LocationStrategy , PathLocationStrategy } from '@angular/common' ;
3
+ import { ActivatedRoute , NavigationStart , Router } from '@angular/router' ;
3
4
import {
4
5
Attribute ,
5
6
AttributesManagerService ,
@@ -14,11 +15,13 @@ import {
14
15
import { StoreService , NotificatorService } from '@perun-web-apps/perun/services' ;
15
16
import { compareFnName } from '@perun-web-apps/perun/utils' ;
16
17
import { TranslateService } from '@ngx-translate/core' ;
18
+ import { filter , Subscription } from 'rxjs' ;
17
19
18
20
@Component ( {
19
21
selector : 'perun-web-apps-mailing-lists' ,
20
22
templateUrl : './mailing-lists.component.html' ,
21
23
styleUrls : [ './mailing-lists.component.scss' ] ,
24
+ providers : [ Location , { provide : LocationStrategy , useClass : PathLocationStrategy } ] ,
22
25
} )
23
26
export class MailingListsComponent implements OnInit , OnDestroy {
24
27
@Input ( ) user : User ;
@@ -33,6 +36,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
33
36
selectedVo : string = null ;
34
37
selectedResource : string = null ;
35
38
changeOptOut : string ;
39
+ routingSubscription : Subscription ;
36
40
37
41
constructor (
38
42
private store : StoreService ,
@@ -43,18 +47,18 @@ export class MailingListsComponent implements OnInit, OnDestroy {
43
47
private route : ActivatedRoute ,
44
48
private router : Router ,
45
49
private notificator : NotificatorService ,
46
- private translate : TranslateService
50
+ private translate : TranslateService ,
51
+ private location : Location
47
52
) { }
48
53
49
54
ngOnDestroy ( ) : void {
50
- if ( ! this . isService ) {
51
- void this . router . navigate ( [ ] , {
52
- relativeTo : this . route ,
53
- queryParams : { vo : null , resource : null } ,
54
- replaceUrl : true ,
55
- queryParamsHandling : 'merge' ,
56
- } ) ;
57
- }
55
+ this . routingSubscription . unsubscribe ( ) ;
56
+ // clear the query params in the new component
57
+ void this . router . navigate ( [ location . pathname ] , {
58
+ replaceUrl : true ,
59
+ queryParams : { vo : null , resource : null } ,
60
+ queryParamsHandling : 'merge' ,
61
+ } ) ;
58
62
}
59
63
60
64
ngOnInit ( ) : void {
@@ -82,6 +86,17 @@ export class MailingListsComponent implements OnInit, OnDestroy {
82
86
} ) ;
83
87
} )
84
88
. unsubscribe ( ) ;
89
+ this . routingSubscription = this . router . events
90
+ . pipe ( filter ( ( event ) : event is NavigationStart => event instanceof NavigationStart ) )
91
+ . subscribe ( ( event ) => {
92
+ if ( ! event . url . startsWith ( location . pathname ) ) {
93
+ // clear the query params when navigating away
94
+ this . location . replaceState (
95
+ location . pathname ,
96
+ this . clearParamsFromCurrUrl ( [ 'vo' , 'resource' ] )
97
+ ) ;
98
+ }
99
+ } ) ;
85
100
}
86
101
87
102
changeSelectedResource ( resource : RichResource ) : void {
@@ -99,6 +114,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
99
114
}
100
115
void this . router . navigate ( [ ] , {
101
116
relativeTo : this . route ,
117
+ replaceUrl : true ,
102
118
queryParams : { vo : this . selectedVo , resource : this . selectedResource , action : null } ,
103
119
queryParamsHandling : 'merge' ,
104
120
} ) ;
@@ -113,6 +129,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
113
129
if ( ! this . isService ) {
114
130
void this . router . navigate ( [ ] , {
115
131
relativeTo : this . route ,
132
+ replaceUrl : true ,
116
133
queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
117
134
queryParamsHandling : 'merge' ,
118
135
} ) ;
@@ -217,9 +234,9 @@ export class MailingListsComponent implements OnInit, OnDestroy {
217
234
}
218
235
}
219
236
220
- applyFilter ( filter : string ) : void {
237
+ applyFilter ( mailingListsFilter : string ) : void {
221
238
this . filteredVos = this . vos . filter ( ( vo ) =>
222
- vo . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
239
+ vo . name . toLowerCase ( ) . includes ( mailingListsFilter . toLowerCase ( ) )
223
240
) ;
224
241
}
225
242
@@ -230,6 +247,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
230
247
if ( ! this . isService ) {
231
248
void this . router . navigate ( [ ] , {
232
249
relativeTo : this . route ,
250
+ replaceUrl : true ,
233
251
queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
234
252
queryParamsHandling : 'merge' ,
235
253
} ) ;
@@ -241,9 +259,16 @@ export class MailingListsComponent implements OnInit, OnDestroy {
241
259
if ( ! this . isService ) {
242
260
void this . router . navigate ( [ ] , {
243
261
relativeTo : this . route ,
262
+ replaceUrl : true ,
244
263
queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
245
264
queryParamsHandling : 'merge' ,
246
265
} ) ;
247
266
}
248
267
}
268
+
269
+ clearParamsFromCurrUrl ( paramsToClear : string [ ] ) : string {
270
+ const searchParams = new URLSearchParams ( location . search ) ;
271
+ paramsToClear . forEach ( ( param ) => searchParams . delete ( param ) ) ;
272
+ return searchParams . toString ( ) ;
273
+ }
249
274
}
0 commit comments