Skip to content
Snippets Groups Projects
test-starter.component.ts 2.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • mechtelm's avatar
    mechtelm committed
    import { Component, OnInit } from '@angular/core';
    
    import {AuthAccessKeyType, AuthData, BookletData} from "../../app.interfaces";
    import {from, Subscription} from "rxjs";
    import {concatMap} from "rxjs/operators";
    import {Router} from "@angular/router";
    import {BackendService} from "../../backend.service";
    import {MainDataService} from "../../maindata.service";
    
    mechtelm's avatar
    mechtelm committed
    
    @Component({
      templateUrl: './test-starter.component.html',
      styleUrls: ['./test-starter.component.css']
    })
    export class TestStarterComponent implements OnInit {
    
      booklets: BookletData[] = [];
      private getBookletDataSubscription: Subscription = null;
      public bookletSelectTitle = 'Bitte wählen';
    
    mechtelm's avatar
    mechtelm committed
      problemText = '';
    
    mechtelm's avatar
    mechtelm committed
    
    
      constructor(
        private router: Router,
        private bs: BackendService,
        private mds: MainDataService
      ) { }
    
    mechtelm's avatar
    mechtelm committed
    
      ngOnInit(): void {
    
        setTimeout(() => {
          this.bs.getSessionData().subscribe(authDataUntyped => {
            if (typeof authDataUntyped !== 'number') {
              const authData = authDataUntyped as AuthData;
              if (authData) {
                if (authData.token) {
                  if (authData.access[AuthAccessKeyType.TEST]) {
                    this.booklets = [];
    
    mechtelm's avatar
    mechtelm committed
                    if (this.getBookletDataSubscription !== null) {
                      this.getBookletDataSubscription.unsubscribe();
                    }
    
                    this.getBookletDataSubscription = from(authData.access[AuthAccessKeyType.TEST]).pipe(
                      concatMap(bookletId => {
                        return this.bs.getBookletData(bookletId)
    
    mechtelm's avatar
    mechtelm committed
                      })).subscribe(
                        bData => {
                          this.booklets.push(bData)
                        },
                        e => {
                          this.problemText = `Fehler in der Netzwerkverbindung (${e}).`
                        },
                        () => {
                          this.problemText = this.booklets.length > 0 ? '' : 'Für diese Anmeldung wurde kein Test gefunden.'
                        }
                      );
    
                  }
                  this.mds.setAuthData(authData);
                } else {
                  this.mds.setAuthData();
                }
              } else {
                this.mds.setAuthData();
              }
            }
          })
        });
    
    mechtelm's avatar
    mechtelm committed
      startTest(b: BookletData) {
    
        this.bs.startTest(b.id).subscribe(testId => {
    
    mechtelm's avatar
    mechtelm committed
          if (typeof testId === 'number') {
            const errCode = testId as number;
            if (errCode === 423) {
              this.problemText = 'Dieser Test ist gesperrt';
    
    mechtelm's avatar
    mechtelm committed
              this.problemText = `Problem beim Start (${errCode})`;
    
    mechtelm's avatar
    mechtelm committed
          } else {
            this.router.navigate(['/t', testId]);
    
    mechtelm's avatar
    mechtelm committed
        });
    
      }
    
      resetLogin() {
        this.mds.setAuthData();
        this.router.navigate(['/']);
      }
    
      ngOnDestroy() {
        if (this.getBookletDataSubscription !== null) {
          this.getBookletDataSubscription.unsubscribe();
        }
      }
    
    mechtelm's avatar
    mechtelm committed
    }