All files / src/app/superadmin/workspaces workspaces.component.ts

25.58% Statements 22/86
0% Branches 0/25
20.83% Functions 5/24
25.88% Lines 22/85

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218                                      1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x   1x                             1x 1x                                                                                                                                                                                                                                                                                                             1x 1x 1x 1x                
import { MatTableDataSource } from '@angular/material/table';
import { ViewChild, Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatSort } from '@angular/material/sort';
import { SelectionModel } from '@angular/cdk/collections';
import {
  ConfirmDialogComponent, ConfirmDialogData,
  MessageDialogComponent, MessageDialogData
} from '../../shared/shared.module';
import { BackendService } from '../backend.service';
import { NewworkspaceComponent } from './newworkspace/newworkspace.component';
import { EditworkspaceComponent } from './editworkspace/editworkspace.component';
import { IdAndName, IdRoleData } from '../superadmin.interfaces';
 
@Component({
  templateUrl: './workspaces.component.html',
  styleUrls: ['./workspaces.component.css']
})
export class WorkspacesComponent implements OnInit {
  workspaces: MatTableDataSource<IdAndName> = new MatTableDataSource<IdAndName>();
  displayedColumns = ['name', 'modification_timestamp'];
  tableSelectionRow = new SelectionModel<IdAndName>(false, []);
  selectedWorkspaceId = 0;
  selectedWorkspaceName = '';
  pendingUserChanges = false;
  userListDatasource: MatTableDataSource<IdRoleData> = new MatTableDataSource<IdRoleData>();
  displayedUserColumns = ['selectCheckbox', 'name'];
 
  @ViewChild(MatSort) sort!: MatSort;
 
  constructor(
    private backendService: BackendService,
    private newWorkspaceDialog: MatDialog,
    private editworkspaceDialog: MatDialog,
    private deleteConfirmDialog: MatDialog,
    private messsageDialog: MatDialog,
    private snackBar: MatSnackBar
  ) {
    this.tableSelectionRow.changed.subscribe(
      r => {
        if (r.added.length > 0) {
          this.selectedWorkspaceId = r.added[0].id;
          this.selectedWorkspaceName = r.added[0].name;
        } else {
          this.selectedWorkspaceId = 0;
          this.selectedWorkspaceName = '';
        }
        this.updateUserList();
      }
    );
  }
 
  ngOnInit(): void {
    setTimeout(() => {
      this.updateWorkspaceList();
    });
  }
 
  addObject(): void {
    const dialogRef = this.newWorkspaceDialog.open(NewworkspaceComponent, {
      width: '600px',
      data: {
        name: ''
      }
    });
 
    dialogRef.afterClosed().subscribe(result => {
      Iif (!result) {
        return;
      }
 
      const newName = result.get('name').value;
      Iif (this.workspaceNameExists(newName)) {
        this.snackBar.open('Arbeitsbereich mit diesem namen bereits vorhanden!', 'Fehler', { duration: 1000 });
        return;
      }
 
      this.backendService.addWorkspace(newName)
        .subscribe(() => {
          this.snackBar.open('Arbeitsbereich hinzugefügt', '', { duration: 1000 });
          this.updateWorkspaceList();
        });
    });
  }
 
  private workspaceNameExists(newName: string): boolean {
    return this.workspaces.data
      .map(ws => ws.name)
      .includes(newName);
  }
 
  changeObject(): void {
    const selectedRows = this.tableSelectionRow.selected;
    if (selectedRows.length === 0) {
      this.messsageDialog.open(MessageDialogComponent, {
        width: '400px',
        data: <MessageDialogData>{
          title: 'Arbeitsbereich ändern',
          content: 'Bitte markieren Sie erst einen Arbeitsbereich!',
          type: 'error'
        }
      });
    } else {
      const dialogRef = this.editworkspaceDialog.open(EditworkspaceComponent, {
        width: '600px',
        data: selectedRows[0].name
      });
 
      dialogRef.afterClosed().subscribe(result => {
        Iif (!result) {
          return;
        }
 
        const newName = result.get('name').value;
        Iif (this.workspaceNameExists(newName)) {
          this.snackBar.open('Arbeitsbereich mit diesem namen bereits vorhanden!', 'Fehler', { duration: 1000 });
          return;
        }
 
        this.backendService.renameWorkspace(selectedRows[0].id, newName)
          .subscribe(
            () => {
              this.snackBar.open('Arbeitsbereich geändert', '', { duration: 1000 });
              this.updateWorkspaceList();
            }
          );
      });
    }
  }
 
  deleteObject(): void {
    const selectedRows = this.tableSelectionRow.selected;
    if (selectedRows.length === 0) {
      this.messsageDialog.open(MessageDialogComponent, {
        width: '400px',
        data: <MessageDialogData>{
          title: 'Löschen von Arbeitsbereichen',
          content: 'Bitte markieren Sie erst Arbeitsbereich/e!',
          type: 'error'
        }
      });
    } else {
      let prompt;
      if (selectedRows.length > 1) {
        prompt = `Sollen ${selectedRows.length} Arbeitsbereiche gelöscht werden?`;
      } else {
        prompt = `Arbeitsbereich "${selectedRows[0].name}" gelöscht werden?`;
      }
      const dialogRef = this.deleteConfirmDialog.open(ConfirmDialogComponent, {
        width: '400px',
        data: <ConfirmDialogData>{
          title: 'Löschen von Arbeitsbereichen',
          content: prompt,
          confirmbuttonlabel: 'Arbeitsbereich/e löschen',
          showcancel: true
        }
      });
 
      dialogRef.afterClosed().subscribe(result => {
        Iif (result !== false) {
          const workspacesToDelete: number[] = [];
          selectedRows.forEach((r: IdAndName) => workspacesToDelete.push(r.id));
          this.backendService.deleteWorkspaces(workspacesToDelete)
            .subscribe(() => {
              this.snackBar.open('Arbeitsbereich/e gelöscht', 'Fehler', { duration: 1000 });
              this.updateWorkspaceList();
            });
        }
      });
    }
  }
 
  updateUserList(): void {
    this.pendingUserChanges = false;
    Iif (this.selectedWorkspaceId > 0) {
      this.userListDatasource = new MatTableDataSource();
      this.backendService.getUsersByWorkspace(this.selectedWorkspaceId)
        .subscribe(dataresponse => {
          this.userListDatasource = new MatTableDataSource(dataresponse);
        });
    }
  }
 
  selectPermissions(user: IdRoleData, role: string): void {
    if (role === 'RW') {
      user.role = (user.role === 'RW') ? 'RO' : 'RW';
    } else Iif (role === 'RO') {
      user.role = (user.role === 'RO' || user.role === 'RW') ? '' : 'RO';
    }
    this.pendingUserChanges = true;
  }
 
  saveUsers():void {
    this.pendingUserChanges = false;
    if (this.selectedWorkspaceId > 0) {
      this.backendService.setUsersByWorkspace(this.selectedWorkspaceId, this.userListDatasource.data)
        .subscribe(() => {
          this.snackBar.open('Zugriffsrechte geändert', '', { duration: 1000 });
        });
    } else {
      this.userListDatasource = new MatTableDataSource<IdRoleData>();
    }
  }
 
  updateWorkspaceList(): void {
    this.backendService.getWorkspaces().subscribe(dataresponse => {
      this.workspaces = new MatTableDataSource(dataresponse);
      this.workspaces.sort = this.sort;
      this.tableSelectionRow.clear();
    });
  }
 
  selectRow(row: IdAndName): void {
    this.tableSelectionRow.select(row);
  }
}