Skip to content

Commit 3e0b111

Browse files
authored
feat(profile-settings): Add query-param to specify a tab (#906)
1 parent aaeb9cb commit 3e0b111

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/app/features/settings/profile-settings/profile-settings.component.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BehaviorSubject } from 'rxjs';
55

66
import { ComponentFixture, TestBed } from '@angular/core/testing';
77
import { By } from '@angular/platform-browser';
8+
import { ActivatedRoute } from '@angular/router';
89

910
import { SelectComponent } from '@osf/shared/components/select/select.component';
1011
import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component';
@@ -34,7 +35,11 @@ describe('ProfileSettingsComponent', () => {
3435
SelectComponent
3536
),
3637
],
37-
providers: [MockProvider(IS_MEDIUM, isMedium), MockProvider(TranslateService)],
38+
providers: [
39+
MockProvider(IS_MEDIUM, isMedium),
40+
MockProvider(TranslateService),
41+
{ provide: ActivatedRoute, useValue: { snapshot: { queryParams: {} } } },
42+
],
3843
}).compileComponents();
3944

4045
fixture = TestBed.createComponent(ProfileSettingsComponent);
@@ -46,6 +51,14 @@ describe('ProfileSettingsComponent', () => {
4651
expect(component).toBeTruthy();
4752
});
4853

54+
it('should update selected tab on init based on query param', () => {
55+
const testTabValue = 2;
56+
component['route'] = { snapshot: { queryParams: { tab: testTabValue } } } as any;
57+
58+
component.ngOnInit();
59+
expect(component['selectedTab']).toBe(testTabValue);
60+
});
61+
4962
it('should update selected tab when onTabChange is called', () => {
5063
const newTabIndex = 2;
5164
component.onTabChange(newTabIndex);

src/app/features/settings/profile-settings/profile-settings.component.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { TranslatePipe } from '@ngx-translate/core';
22

33
import { Tab, TabList, TabPanel, TabPanels, Tabs } from 'primeng/tabs';
44

5-
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
5+
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
66
import { toSignal } from '@angular/core/rxjs-interop';
77
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
8+
import { ActivatedRoute, Router } from '@angular/router';
89

910
import { SelectComponent } from '@osf/shared/components/select/select.component';
1011
import { SubHeaderComponent } from '@osf/shared/components/sub-header/sub-header.component';
@@ -36,14 +37,28 @@ import { ProfileSettingsTabOption } from './enums';
3637
styleUrl: './profile-settings.component.scss',
3738
changeDetection: ChangeDetectionStrategy.OnPush,
3839
})
39-
export class ProfileSettingsComponent {
40+
export class ProfileSettingsComponent implements OnInit {
4041
readonly isMedium = toSignal(inject(IS_MEDIUM));
4142
readonly tabOptions = PROFILE_SETTINGS_TAB_OPTIONS;
4243
readonly tabOption = ProfileSettingsTabOption;
4344

45+
private router = inject(Router);
46+
private route = inject(ActivatedRoute);
4447
selectedTab = this.tabOption.Name;
4548

49+
ngOnInit(): void {
50+
const tabParam = Number(this.route.snapshot.queryParams['tab']);
51+
const selectedTab = tabParam && Object.values(this.tabOption).includes(tabParam) ? tabParam : this.tabOption.Name;
52+
53+
this.selectedTab = selectedTab;
54+
}
55+
4656
onTabChange(index: number): void {
4757
this.selectedTab = index;
58+
this.router.navigate([], {
59+
queryParams: { tab: index },
60+
queryParamsHandling: 'merge',
61+
replaceUrl: true,
62+
});
4863
}
4964
}

0 commit comments

Comments
 (0)