import {Component, inject, input, output, signal} from '@angular/core'; import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms"; 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 {GetProviderDto, ServiceprovidersService, UpdateProviderDto} from "../../../services/api"; import {NzNotificationService} from "ng-zorro-antd/notification"; import {firstValueFrom} from "rxjs"; @Component({ selector: 'app-providers-card', imports: [FormsModule, NzCardComponent, NzColDirective, NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent, NzIconDirective, NzInputDirective, NzRowDirective, ReactiveFormsModule], templateUrl: './providers-card.html', styleUrl: './providers-card.css', }) export class ProvidersCard { private providersService = inject(ServiceprovidersService); private notificationService = inject(NzNotificationService) providerEdit = signal(null); providersLoading = signal(false); providerForm = new FormGroup({ id: new FormControl(null, [Validators.required]), price: new FormControl(null, [Validators.required]), }) async submitForm() { this.providersLoading.set(true); const providerValue = { price: this.providerForm.value.price, } try { const provider = await firstValueFrom(this.providersService.updateProviderEndpoint(this.provider().id,providerValue)); this.providerEdit.set(provider); console.log(provider); this.providerForm.reset(); this.edit.set(false); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur', ' (ou Erreur de communication avec l\'API)'); } this.providersLoading.set(false); } provider = input(null); edit = signal(false) triggerEdited = output(); protected Edit() { this.edit.set(true) } protected Back() { this.edit.set(false) this.providerForm.reset(); } async Delete() { this.providersLoading.set(true); try { const provider = await firstValueFrom(this.providersService.deleteProviderEndpoint(this.provider().id)); this.providerEdit.set(provider); this.providerForm.reset(); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur de recherche', "L\'auteur n\'existe pas !"); this.notificationService.error('Erreur', ' (ou Erreur de communication avec l\'API)'); } this.providersLoading.set(false); } }