feat: Delete-Service erstellt und Löschprozess optimiert

- Delete-Service hinzugefügt, um den Löschvorgang zu verwalten.
- Löschprozess in `User`- und `Group`-Komponenten im `ngAfterViewInit`-Lebenszyklus implementiert.
- `removeAll`-Methode im Konstruktor der `BaseComponent` aufgerufen, um vorhandene Services zu bereinigen.
This commit is contained in:
Developer 02 2024-08-12 13:37:36 +02:00
parent 42f082996b
commit 45dac8a554
6 changed files with 60 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import {NavMenuComponent} from './components/nav-menu/nav-menu.component'
import { TransferService } from './services/button/transfer.service'; import { TransferService } from './services/button/transfer.service';
import { UpdateService } from './services/button/update.service'; import { UpdateService } from './services/button/update.service';
import { RefreshService } from './services/button/refresh.service'; import { RefreshService } from './services/button/refresh.service';
import { DeletionService } from './services/button/deletion.service';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
@ -17,6 +18,7 @@ export class AppComponent {
protected transferService: TransferService = inject(TransferService) protected transferService: TransferService = inject(TransferService)
protected updateService: UpdateService = inject(UpdateService) protected updateService: UpdateService = inject(UpdateService)
protected refreshService: RefreshService = inject(RefreshService) protected refreshService: RefreshService = inject(RefreshService)
protected deletionService: DeletionService = inject(DeletionService)
@HostListener('window:keydown.control.s', ['$event']) @HostListener('window:keydown.control.s', ['$event'])
protected handleCtrlS(event: KeyboardEvent) { protected handleCtrlS(event: KeyboardEvent) {
@ -32,9 +34,10 @@ export class AppComponent {
@HostListener('window:keydown.delete', ['$event']) @HostListener('window:keydown.delete', ['$event'])
protected handleDelete(event: KeyboardEvent) { protected handleDelete(event: KeyboardEvent) {
event.preventDefault();
this.deletionService.executeAll();
} }
@HostListener('window:keydown.control.space', ['$event']) @HostListener('window:keydown.control.space', ['$event'])
protected handleCtrlSapce(event: KeyboardEvent) { protected handleCtrlSapce(event: KeyboardEvent) {
event.preventDefault(); event.preventDefault();

View File

@ -3,6 +3,7 @@ import { RefreshService } from '../../services/button/refresh.service';
import { CreationService } from '../../services/button/creation.service'; import { CreationService } from '../../services/button/creation.service';
import { UpdateService } from '../../services/button/update.service'; import { UpdateService } from '../../services/button/update.service';
import { TransferService } from '../../services/button/transfer.service'; import { TransferService } from '../../services/button/transfer.service';
import { DeletionService } from '../../services/button/deletion.service';
import { ButtonVisibilityService } from '../../services/button/button-visibility.service'; import { ButtonVisibilityService } from '../../services/button/button-visibility.service';
@Component({ @Component({
@ -18,6 +19,7 @@ export class BasePageComponent {
protected creationService: CreationService = inject(CreationService) protected creationService: CreationService = inject(CreationService)
protected updateService: UpdateService = inject(UpdateService) protected updateService: UpdateService = inject(UpdateService)
protected transferService: TransferService = inject(TransferService) protected transferService: TransferService = inject(TransferService)
protected deletionService: DeletionService = inject(DeletionService)
protected buttonVisibilityService: ButtonVisibilityService = inject(ButtonVisibilityService) protected buttonVisibilityService: ButtonVisibilityService = inject(ButtonVisibilityService)
constructor() { constructor() {
@ -28,6 +30,7 @@ export class BasePageComponent {
} }
if(this.transferService.any) if(this.transferService.any)
this.transferService.removeAll() this.transferService.removeAll()
if(this.deletionService.any)
this.deletionService.removeAll()
} }
handleDeleteRequest() { }
} }

View File

@ -43,6 +43,8 @@ export class GroupComponent extends BasePageComponent implements AfterViewInit {
this.userTable.fetchDataByGroupId(this.sGroupId); this.userTable.fetchDataByGroupId(this.sGroupId);
}); });
this.creationService.component = GroupFormComponent this.creationService.component = GroupFormComponent
this.deletionService.add(() => this.handleDeleteRequest());
} }
@ViewChild("groupTable") groupTable!: GroupTableComponent; @ViewChild("groupTable") groupTable!: GroupTableComponent;
@ -56,7 +58,7 @@ export class GroupComponent extends BasePageComponent implements AfterViewInit {
} }
} }
override handleDeleteRequest() { handleDeleteRequest() {
const sRows = this.groupTable.selectedRows; const sRows = this.groupTable.selectedRows;
if (sRows.length > 0) if (sRows.length > 0)
Swal.fire({ Swal.fire({

View File

@ -47,6 +47,8 @@ export class UserComponent extends BasePageComponent implements AfterViewInit {
}); });
this.creationService.component = UserFormComponent; this.creationService.component = UserFormComponent;
this.deletionService.add(() => this.handleDeleteRequest());
} }
@ViewChild("userTable") userTable!: UserTableComponent @ViewChild("userTable") userTable!: UserTableComponent
@ -64,7 +66,7 @@ export class UserComponent extends BasePageComponent implements AfterViewInit {
} }
} }
override handleDeleteRequest() { handleDeleteRequest() {
const sRows = this.userTable.selectedRows; const sRows = this.userTable.selectedRows;
if (sRows.length > 0) if (sRows.length > 0)
Swal.fire({ Swal.fire({

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { DeletionService } from './deletion.service';
describe('DeletionService', () => {
let service: DeletionService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DeletionService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,30 @@
import { Injectable } from '@angular/core';
import { BaseButtonService } from './base-button.service';
@Injectable({
providedIn: 'root'
})
export class DeletionService extends BaseButtonService {
private actions: Array<() => void> = [];
public get count(): number {
return this.actions.length;
}
public get any(): boolean {
return this.count > 0;
}
add(action: () => void): void {
this.actions.push(action);
}
removeAll(): void {
this.actions = [];
}
executeAll(): void {
this.actions.forEach(action => action());
}
}