All files / src/app/workspace-admin/files/iqb-files-upload iqb-files-upload.component.ts

2.08% Statements 1/48
0% Branches 0/9
0% Functions 0/14
2.08% Lines 1/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                              1x                                                                                                                                                                                                                        
import {
  Component, EventEmitter, HostBinding, Input, OnDestroy, OnInit, Output
} from '@angular/core';
import { Subscription } from 'rxjs';
import { BackendService } from '../../backend.service';
import { BackendService as RootBackendService } from '../../../backend.service';
import { UploadReport, UploadStatus } from '../files.interfaces';
import { WorkspaceDataService } from '../../workspacedata.service';
import { MainDataService } from '../../../shared/services/maindata/maindata.service';
 
@Component({
  selector: 'tc-files-upload',
  templateUrl: './iqb-files-upload.component.html',
  styleUrls: ['../iqb-files.scss']
})
export class IqbFilesUploadComponent implements OnInit, OnDestroy {
  @HostBinding('class') myclass = 'iqb-files-upload';
 
  constructor(
    private bs: BackendService,
    private rbs: RootBackendService,
    private mds: MainDataService,
    public wds: WorkspaceDataService
  ) { }
 
  private _status: UploadStatus = UploadStatus.ready;
  get status(): UploadStatus {
    return this._status;
  }
 
  set status(newstatus: UploadStatus) {
    this._status = newstatus;
    this.statusChangedEvent.emit(this);
  }
 
  private requestResponse: UploadReport = {};
  get uploadResponse(): UploadReport {
    switch (this._status) {
      case UploadStatus.busy:
        return { '': { info: ['Bitte warten'] } };
      case UploadStatus.ready:
        return { '': { info: ['Bereit'] } };
      default:
        return this.requestResponse;
    }
  }
 
  /* Http request input bindings */
 
  @Input() fileAlias = 'file';
 
  @Input() folderName = '';
 
  @Input() folder = '';
 
  @Input() get files(): File[] {
    return this._files;
  }
 
  set files(files: File[]) {
    this._files = files;
  }
 
  private _files: File[] = [];
 
  @Input()
  set id(id: number) {
    this._id = id;
  }
 
  get id(): number {
    return this._id;
  }
 
  private _id: number = 0;
 
  @Output() removeFileRequestEvent = new EventEmitter<IqbFilesUploadComponent>();
  @Output() statusChangedEvent = new EventEmitter<IqbFilesUploadComponent>();
 
  progressPercentage = 0;
 
  private fileUploadSubscription: Subscription | null = null;
 
  ngOnInit(): void {
    this._status = UploadStatus.ready;
    this.requestResponse = {};
    this.upload();
  }
 
  upload(): void {
    Iif (this.status !== UploadStatus.ready) {
      return;
    }
 
    this.status = UploadStatus.busy;
    const formData = new FormData();
    this._files.forEach(file => {
      formData.append(this.fileAlias.concat('[]'), file, file.name);
    });
    Iif ((typeof this.folderName !== 'undefined') && (typeof this.folder !== 'undefined')) {
      Iif (this.folderName.length > 0) {
        formData.append(this.folderName, this.folder);
      }
    }
 
    this.fileUploadSubscription = this.bs.postFile(this.wds.workspaceId, formData)
      .subscribe(res => {
        this.requestResponse = res.report;
        this.status = res.status;
        this.progressPercentage = res.progress;
        this.rbs.checkIfSysCheckModeExists()
          .subscribe(doesSysCheckModeExist => {
            this.mds.sysCheckAvailableForAll = !doesSysCheckModeExist;
          });
      });
  }
 
  ngOnDestroy(): void {
    Iif (this.fileUploadSubscription) {
      this.fileUploadSubscription.unsubscribe();
    }
  }
}