Skip to content

[Draft] [Port to dspace-7_x] Prevent double rendering dynamic themes #4230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: dspace-7_x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/app/shared/theme-support/theme.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ThemeEffects } from './theme.effects';
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles';
import { hot } from 'jasmine-marbles';
import { ROOT_EFFECTS_INIT } from '@ngrx/effects';
import { SetThemeAction } from './theme.actions';
import { provideMockStore } from '@ngrx/store/testing';
import { BASE_THEME_NAME } from './theme.constants';

describe('ThemeEffects', () => {
let themeEffects: ThemeEffects;
Expand Down Expand Up @@ -42,13 +40,5 @@ describe('ThemeEffects', () => {
})
);
});

it('should set the default theme', () => {
const expected = cold('--b-', {
b: new SetThemeAction(BASE_THEME_NAME)
});

expect(themeEffects.initTheme$).toBeObservable(expected);
});
});
});
21 changes: 2 additions & 19 deletions src/app/shared/theme-support/theme.effects.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import { Injectable } from '@angular/core';
import { createEffect, Actions, ofType, ROOT_EFFECTS_INIT } from '@ngrx/effects';
import { map } from 'rxjs/operators';
import { SetThemeAction } from './theme.actions';
import { hasValue } from '../empty.util';
import { BASE_THEME_NAME } from './theme.constants';
import { getDefaultThemeConfig } from '../../../config/config.util';
import { Actions } from '@ngrx/effects';

@Injectable()
export class ThemeEffects {
/**
* Initialize with a theme that doesn't depend on the route.
*/
initTheme$ = createEffect(() =>
this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
map(() => {
const defaultThemeConfig = getDefaultThemeConfig();
if (hasValue(defaultThemeConfig)) {
return new SetThemeAction(defaultThemeConfig.name);
} else {
return new SetThemeAction(BASE_THEME_NAME);
}
})
)
);


constructor(
private actions$: Actions,
Expand Down
7 changes: 7 additions & 0 deletions src/app/shared/theme-support/theme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@
take(1),
map((theme: Theme) => this.getActionForMatch(theme, currentTheme))
);
} else if (hasNoValue(currentTheme)) {
const defaultThemeConfig = getDefaultThemeConfig();

Check warning on line 338 in src/app/shared/theme-support/theme.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L338 was not covered by tests
if (hasValue(defaultThemeConfig)) {
return [new SetThemeAction(defaultThemeConfig.name)];

Check warning on line 340 in src/app/shared/theme-support/theme.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L340 was not covered by tests
} else {
return [new SetThemeAction(BASE_THEME_NAME)];

Check warning on line 342 in src/app/shared/theme-support/theme.service.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L342 was not covered by tests
}
} else {
// If there are no themes configured, do nothing
return observableOf(new NoOpAction());
Expand Down
17 changes: 12 additions & 5 deletions src/app/shared/theme-support/themed.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import {
HostBinding,
ElementRef,
} from '@angular/core';
import { hasNoValue, hasValue, isNotEmpty } from '../empty.util';
import { combineLatest, from as fromPromise, Observable, of as observableOf, Subscription, BehaviorSubject } from 'rxjs';
import { ThemeService } from './theme.service';
import { catchError, switchMap, map, tap } from 'rxjs/operators';
import { GenericConstructor } from '../../core/shared/generic-constructor';
import {
hasNoValue,
hasValue,
hasValueOperator,
isNotEmpty,
} from '../empty.util';
import { BASE_THEME_NAME } from './theme.constants';

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

protected abstract importThemedComponent(themeName: string): Promise<any>;

protected abstract importUnthemedComponent(): Promise<any>;

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

initComponentInstance(changes?: SimpleChanges) {
this.themeSub = this.themeService?.getThemeName$().subscribe(() => {
this.renderComponentInstance(changes);
});
this.themeSub = this.themeService?.getThemeName$()
.pipe(hasValueOperator())
.subscribe(() => {
this.renderComponentInstance(changes);
});
}

protected renderComponentInstance(changes?: SimpleChanges): void {
if (hasValue(this.lazyLoadSub)) {
this.lazyLoadSub.unsubscribe();
}

if (hasNoValue(this.lazyLoadObs)) {
this.lazyLoadObs = combineLatest([
observableOf(changes),
Expand Down
Loading