Skip to content
Snippets Groups Projects
monitor-starter.component.ts 3.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {Component, OnDestroy, OnInit} from '@angular/core';
    
    import {AccessObject, AuthAccessKeyType, AuthData, WorkspaceData} from '../../app.interfaces';
    
    paf's avatar
    paf committed
    import {from, Subscription} from 'rxjs';
    import {Router} from '@angular/router';
    import {BackendService} from '../../backend.service';
    import {MainDataService} from '../../maindata.service';
    import {concatMap} from 'rxjs/operators';
    import {CustomtextService} from 'iqb-components';
    
    
    @Component({
    
      templateUrl: './monitor-starter.component.html',
      styles: [
        'mat-card {margin: 10px;}',
        '.mat-card-gray {background-color: lightgray}'
      ]
    
    })
    export class MonitorStarterComponent implements OnInit, OnDestroy {
    
      accessObjects: (WorkspaceData|AccessObject)[] = [];
    
    mechtelm's avatar
    mechtelm committed
      isWorkspaceMonitor = false;
      private getWorkspaceDataSubscription: Subscription = null;
    
    
      constructor(
        private router: Router,
        private bs: BackendService,
    
        public cts: CustomtextService,
    
        private mds: MainDataService
      ) { }
    
      ngOnInit() {
        setTimeout(() => {
    
          this.mds.setSpinnerOn();
    
          this.bs.getSessionData().subscribe(authDataUntyped => {
            if (typeof authDataUntyped !== 'number') {
              const authData = authDataUntyped as AuthData;
              if (authData) {
                if (authData.token) {
    
                  this.accessObjects = [];
    
                  let scopeIdList = [];
                  if (authData.access[AuthAccessKeyType.TEST_GROUP_MONITOR]) {
                    scopeIdList = authData.access[AuthAccessKeyType.TEST_GROUP_MONITOR];
    
    mechtelm's avatar
    mechtelm committed
                    this.isWorkspaceMonitor = false;
    
                  } else if (authData.access[AuthAccessKeyType.WORKSPACE_MONITOR]) {
                    scopeIdList = authData.access[AuthAccessKeyType.WORKSPACE_MONITOR];
    
    mechtelm's avatar
    mechtelm committed
                    this.isWorkspaceMonitor = true;
    
    mechtelm's avatar
    mechtelm committed
                  if (this.getWorkspaceDataSubscription !== null) {
                    this.getWorkspaceDataSubscription.unsubscribe();
                  }
                  this.getWorkspaceDataSubscription = from(scopeIdList).pipe(
    
                    concatMap(monitorScopeId => {
    
                      if (authData.access[AuthAccessKeyType.TEST_GROUP_MONITOR]) {
    
    paf's avatar
    paf committed
                        return this.bs.getGroupData(monitorScopeId);
    
                      } else if (authData.access[AuthAccessKeyType.WORKSPACE_MONITOR]) {
    
    paf's avatar
    paf committed
                        return this.bs.getWorkspaceData(monitorScopeId);
    
                    })).subscribe(
    
                    wsData => this.accessObjects.push(wsData),
    
                    () => this.mds.setSpinnerOff(),
                    () => this.mds.setSpinnerOff()
                  );
    
                  this.mds.setAuthData(authData);
                } else {
                  this.mds.setAuthData();
    
                  this.mds.setSpinnerOff();
    
                }
              } else {
                this.mds.setAuthData();
    
                this.mds.setSpinnerOff();
    
            } else {
              this.mds.setSpinnerOff();
    
    paf's avatar
    paf committed
          });
    
      buttonGotoMonitor(accessObject: AccessObject) {
    
    
    mechtelm's avatar
    mechtelm committed
        if (this.isWorkspaceMonitor) {
    
          this.router.navigateByUrl('/wm/' + accessObject.id.toString());
    
    mechtelm's avatar
    mechtelm committed
        } else {
    
          this.router.navigateByUrl('/gm/' + accessObject.id.toString());
    
        }
      }
    
      resetLogin() {
        this.mds.setAuthData();
        this.router.navigate(['/']);
      }
    
      ngOnDestroy() {
    
    mechtelm's avatar
    mechtelm committed
        if (this.getWorkspaceDataSubscription !== null) {
          this.getWorkspaceDataSubscription.unsubscribe();