Skip to content
Snippets Groups Projects
files.component.ts 5.61 KiB
Newer Older
rhenck's avatar
rhenck committed
import {
  Component, OnInit, Inject, ViewChild
} from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
paf's avatar
paf committed
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatDialog } from '@angular/material/dialog';
import { MatSort } from '@angular/material/sort';
rhenck's avatar
rhenck committed

import { saveAs } from 'file-saver';
rhenck's avatar
rhenck committed
import {
  ConfirmDialogComponent, ConfirmDialogData, MessageDialogComponent,
  MessageDialogData, MessageType
} from 'iqb-components';
import { WorkspaceDataService } from '../workspacedata.service';
import { GetFileResponseData } from '../workspace.interfaces';
rhenck's avatar
rhenck committed
import { BackendService, FileDeletionReport } from '../backend.service';
import { MainDataService } from '../../maindata.service';
paflov's avatar
paflov committed
interface FileStats {
  types: {
    [type: string]: {
paflov's avatar
paflov committed
      valid: number;
    }
  }
paflov's avatar
paflov committed
  valid: number;
Martin Mechtel's avatar
Martin Mechtel committed
  templateUrl: './files.component.html',
  styleUrls: ['./files.component.css']
export class FilesComponent implements OnInit {
  public serverfiles: MatTableDataSource<GetFileResponseData>;
  public displayedColumns = ['checked', 'filename', 'typelabel', 'filesize', 'filedatetime'];
mechtelm's avatar
mechtelm committed

  // for fileupload
  public uploadUrl = '';
  public fileNameAlias = 'fileforvo';
  public typeLabels = {
    'Testtakers': 'Teilnehmerliste',
    'Booklet': 'Testheft',
    'SysCheck': 'Systemcheck',
paf's avatar
paf committed
  @ViewChild(MatSort, { static: true }) sort: MatSort;
paflov's avatar
paflov committed
  public fileStats: FileStats = {
    types: {},
paflov's avatar
paflov committed
  };

  constructor(
    @Inject('SERVER_URL') private serverUrl: string,
    private bs: BackendService,
mechtelm's avatar
mechtelm committed
    public wds: WorkspaceDataService,
    public confirmDialog: MatDialog,
    public messageDialog: MatDialog,
    private mds: MainDataService,
    public snackBar: MatSnackBar
rhenck's avatar
rhenck committed
  ngOnInit(): void {
    this.uploadUrl = `${this.serverUrl}workspace/${this.wds.wsId}/file`;
mechtelm's avatar
mechtelm committed
    setTimeout(() => {
      this.mds.setSpinnerOn();
mechtelm's avatar
mechtelm committed
      this.updateFileList();
paf's avatar
paf committed
    });
  public checkAll(isChecked: boolean): void {
paflov's avatar
paflov committed
    this.serverfiles.data.forEach(element => {
      // eslint-disable-next-line no-param-reassign
      element.isChecked = isChecked;
    });
  }

  public deleteFiles(): void {
mechtelm's avatar
mechtelm committed
    if (this.wds.wsRole === 'RW') {

      const filesToDelete = [];
paflov's avatar
paflov committed
      this.serverfiles.data.forEach(element => {
mechtelm's avatar
mechtelm committed
        if (element.isChecked) {
          filesToDelete.push(`${element.type}/${element.name}`);
mechtelm's avatar
mechtelm committed
      if (filesToDelete.length > 0) {
paflov's avatar
paflov committed
        const p = filesToDelete.length > 1;
mechtelm's avatar
mechtelm committed
        const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
          width: '400px',
          data: <ConfirmDialogData>{
            title: 'Löschen von Dateien',
paflov's avatar
paflov committed
            content: `Sie haben ${p ? filesToDelete.length : 'eine'} Datei${p ? 'en' : ''}\` 
              ausgewählt. Soll${p ? 'en' : ''}  diese gelöscht werden?`,
            confirmbuttonlabel: 'Löschen',
            showcancel: true
mechtelm's avatar
mechtelm committed
          }
        });

        dialogRef.afterClosed().subscribe(result => {
          if (result !== false) {
            this.mds.setSpinnerOn();
            this.bs.deleteFiles(filesToDelete).subscribe((fileDeletionReport: FileDeletionReport) => {
              const message = [];
              if (fileDeletionReport.deleted.length > 0) {
paflov's avatar
paflov committed
                message.push(`${fileDeletionReport.deleted.length} Dateien erfolgreich gelöscht.`);
              if (fileDeletionReport.not_allowed.length > 0) {
paflov's avatar
paflov committed
                message.push(`${fileDeletionReport.not_allowed.length} Dateien konnten nicht gelöscht werden.`);
paflov's avatar
paflov committed
              this.snackBar.open(message.join('<br>'), message.length > 1 ? 'Achtung' : '', { duration: 1000 });
mechtelm's avatar
mechtelm committed
            });
          }
        });
      } else {
        this.messageDialog.open(MessageDialogComponent, {
mechtelm's avatar
mechtelm committed
          width: '400px',
          data: <MessageDialogData>{
            title: 'Löschen von Dateien',
            content: 'Bitte markieren Sie erst Dateien!',
            type: MessageType.error
          }
        });
      }
  public updateFileList(empty = false): void {
      this.serverfiles = new MatTableDataSource([]);
      this.mds.setSpinnerOff();
      this.bs.getFiles().subscribe(
        (fileList: GetFileResponseData[]) => {
          this.serverfiles = new MatTableDataSource(fileList);
          this.serverfiles.sort = this.sort;
paf's avatar
paf committed
          this.fileStats = FilesComponent.getStats(fileList);
          this.mds.setSpinnerOff();
paflov's avatar
paflov committed
  private static getStats(fileList: GetFileResponseData[]): FileStats {
    const stats: FileStats = {
      types: {},
paflov's avatar
paflov committed
    };
    fileList.forEach(file => {
      if (typeof stats.types[file.type] === 'undefined') {
        stats.types[file.type] = {
paflov's avatar
paflov committed
          valid: 0
        };
paf's avatar
paf committed
      }
      stats.types[file.type].total += 1;
      stats.total += 1;
      if (file.report.error && file.report.error.length) {
        stats.valid += 1;
        stats.types[file.type].valid += 1;
        stats.testtakers += (typeof file.info.testtakers === "number") ? file.info.testtakers : 0;
paflov's avatar
paflov committed
    });
    return stats;
paf's avatar
paf committed
  }

  public download(element: GetFileResponseData): void {
    this.mds.setSpinnerOn();
    this.bs.downloadFile(element.type, element.name)
          this.mds.setSpinnerOff();
            saveAs(fileData as Blob, element.name);