import {Component, inject, viewChild} from '@angular/core'; import {ModalButton} from "../../components/modal-button/modal-button"; import {SupplierTable} from "../../components/supplier-table/supplier-table"; import {SupplierForm} from "../../components/supplier-form/supplier-form"; import {SuppliersService} from "../../services/api"; import {NzNotificationService} from "ng-zorro-antd/notification"; import {firstValueFrom} from "rxjs"; @Component({ selector: 'app-supplier', imports: [ SupplierForm, SupplierTable, ModalButton ], templateUrl: './supplier.html', styleUrl: './supplier.css', }) export class Supplier { modal = viewChild.required('modalButton'); createSupplier = viewChild.required('supplierForm'); supplierTable = viewChild.required('supplierTable'); private suppliersService = inject(SuppliersService); private notificationService = inject(NzNotificationService) async onModalOk() { await this.addSupplier() this.createSupplier().supplierForm.reset(); this.onModalCancel(); await this.supplierTable().fetchSuppliers() } onModalCancel() { this.modal().isVisible = false; } async addSupplier() { if (this.createSupplier().supplierForm.invalid) { this.notificationService.error('Erreur', 'Formulaire invalide') } try { const suppliers = this.createSupplier().supplierForm.getRawValue(); await firstValueFrom(this.suppliersService.createSupplierEndpoint(suppliers)) this.notificationService.success('Success', 'Fournisseur enregistré') } catch { this.notificationService.error('Erreur', 'Erreur d\'enregistrement') } } }