fix: Sidans titel ändras nu korrekt när man går tillbaka till startsidan. (TV-643)

Squashed commit of the following:

commit b70383f449eb2e17e0da2e479316c821cdf4bb85
Author: Daniel Appelgren <daniel.appelgren@arbetsformedlingen.se>
Date:   Wed Sep 22 15:04:38 2021 +0200

    fix: Flyttade titlesättning från layout.component till app.component. Krävde mer logik för att hitta vilken route man är på. (TV-643)
This commit is contained in:
Daniel Appelgren
2021-09-23 11:01:12 +02:00
parent 454699b785
commit 0302b1a1ad
2 changed files with 28 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
import { DOCUMENT } from '@angular/common';
import { ChangeDetectionStrategy, Component, Inject } from '@angular/core';
import { environment } from '@msfa-environment';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { filter, map, mergeMap, switchMap } from 'rxjs/operators';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'msfa-root',
@@ -9,7 +12,30 @@ import { environment } from '@msfa-environment';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
constructor(@Inject(DOCUMENT) private document: Document) {
constructor(
@Inject(DOCUMENT) private document: Document,
private router: Router,
private activatedRoute: ActivatedRoute,
private titleService: Title
) {
this.document.body.dataset.version = environment.version;
this.router.events
.pipe(
filter(event => event instanceof NavigationEnd),
map(() => traverseUntilNoChildRoute(this.activatedRoute)),
switchMap((route: ActivatedRoute) => route.data)
)
.subscribe((routeData: { [title: string]: string }) => {
const pageTitle = routeData.title;
this.titleService.setTitle(`${pageTitle ? `${pageTitle} - ` : ''}Mina sidor för fristående aktörer`);
});
}
}
function traverseUntilNoChildRoute(route: ActivatedRoute): ActivatedRoute {
while (route.firstChild) {
route = route.firstChild;
}
return route;
}

View File

@@ -40,17 +40,13 @@ export class LayoutComponent extends UnsubscribeDirective {
private router: Router,
private activatedRoute: ActivatedRoute,
private authenticationService: AuthenticationService,
private userService: UserService,
private titleService: Title
private userService: UserService
) {
super();
super.unsubscribeOnDestroy(
this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => {
const pageTitle = this.activatedRoute?.snapshot?.data?.title as string;
const urlTree = this.router.parseUrl(this.router.url);
this.titleService.setTitle(`${pageTitle ? `${pageTitle} - ` : ''}Mina sidor för fristående aktörer`);
if (urlTree.queryParams.code) {
void this.router.navigate([], {
relativeTo: this.activatedRoute,