Skip to content

Commit c02ca9a

Browse files
committed
129694: PoC #4123 solution
1 parent 3a04ea8 commit c02ca9a

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

src/app/shared/theme-support/theme.effects.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
import { Injectable } from '@angular/core';
2-
import { createEffect, Actions, ofType, ROOT_EFFECTS_INIT } from '@ngrx/effects';
3-
import { map } from 'rxjs/operators';
4-
import { SetThemeAction } from './theme.actions';
5-
import { hasValue } from '../empty.util';
6-
import { BASE_THEME_NAME } from './theme.constants';
7-
import { getDefaultThemeConfig } from '../../../config/config.util';
2+
import { Actions } from '@ngrx/effects';
83

94
@Injectable()
105
export class ThemeEffects {
116
/**
127
* Initialize with a theme that doesn't depend on the route.
138
*/
14-
initTheme$ = createEffect(() =>
15-
this.actions$.pipe(
16-
ofType(ROOT_EFFECTS_INIT),
17-
map(() => {
18-
const defaultThemeConfig = getDefaultThemeConfig();
19-
if (hasValue(defaultThemeConfig)) {
20-
return new SetThemeAction(defaultThemeConfig.name);
21-
} else {
22-
return new SetThemeAction(BASE_THEME_NAME);
23-
}
24-
})
25-
)
26-
);
9+
2710

2811
constructor(
2912
private actions$: Actions,

src/app/shared/theme-support/theme.service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ export class ThemeService {
334334
take(1),
335335
map((theme: Theme) => this.getActionForMatch(theme, currentTheme))
336336
);
337+
} else if (hasNoValue(currentTheme)) {
338+
const defaultThemeConfig = getDefaultThemeConfig();
339+
if (hasValue(defaultThemeConfig)) {
340+
return [new SetThemeAction(defaultThemeConfig.name)];
341+
} else {
342+
return [new SetThemeAction(BASE_THEME_NAME)];
343+
}
337344
} else {
338345
// If there are no themes configured, do nothing
339346
return observableOf(new NoOpAction());

src/app/shared/theme-support/themed.component.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ import {
1111
HostBinding,
1212
ElementRef,
1313
} from '@angular/core';
14-
import { hasNoValue, hasValue, isNotEmpty } from '../empty.util';
1514
import { combineLatest, from as fromPromise, Observable, of as observableOf, Subscription, BehaviorSubject } from 'rxjs';
1615
import { ThemeService } from './theme.service';
1716
import { catchError, switchMap, map, tap } from 'rxjs/operators';
1817
import { GenericConstructor } from '../../core/shared/generic-constructor';
18+
import {
19+
hasNoValue,
20+
hasValue,
21+
hasValueOperator,
22+
isNotEmpty,
23+
} from '../empty.util';
1924
import { BASE_THEME_NAME } from './theme.constants';
2025

2126
@Component({
@@ -54,6 +59,7 @@ export abstract class ThemedComponent<T> implements AfterViewInit, OnDestroy, On
5459
protected abstract getComponentName(): string;
5560

5661
protected abstract importThemedComponent(themeName: string): Promise<any>;
62+
5763
protected abstract importUnthemedComponent(): Promise<any>;
5864

5965
ngOnChanges(changes: SimpleChanges): void {
@@ -82,16 +88,17 @@ export abstract class ThemedComponent<T> implements AfterViewInit, OnDestroy, On
8288
}
8389

8490
initComponentInstance(changes?: SimpleChanges) {
85-
this.themeSub = this.themeService?.getThemeName$().subscribe(() => {
86-
this.renderComponentInstance(changes);
87-
});
91+
this.themeSub = this.themeService?.getThemeName$()
92+
.pipe(hasValueOperator())
93+
.subscribe(() => {
94+
this.renderComponentInstance(changes);
95+
});
8896
}
8997

9098
protected renderComponentInstance(changes?: SimpleChanges): void {
9199
if (hasValue(this.lazyLoadSub)) {
92100
this.lazyLoadSub.unsubscribe();
93101
}
94-
95102
if (hasNoValue(this.lazyLoadObs)) {
96103
this.lazyLoadObs = combineLatest([
97104
observableOf(changes),

0 commit comments

Comments
 (0)