Skip to content
Snippets Groups Projects
test-view.component.ts 4 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChange} from '@angular/core';
    
    import {BookletService} from '../booklet.service';
    
    import {combineLatest, Observable, Subject} from 'rxjs';
    
    import {Booklet, StatusUpdate, Testlet, Unit} from '../group-monitor.interfaces';
    
    import {map} from 'rxjs/operators';
    
    paf's avatar
    paf committed
    
    
    function isUnit(testletOrUnit: Testlet|Unit): testletOrUnit is Unit {
    
    
        return !('children' in testletOrUnit);
    
    @Component({
      selector: 'test-view',
      templateUrl: './test-view.component.html',
      styleUrls: ['./test-view.component.css']
    })
    
    export class TestViewComponent implements OnInit, OnDestroy, OnChanges {
    
        @Input() testStatus: StatusUpdate;
    
    
        private testStatus$: Subject<StatusUpdate>;
    
        public booklet$: Observable<boolean|Booklet>;
    
        public featuredUnit$: Observable<{
            unit: Unit,
            parent: Testlet
        }|null>;
    
    paf's avatar
    paf committed
        public units: (Testlet|Unit)[];
        public maxTimeLeft: object|null;
    
        constructor(
            private bookletsService: BookletService,
        ) {
    
            this.testStatus$ = new Subject<StatusUpdate>();
    
    paf's avatar
    paf committed
            console.log('NEW:' + this.testStatus.personId, this.testStatus.bookletName);
    
            this.booklet$ = this.bookletsService.getBooklet(this.testStatus.bookletName || "");
    
    
            this.childrenSubscription = this.booklet$.subscribe((booklet: Booklet|boolean) => {
    
    
                if ((booklet !== null) && (booklet !== true) && (booklet !== false)) {
                    this.units = booklet.units.children;
                }
    
    
            this.featuredUnit$ = combineLatest<[Booklet|null, StatusUpdate]>([this.booklet$, this.testStatus$])
                .pipe(map((bookletAndStatus: [Booklet|boolean, StatusUpdate]) => {
    
    
    paf's avatar
    paf committed
                    console.log("MAP");
    
    
                    const booklet: Booklet|boolean = bookletAndStatus[0];
    
                    if ((booklet === true) || (booklet === false)) {
                        return null;
                    }
    
                    if (this.testStatus.unitName) {
                        return this.findUnitByName(booklet.units, this.testStatus.unitName);
                    }
                }));
    
    paf's avatar
    paf committed
        ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
    
            this.testStatus$.next(this.testStatus);
            this.maxTimeLeft = this.parseJsonState(this.testStatus.testState, 'MAXTIMELEFT');
        }
    
    
    
        ngOnDestroy(): void {
    
            this.childrenSubscription.unsubscribe();
        }
    
    
    
        getTestletType(testletOrUnit: Unit|Testlet): 'testlet'|'unit' {
    
            return isUnit(testletOrUnit) ? 'unit': 'testlet';
    
    paf's avatar
    paf committed
        hasState(stateObject: object, key: string, value: any = null): boolean {
    
    paf's avatar
    paf committed
            return ((typeof stateObject[key] !== "undefined") && ((value !== null) ? (stateObject[key] === value) : true));
    
    paf's avatar
    paf committed
        parseJsonState(testStateObject: object, key: string): object|null {
    
            if (typeof testStateObject[key] === "undefined") {
                return null;
            }
    
            const stateValueString = testStateObject[key];
    
            try {
                return JSON.parse(stateValueString);
            } catch (error) {
                console.warn(`state ${key} is no valid JSON`, stateValueString, error);
                return null;
            }
        }
    
    
    
    
        trackUnits(index: number, testlet: Testlet|Unit): string {
    
            return testlet['id'] || index.toString();
    
        findUnitByName(testlet: Testlet, unitName: String): {unit: Unit|null, parent: Testlet} {
    
            for (let i = 0; i < testlet.children.length; i++) {
    
                const testletOrUnit = testlet.children[i];
    
                if (isUnit(testletOrUnit)) {
    
                    if (testletOrUnit.id === unitName) {
    
                        return {
                            parent: testlet,
                            unit: testletOrUnit
                        }
    
    paf's avatar
    paf committed
    
                    const findInChildren = this.findUnitByName(testletOrUnit, unitName);
    
                    if (findInChildren !== null) {
                        return findInChildren;
                    }