Skip to content
Snippets Groups Projects
files.component.ts 5.8 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, CheckWorkspaceResponseData } from '../workspace.interfaces';
import { BackendService, FileDeletionReport } from '../backend.service';
import { MainDataService } from '../../maindata.service';
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', 'status', 'typelabel', 'filesize', 'filedatetime'];
mechtelm's avatar
mechtelm committed

  // for fileupload
  public uploadUrl = '';
  public fileNameAlias = 'fileforvo';

  // for workspace-check
  public checkErrors = [];
  public checkWarnings = [];
  public checkInfos = [];

paf's avatar
paf committed
  @ViewChild(MatSort, { static: true }) sort: MatSort;
paf's avatar
paf committed
  private fileStats: {[type: string]: number};

  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
    });
  }

  checkAll(isChecked: boolean) {
rhenck's avatar
rhenck committed
    this.serverfiles.data.forEach((element) => {
      element.isChecked = isChecked;
    });
  }

  deleteFiles() {
mechtelm's avatar
mechtelm committed
    if (this.wds.wsRole === 'RW') {
      this.checkErrors = [];
      this.checkWarnings = [];
      this.checkInfos = [];

      const filesToDelete = [];
rhenck's avatar
rhenck committed
      this.serverfiles.data.forEach((element) => {
mechtelm's avatar
mechtelm committed
        if (element.isChecked) {
rhenck's avatar
rhenck committed
          filesToDelete.push(`${element.type}/${element.filename}`);
mechtelm's avatar
mechtelm committed
      if (filesToDelete.length > 0) {
        let prompt = 'Sie haben ';
        if (filesToDelete.length > 1) {
          prompt = prompt + filesToDelete.length + ' Dateien ausgewählt. Sollen';
        } else {
          prompt = prompt + ' eine Datei ausgewählt. Soll';
        }
        const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
          width: '400px',
          data: <ConfirmDialogData>{
            title: 'Löschen von Dateien',
rhenck's avatar
rhenck committed
            content: `${prompt} 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) {
                message.push(fileDeletionReport.deleted.length + ' Dateien erfolgreich gelöscht.');
              if (fileDeletionReport.not_allowed.length > 0) {
                message.push(fileDeletionReport.not_allowed.length + ' Dateien konnten nicht gelöscht werden.');
              }
              this.snackBar.open(message.join('<br>'), message.length > 1 ? 'Achtung' : '',  {duration: 1000});
              this.updateFileList();
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
          }
        });
      }
rhenck's avatar
rhenck committed
  updateFileList(empty = false): void {
    this.checkErrors = [];
    this.checkWarnings = [];
    this.checkInfos = [];

mechtelm's avatar
mechtelm committed
    if (empty || this.wds.wsRole === 'MO') {
      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();
paf's avatar
paf committed
  private static getStats(fileList: GetFileResponseData[]): {[type: string]: number} {
    const stats: {[type: string]: number} = {};
    // TODO filter validity
    return fileList.reduce((carry, file) => {
      if (typeof carry[file.type] === 'undefined') {
        // eslint-disable-next-line no-param-reassign
        carry[file.type] = 0;
      }
      // eslint-disable-next-line no-param-reassign
      carry[file.type] += 1;
      return carry;
    }, stats);
  }

  download(element: GetFileResponseData): void {
    this.mds.setSpinnerOn();
    this.bs.downloadFile(element.type, element.filename)
          this.mds.setSpinnerOff();
          if (fileData !== false) {
            saveAs(fileData as Blob, element.filename);
rhenck's avatar
rhenck committed
  checkWorkspace(): void {
    this.checkErrors = [];
    this.checkWarnings = [];
    this.checkInfos = [];

    this.mds.setSpinnerOn();
    this.bs.checkWorkspace().subscribe(
Martin Mechtel's avatar
Martin Mechtel committed
      (checkResponse: CheckWorkspaceResponseData) => {
        this.mds.setSpinnerOff();
Martin Mechtel's avatar
Martin Mechtel committed
        this.checkErrors = checkResponse.errors;
        this.checkWarnings = checkResponse.warnings;
        this.checkInfos = checkResponse.infos;
Martin Mechtel's avatar
Martin Mechtel committed
    );