Files
pyrofetes-frontend/src/app/pages/supplier/supplier.ts
T
2026-05-28 10:52:46 +01:00

51 lines
1.8 KiB
TypeScript

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>('modalButton');
createSupplier = viewChild.required<SupplierForm>('supplierForm');
supplierTable = viewChild.required<SupplierTable>('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')
}
}
}