1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import { ChangeDetectionStrategy , Component , OnInit } from '@angular/core' ;
16+ import {
17+ ChangeDetectionStrategy ,
18+ Component ,
19+ OnDestroy ,
20+ OnInit ,
21+ } from '@angular/core' ;
1722import { MatToolbarModule } from '@angular/material/toolbar' ;
1823import { CommonModule } from '@angular/common' ;
1924import { MatTabChangeEvent , MatTabsModule } from '@angular/material/tabs' ;
20- import { ActivatedRoute , Router , RouterModule } from '@angular/router' ;
25+ import {
26+ ActivatedRoute ,
27+ NavigationEnd ,
28+ Router ,
29+ RouterModule ,
30+ } from '@angular/router' ;
2131import { Routes } from '../../model/routes' ;
32+ import { filter , Subject , takeUntil } from 'rxjs' ;
2233
2334@Component ( {
2435 selector : 'app-settings' ,
@@ -27,8 +38,9 @@ import { Routes } from '../../model/routes';
2738 styleUrls : [ './settings.component.scss' ] ,
2839 changeDetection : ChangeDetectionStrategy . OnPush ,
2940} )
30- export class SettingsComponent implements OnInit {
41+ export class SettingsComponent implements OnInit , OnDestroy {
3142 private routes = [ Routes . General , Routes . Certificates ] ;
43+ private destroy$ : Subject < boolean > = new Subject < boolean > ( ) ;
3244 selectedIndex = 0 ;
3345 constructor (
3446 private router : Router ,
@@ -37,13 +49,33 @@ export class SettingsComponent implements OnInit {
3749
3850 ngOnInit ( ) : void {
3951 const currentRoute = this . router . url ;
40- this . selectedIndex = this . routes . findIndex ( route =>
41- currentRoute . includes ( route )
42- ) ;
52+ this . setSelectedIndex ( currentRoute ) ;
53+
54+ this . router . events
55+ . pipe (
56+ takeUntil ( this . destroy$ ) ,
57+ filter ( event => event instanceof NavigationEnd )
58+ )
59+ . subscribe ( event => {
60+ if ( event . url !== currentRoute ) {
61+ this . setSelectedIndex ( event . url ) ;
62+ }
63+ } ) ;
4364 }
4465
4566 onTabChange ( event : MatTabChangeEvent ) : void {
4667 const index = event . index ;
4768 this . router . navigate ( [ this . routes [ index ] ] , { relativeTo : this . route } ) ;
4869 }
70+
71+ private setSelectedIndex ( currentRoute : string ) : void {
72+ this . selectedIndex = this . routes . findIndex ( route =>
73+ currentRoute . includes ( route )
74+ ) ;
75+ }
76+
77+ ngOnDestroy ( ) {
78+ this . destroy$ . next ( true ) ;
79+ this . destroy$ . unsubscribe ( ) ;
80+ }
4981}
0 commit comments