import { Component, inject } from '@angular/core'; import { ICellRendererAngularComp } from 'ag-grid-angular'; import { ICellRendererParams } from 'ag-grid-community'; import { IKey } from '../../../../model/interface/key.interface'; import { ApiService } from '../../../api.service'; import { MatDialog, MatDialogModule } from '@angular/material/dialog'; import { DeleteKeyComponent } from '../../../../modules/keys/components/delete-key/delete-key.component'; import { HotToastService } from '@ngxpert/hot-toast'; import { HandoverDialogComponent } from '../../../../modules/keys/components/handover-dialog/handover-dialog.component'; import { MatTooltipModule } from '@angular/material/tooltip'; import { LostKeyComponent } from '../../../../modules/keys/components/lost-key/lost-key.component'; import { CreateKeyComponent } from '../../../../modules/keys/create/create.component'; @Component({ selector: 'app-ag-key-actions', imports: [MatDialogModule, MatTooltipModule], templateUrl: './ag-key-actions.component.html', styleUrl: './ag-key-actions.component.scss' }) export class AgKeyActionsComponent implements ICellRendererAngularComp { key!: IKey; params!: ICellRendererParams; private api: ApiService = inject(ApiService); private dialog: MatDialog = inject(MatDialog); private toast = inject(HotToastService); agInit(params: ICellRendererParams): void { this.params = params; this.key = params.data; } refresh(params: ICellRendererParams): boolean { return false; } delete() { const ref = this.dialog.open(DeleteKeyComponent, { data: this.key, autoFocus: false, width: '500px', maxWidth: 'calc(100vw - 24px)', }) ref.afterClosed().subscribe({ next: n => { if (n) { this.deleteThisKey(); } } }) } edit() { const ref = this.dialog.open(CreateKeyComponent, { data: this.key, autoFocus: false, maxWidth: '100vw', maxHeight: '100vh' }) ref.afterClosed().subscribe({ next: n => { if (n != null) { this.key.handedOut = n; this.params.api.refreshCells(); } } }); // ref.componentInstance.editKey(this.key) } deleteThisKey() { this.params.api.setGridOption("loading", true); this.api.deleteKey(this.key.id).pipe( this.toast.observe({ loading: 'Lösche Schlüssel ' + this.key.name, success: 'Schlüssel ' + this.key.name + ' gelöscht', error: 'Konnte nicht gelöscht werden' }) ).subscribe({ next: () => { this.api.refreshKeys(); }, error: () => { this.params.api.setGridOption("loading", false); } }) } openHandoutDialog() { this.dialog.open(HandoverDialogComponent, { data: this.key, autoFocus: false, maxWidth: '100vw', maxHeight: '100vh' }).afterClosed().subscribe({ next: n => { if (n != null) { this.key.handedOut = n; this.params.api.refreshCells(); this.api.refreshKeys(); } } }) } openLostKeyDialog() { this.dialog.open(LostKeyComponent, { data: this.key, autoFocus: false, maxWidth: '100vw', maxHeight: '100vh' }).afterClosed().subscribe({ next: async n => { if (n != null) { if (n == "") { n = null; } this.key.keyLost = n; this.params.api.refreshCells(); await this.api.updateKey(this.key).subscribe(); this.api.refreshKeys(); } } }) } }