import { Component, Inject, ViewChild } from '@angular/core'; import { GuiColumn, GuiSelectedRow } from '@generic-ui/ngx-grid/gui/grid/src/core/api/gui.grid.public-api'; import { UserTableComponent } from '../tables/user-table/user-table.component'; import { UserRepTableComponent } from '../tables/user-rep-table/user-rep-table.component'; import { GroupTableComponent } from '../tables/group-table/group-table.component'; import { UserRepService } from 'src/app/services/user-representation.service'; import Swal from 'sweetalert2'; @Component({ selector: 'app-user-representation', templateUrl: './user-representation.component.html', styleUrl: './user-representation.component.css' }) export class UserRepresentationComponent { useRepLabel: string = ""; groupRepCols: Array; groupRightColumns: Array; slUserId: null | number = null; slRepUserId: null | number = null; slRepGroupId: null | number = null; slRightGroupId: null | number = null; slUserRepId: null | number = null; userRepService: UserRepService initWithoutData = () => { } constructor(userRepService: UserRepService, @Inject('GROUP_REP_TABLE_COLUMNS') groupRepCols: Array, @Inject('GROUP_RIGHT_TABLE_COLUMNS') groupRightColumns: Array) { this.groupRepCols = groupRepCols; this.groupRightColumns = groupRightColumns; this.userRepService = userRepService; } @ViewChild("users") users!: UserTableComponent; @ViewChild("repUsers") repUsers!: UserTableComponent; @ViewChild("repGroups") repGroups!: GroupTableComponent; @ViewChild("rightGroups") rightGroups!: GroupTableComponent; @ViewChild("userReps") userReps!: UserRepTableComponent; userOnSelectedRows = (rows: GuiSelectedRow[]) => { if (rows.length > 0) { this.users.safelyUnselectAll(); this.useRepLabel = `Repräsentationen von ${rows[0].source?.username}` this.userReps.fetchData(rows[0].source?.guid) this.slUserId = rows[0].source?.guid } } rightGroupOnSelectedRows = (rows: GuiSelectedRow[]) => { if (rows.length > 0) { this.slRightGroupId = rows[0].source?.guid } else { this.slRightGroupId = null; } } repUserOnSelectedRows = (rows: GuiSelectedRow[]) => { if (rows.length == 0 && this.slRepUserId) { if(!this.slUserId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie den Benutzer!", }); } else if(!this.slRepUserId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie die repräsentative Benutzer!", }); } else if(!this.slRightGroupId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie die richtige Gruppe!", }); } else { var newUserRep = { userId: this.slUserId, rightGroupId: this.slRightGroupId, repUserId: this.slRepUserId, addedWho: 'DEFAULT' } this.userRepService.create(newUserRep).subscribe({ next: (response) => { this.slRepUserId = null; this.repUsers.safelyUnselectAll() if(this.slUserId != null) this.userReps.fetchData(this.slUserId) }, error: (error) => { const errorMessage = error?.error || "Es ist ein unerwarteter Fehler aufgetreten."; Swal.fire({ icon: "error", title: "Oops...", text: `${errorMessage}\nBitte versuchen Sie es später noch einmal.`, }); } }); } this.slRepUserId = null; } else if(rows.length > 0) { this.slRepUserId = rows[0].source?.guid; } } repGroupOnSelectedRows = (rows: GuiSelectedRow[]) => { if (rows.length == 0 && this.slRepGroupId) { if(!this.slUserId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie den Benutzer!", }); } else if(!this.slRepGroupId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie die repräsentative Gruppe!", }); } else if(!this.slRightGroupId){ Swal.fire({ icon: "error", title: "Oops...", text: "Bitte wählen Sie die richtige Gruppe!", }); } else { var newUserRep = { userId: this.slUserId, rightGroupId: this.slRightGroupId, repGroupId: this.slRepGroupId, addedWho: 'DEFAULT' } this.userRepService.create(newUserRep).subscribe({ next: (res) => { this.slRepGroupId = null; this.repUsers.safelyUnselectAll() if(this.slUserId != null) this.userReps.fetchData(this.slUserId) }, error: (error) => { const errorMessage = error?.error || "Es ist ein unerwarteter Fehler aufgetreten."; Swal.fire({ icon: "error", title: "Oops...", text: `${errorMessage}\nBitte versuchen Sie es später noch einmal.`, }); } }); } this.slRepGroupId = null; } else if(rows.length > 0) { this.slRepGroupId = rows[0].source?.guid; } } userRepOnSelectedRows = (rows: GuiSelectedRow[]) => { if (rows.length == 0 && this.slUserRepId) { this.userRepService.delete(this.slUserRepId).subscribe({ next: (res) => { this.slUserRepId = null; this.userReps.safelyUnselectAll(); if(this.slUserId != null) this.userReps.fetchData(this.slUserId) }, error: (err) => { this.slUserRepId = null; this.repUsers.safelyUnselectAll() const errorMessage = err?.error || "Es ist ein unerwarteter Fehler aufgetreten."; Swal.fire({ icon: "error", title: "Oops...", text: `${errorMessage}\nBitte versuchen Sie es später noch einmal.`, }); } }) } else if(rows.length > 0) { this.slUserRepId = rows[0].source?.guid; } } }