From e9307788e1fe0c41f32ae60225b1bf3434dddee2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Fri, 28 Jun 2019 01:38:05 +0200
Subject: [PATCH] Refactor header: rename to short id and use router

---
 .../shared/header/header.component.html       |  2 +-
 .../shared/header/header.component.ts         | 25 +++++++++++++------
 2 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/app/components/shared/header/header.component.html b/src/app/components/shared/header/header.component.html
index c592d71b5..1c76c3b8c 100644
--- a/src/app/components/shared/header/header.component.html
+++ b/src/app/components/shared/header/header.component.html
@@ -12,7 +12,7 @@
     <span class="fill-remaining-space"
           *ngIf="router.url.includes('comments') && user.role === 1 && deviceType === 'desktop'"></span>
     <h3 *ngIf="router.url.includes('comments') && user.role === 1 && deviceType === 'desktop'" fxLayoutAlign="center center">
-      {{'header.id' | translate}}: {{ roomId.slice(0,4) }} {{  roomId.slice(4,8) }}
+      {{'header.id' | translate}}: {{ shortId.slice(0,4) }} {{  shortId.slice(4,8) }}
     </h3>
     <span class="fill-remaining-space"></span>
 
diff --git a/src/app/components/shared/header/header.component.ts b/src/app/components/shared/header/header.component.ts
index 3f5f4a04f..50943e98f 100644
--- a/src/app/components/shared/header/header.component.ts
+++ b/src/app/components/shared/header/header.component.ts
@@ -1,7 +1,7 @@
 import { Component, OnInit } from '@angular/core';
 import { AuthenticationService } from '../../../services/http/authentication.service';
 import { NotificationService } from '../../../services/util/notification.service';
-import { Router } from '@angular/router';
+import { Router, NavigationEnd } from '@angular/router';
 import { User } from '../../../models/user';
 import { UserRole } from '../../../models/user-roles.enum';
 import { Location } from '@angular/common';
@@ -20,7 +20,7 @@ export class HeaderComponent implements OnInit {
   user: User;
   themeClass = localStorage.getItem('theme');
   cTime: string;
-  roomId: string;
+  shortId: string;
   deviceType = localStorage.getItem('deviceType');
 
   constructor(public location: Location,
@@ -44,18 +44,29 @@ export class HeaderComponent implements OnInit {
       this.translationService.setDefaultLang(localStorage.getItem('currentLang'));
     }
     this.authenticationService.watchUser.subscribe(newUser => this.user = newUser);
-    this.getRoomId();
+
     let time = new Date();
     this.getTime(time);
     setInterval(() => {
       time = new Date();
       this.getTime(time);
-      this.getRoomId();
     }, 1000);
-  }
 
-  public getRoomId() {
-    this.roomId = localStorage.getItem('shortId');
+    this.router.events.subscribe(val => {
+      /* the router will fire multiple events */
+      /* we only want to react if it's the final active route */
+      if (val instanceof NavigationEnd) {
+       /* segments gets all parts of the url */
+       const segments = this.router.parseUrl(this.router.url).root.children.primary.segments;
+       const roomIdRegExp = new RegExp('^[0-9]{8}$');
+       segments.forEach(element => {
+         /* searches the url segments for a short id */
+         if (roomIdRegExp.test(element.path)) {
+           this.shortId = element.path;
+         }
+       });
+      }
+    });
   }
 
   getTime(time: Date) {
-- 
GitLab