Skip to content

Commit 73fc296

Browse files
committed
refactor: move ColorModeService setup from default-header to app component
1 parent fdb0b4a commit 73fc296

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

src/app/app.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { TestBed } from '@angular/core/testing';
2-
import { RouterTestingModule } from '@angular/router/testing';
2+
import { provideRouter } from '@angular/router';
33
import { AppComponent } from './app.component';
44

55
describe('AppComponent', () => {
66
beforeEach(async () => {
77
await TestBed.configureTestingModule({
8-
imports: [
9-
RouterTestingModule,
8+
imports: [
109
AppComponent
11-
],
12-
}).compileComponents();
10+
],
11+
providers: [provideRouter([])]
12+
}).compileComponents();
1313
});
1414

1515
it('should create the app', () => {

src/app/app.component.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
1+
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
2+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
33
import { Title } from '@angular/platform-browser';
4+
import { ActivatedRoute, NavigationEnd, Router, RouterOutlet } from '@angular/router';
5+
import { delay, filter, map, tap } from 'rxjs/operators';
46

7+
import { ColorModeService } from '@coreui/angular';
58
import { IconSetService } from '@coreui/icons-angular';
69
import { iconSubset } from './icons/icon-subset';
710

@@ -14,21 +17,42 @@ import { iconSubset } from './icons/icon-subset';
1417
export class AppComponent implements OnInit {
1518
title = 'CoreUI Angular Admin Template';
1619

17-
constructor(
18-
private router: Router,
19-
private titleService: Title,
20-
private iconSetService: IconSetService
21-
) {
22-
this.titleService.setTitle(this.title);
20+
readonly #destroyRef: DestroyRef = inject(DestroyRef);
21+
readonly #activatedRoute: ActivatedRoute = inject(ActivatedRoute);
22+
readonly #router = inject(Router);
23+
readonly #titleService = inject(Title);
24+
25+
readonly #colorModeService = inject(ColorModeService);
26+
readonly #iconSetService = inject(IconSetService);
27+
28+
constructor() {
29+
this.#titleService.setTitle(this.title);
2330
// iconSet singleton
24-
this.iconSetService.icons = { ...iconSubset };
31+
this.#iconSetService.icons = { ...iconSubset };
32+
this.#colorModeService.localStorageItemName.set('coreui-free-angular-admin-template-theme-default');
33+
this.#colorModeService.eventName.set('ColorSchemeChange');
2534
}
2635

2736
ngOnInit(): void {
28-
this.router.events.subscribe((evt) => {
37+
38+
this.#router.events.pipe(
39+
takeUntilDestroyed(this.#destroyRef)
40+
).subscribe((evt) => {
2941
if (!(evt instanceof NavigationEnd)) {
3042
return;
3143
}
3244
});
45+
46+
this.#activatedRoute.queryParams
47+
.pipe(
48+
delay(1),
49+
map(params => <string>params['theme']?.match(/^[A-Za-z0-9\s]+/)?.[0]),
50+
filter(theme => ['dark', 'light', 'auto'].includes(theme)),
51+
tap(theme => {
52+
this.#colorModeService.colorMode.set(theme);
53+
}),
54+
takeUntilDestroyed(this.#destroyRef)
55+
)
56+
.subscribe();
3357
}
3458
}

src/app/layout/default-layout/default-header/default-header.component.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<ng-container>
33
<c-container [fluid]="true" class="border-bottom px-4">
44
<button
5-
[cSidebarToggle]="sidebarId"
5+
[cSidebarToggle]="sidebarId()"
66
cHeaderToggler
77
class="btn"
88
toggle="visible"
@@ -169,7 +169,6 @@ <h6 cDropdownHeader class="bg-body-secondary text-body-secondary fw-semibold py-
169169
<button
170170
(click)="colorMode.set(mode.name)"
171171
[active]="colorMode()===mode.name"
172-
[routerLink]="[]"
173172
cDropdownItem
174173
class="d-flex align-items-center"
175174
>

src/app/layout/default-layout/default-header/default-header.component.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Component, computed, DestroyRef, inject, Input } from '@angular/core';
1+
import { NgStyle, NgTemplateOutlet } from '@angular/common';
2+
import { Component, computed, inject, input } from '@angular/core';
3+
import { RouterLink, RouterLinkActive } from '@angular/router';
4+
25
import {
36
AvatarComponent,
47
BadgeComponent,
@@ -22,11 +25,8 @@ import {
2225
TextColorDirective,
2326
ThemeDirective
2427
} from '@coreui/angular';
25-
import { NgStyle, NgTemplateOutlet } from '@angular/common';
26-
import { ActivatedRoute, RouterLink, RouterLinkActive } from '@angular/router';
28+
2729
import { IconDirective } from '@coreui/icons-angular';
28-
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
29-
import { delay, filter, map, tap } from 'rxjs/operators';
3030

3131
@Component({
3232
selector: 'app-default-header',
@@ -36,10 +36,8 @@ import { delay, filter, map, tap } from 'rxjs/operators';
3636
})
3737
export class DefaultHeaderComponent extends HeaderComponent {
3838

39-
readonly #activatedRoute: ActivatedRoute = inject(ActivatedRoute);
4039
readonly #colorModeService = inject(ColorModeService);
4140
readonly colorMode = this.#colorModeService.colorMode;
42-
readonly #destroyRef: DestroyRef = inject(DestroyRef);
4341

4442
readonly colorModes = [
4543
{ name: 'light', text: 'Light', icon: 'cilSun' },
@@ -49,28 +47,14 @@ export class DefaultHeaderComponent extends HeaderComponent {
4947

5048
readonly icons = computed(() => {
5149
const currentMode = this.colorMode();
52-
return this.colorModes.find(mode=> mode.name === currentMode)?.icon ?? 'cilSun';
50+
return this.colorModes.find(mode => mode.name === currentMode)?.icon ?? 'cilSun';
5351
});
5452

5553
constructor() {
5654
super();
57-
this.#colorModeService.localStorageItemName.set('coreui-free-angular-admin-template-theme-default');
58-
this.#colorModeService.eventName.set('ColorSchemeChange');
59-
60-
this.#activatedRoute.queryParams
61-
.pipe(
62-
delay(1),
63-
map(params => <string>params['theme']?.match(/^[A-Za-z0-9\s]+/)?.[0]),
64-
filter(theme => ['dark', 'light', 'auto'].includes(theme)),
65-
tap(theme => {
66-
this.colorMode.set(theme);
67-
}),
68-
takeUntilDestroyed(this.#destroyRef)
69-
)
70-
.subscribe();
7155
}
7256

73-
@Input() sidebarId: string = 'sidebar1';
57+
sidebarId = input('sidebar1');
7458

7559
public newMessages = [
7660
{

0 commit comments

Comments
 (0)