Skip to content

[Draft] [Port to dspace-8_x] Prevent double rendering dynamic themes #4229

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 5 commits into
base: dspace-8_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
15 changes: 1 addition & 14 deletions src/app/shared/theme-support/theme.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import { TestBed } from '@angular/core/testing';
import { ROOT_EFFECTS_INIT } from '@ngrx/effects';
import { provideMockActions } from '@ngrx/effects/testing';
import { provideMockStore } from '@ngrx/store/testing';
import {
cold,
hot,
} from 'jasmine-marbles';
import { hot } from 'jasmine-marbles';

import { SetThemeAction } from './theme.actions';
import { BASE_THEME_NAME } from './theme.constants';
import { ThemeEffects } from './theme.effects';

describe('ThemeEffects', () => {
Expand Down Expand Up @@ -46,13 +41,5 @@ describe('ThemeEffects', () => {
}),
);
});

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

expect(themeEffects.initTheme$).toBeObservable(expected);
});
});
});
26 changes: 2 additions & 24 deletions src/app/shared/theme-support/theme.effects.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
import { Injectable } from '@angular/core';
import {
Actions,
createEffect,
ofType,
ROOT_EFFECTS_INIT,
} from '@ngrx/effects';
import { map } from 'rxjs/operators';
import { Actions } from '@ngrx/effects';

import { getDefaultThemeConfig } from '../../../config/config.util';
import { hasValue } from '../empty.util';
import { SetThemeAction } from './theme.actions';
import { BASE_THEME_NAME } from './theme.constants';

@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 @@ -383,6 +383,13 @@
take(1),
map((theme: Theme) => this.getActionForMatch(theme, currentTheme)),
);
} else if (hasNoValue(currentTheme)) {
const defaultThemeConfig = getDefaultThemeConfig();

Check warning on line 387 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#L387

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

Check warning on line 389 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#L389

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

Check warning on line 391 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#L391

Added line #L391 was not covered by tests
}
} else {
// If there are no themes configured, do nothing
return observableOf(new NoOpAction());
Expand Down
11 changes: 7 additions & 4 deletions src/app/shared/theme-support/themed.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { GenericConstructor } from '../../core/shared/generic-constructor';
import {
hasNoValue,
hasValue,
hasValueOperator,
isNotEmpty,
} from '../empty.util';
import { BASE_THEME_NAME } from './theme.constants';
Expand Down Expand Up @@ -71,6 +72,7 @@ export abstract class ThemedComponent<T extends object> implements AfterViewInit
protected abstract getComponentName(): string;

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

protected abstract importUnthemedComponent(): Promise<any>;

ngOnChanges(changes: SimpleChanges): void {
Expand Down Expand Up @@ -99,16 +101,17 @@ export abstract class ThemedComponent<T extends object> implements AfterViewInit
}

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