11import { 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' ;
34import {
45 Attribute ,
56 AttributesManagerService ,
@@ -14,11 +15,13 @@ import {
1415import { StoreService , NotificatorService } from '@perun-web-apps/perun/services' ;
1516import { compareFnName } from '@perun-web-apps/perun/utils' ;
1617import { TranslateService } from '@ngx-translate/core' ;
18+ import { filter , Subscription } from 'rxjs' ;
1719
1820@Component ( {
1921 selector : 'perun-web-apps-mailing-lists' ,
2022 templateUrl : './mailing-lists.component.html' ,
2123 styleUrls : [ './mailing-lists.component.scss' ] ,
24+ providers : [ Location , { provide : LocationStrategy , useClass : PathLocationStrategy } ] ,
2225} )
2326export class MailingListsComponent implements OnInit , OnDestroy {
2427 @Input ( ) user : User ;
@@ -33,6 +36,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
3336 selectedVo : string = null ;
3437 selectedResource : string = null ;
3538 changeOptOut : string ;
39+ routingSubscription : Subscription ;
3640
3741 constructor (
3842 private store : StoreService ,
@@ -43,18 +47,18 @@ export class MailingListsComponent implements OnInit, OnDestroy {
4347 private route : ActivatedRoute ,
4448 private router : Router ,
4549 private notificator : NotificatorService ,
46- private translate : TranslateService
50+ private translate : TranslateService ,
51+ private location : Location
4752 ) { }
4853
4954 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+ } ) ;
5862 }
5963
6064 ngOnInit ( ) : void {
@@ -82,6 +86,17 @@ export class MailingListsComponent implements OnInit, OnDestroy {
8286 } ) ;
8387 } )
8488 . 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+ } ) ;
85100 }
86101
87102 changeSelectedResource ( resource : RichResource ) : void {
@@ -99,6 +114,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
99114 }
100115 void this . router . navigate ( [ ] , {
101116 relativeTo : this . route ,
117+ replaceUrl : true ,
102118 queryParams : { vo : this . selectedVo , resource : this . selectedResource , action : null } ,
103119 queryParamsHandling : 'merge' ,
104120 } ) ;
@@ -113,6 +129,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
113129 if ( ! this . isService ) {
114130 void this . router . navigate ( [ ] , {
115131 relativeTo : this . route ,
132+ replaceUrl : true ,
116133 queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
117134 queryParamsHandling : 'merge' ,
118135 } ) ;
@@ -217,9 +234,9 @@ export class MailingListsComponent implements OnInit, OnDestroy {
217234 }
218235 }
219236
220- applyFilter ( filter : string ) : void {
237+ applyFilter ( mailingListsFilter : string ) : void {
221238 this . filteredVos = this . vos . filter ( ( vo ) =>
222- vo . name . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
239+ vo . name . toLowerCase ( ) . includes ( mailingListsFilter . toLowerCase ( ) )
223240 ) ;
224241 }
225242
@@ -230,6 +247,7 @@ export class MailingListsComponent implements OnInit, OnDestroy {
230247 if ( ! this . isService ) {
231248 void this . router . navigate ( [ ] , {
232249 relativeTo : this . route ,
250+ replaceUrl : true ,
233251 queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
234252 queryParamsHandling : 'merge' ,
235253 } ) ;
@@ -241,9 +259,16 @@ export class MailingListsComponent implements OnInit, OnDestroy {
241259 if ( ! this . isService ) {
242260 void this . router . navigate ( [ ] , {
243261 relativeTo : this . route ,
262+ replaceUrl : true ,
244263 queryParams : { vo : this . selectedVo , resource : this . selectedResource } ,
245264 queryParamsHandling : 'merge' ,
246265 } ) ;
247266 }
248267 }
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+ }
249274}
0 commit comments