import {Component, inject, input, output, signal} from '@angular/core'; import {NzCardComponent} from "ng-zorro-antd/card"; import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid"; import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form"; import {NzIconDirective} from "ng-zorro-antd/icon"; import {NzInputDirective} from "ng-zorro-antd/input"; import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms"; import {NzNotificationService} from "ng-zorro-antd/notification"; import {firstValueFrom} from "rxjs"; import {GetStaffDto, StaffsService, UpdateStaffDto} from "../../../services/api"; @Component({ selector: 'app-staff-card', imports: [ NzCardComponent, NzColDirective, NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent, NzIconDirective, NzInputDirective, NzRowDirective, ReactiveFormsModule ], templateUrl: './staff-card.html', styleUrl: './staff-card.css', }) export class StaffCard { private staffsService = inject(StaffsService); private notificationService = inject(NzNotificationService) staffEdit = signal(null); staffsLoading = signal(false); staffForm = new FormGroup({ f4T2NumberApproval: new FormControl(null, [Validators.required]), f4T2ExpirationDate: new FormControl(null, [Validators.required]), }) async submitForm() { this.staffsLoading.set(true); const staffValue: UpdateStaffDto = { f4T2NumberApproval: this.staffForm.value.f4T2NumberApproval, f4T2ExpirationDate: this.staffForm.value.f4T2ExpirationDate, } try { const staff = await firstValueFrom(this.staffsService.updateStaffEndpoint(this.staff().id, staffValue)); this.staffEdit.set(staff as unknown as UpdateStaffDto); console.log(staff); this.staffForm.reset(); this.edit.set(false); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur', '(ou Erreur de communication avec l\'API)'); } this.staffsLoading.set(false); } staff = input(null); edit = signal(false) triggerEdited = output(); protected Edit() { this.edit.set(true) } protected Back() { this.edit.set(false) this.staffForm.reset(); } async Delete() { this.staffsLoading.set(true); try { await firstValueFrom(this.staffsService.deleteStaffEndpoint(this.staff().id)); this.staffForm.reset(); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur', '(ou Erreur de communication avec l\'API)'); } this.staffsLoading.set(false); } }