Refactor code

This commit is contained in:
2026-05-28 10:52:46 +01:00
parent 7041c5335b
commit d37ff4ace4
31 changed files with 132 additions and 353 deletions
@@ -1,4 +1,4 @@
<nz-table [nzData]="filteredSuppliers()"
<nz-table [nzData]="suppliers()"
[nzLoading]="suppliersLoading()"
[nzFrontPagination]="false">
<thead>
@@ -15,7 +15,7 @@
</tr>
</thead>
<tbody style="text-align: center">
@for (supplier of filteredSuppliers(); track supplier.id) {
@for (supplier of suppliers(); track supplier.id) {
<tr>
<td>{{ supplier.name }}</td>
<td>{{ supplier.phone }}</td>
@@ -26,7 +26,7 @@
<td>{{ supplier.deliveryDelay }} jours</td>
<td>
<app-modal-button type="link" [name]="'Voir les produits'" size="45%">
<nz-table [nzData]="filteredSuppliers()" [nzFrontPagination]="false">
<nz-table [nzData]="suppliers()" [nzFrontPagination]="false">
<thead>
<tr class="text-center">
<th>Produit</th>
@@ -1,4 +1,4 @@
import {Component, computed, inject, OnInit, signal, viewChild} from '@angular/core';
import {Component, inject, OnInit, signal, viewChild} from '@angular/core';
import {ModalNav} from "../modal-nav/modal-nav";
import {NzDividerComponent} from "ng-zorro-antd/divider";
import {NzIconDirective} from "ng-zorro-antd/icon";
@@ -29,11 +29,13 @@ export class SupplierTable implements OnInit {
private suppliersService = inject(SuppliersService);
private pricesService = inject(PricesService);
private notificationService = inject(NzNotificationService);
suppliers = signal<GetSupplierDto[]>([]);
suppliersLoading = signal<boolean>(false);
updateSupplier = viewChild.required<SupplierForm>('supplierForm');
supplierModal = viewChild.required<ModalNav>('supplierModal');
productModal = viewChild.required<ModalNav>('productModal');
selectedSupplier: GetSupplierDto | null = null;
selectedProduct: GetPriceDto | null = null;
selectedProductSupplierId: number | null = null;
@@ -42,29 +44,12 @@ export class SupplierTable implements OnInit {
await this.fetchSuppliers();
}
private searchQuery = signal<string>('');
filteredSuppliers = computed(() => {
const q = this.searchQuery().toLowerCase().trim();
if (!q) return this.suppliers();
return this.suppliers().filter(s => {
const name = (s.name ?? '').toLowerCase();
return name.includes(q);
});
});
applySearch(query: string) {
this.searchQuery.set(query);
}
async fetchSuppliers() {
this.suppliersLoading.set(true);
try {
const suppliers = await firstValueFrom(this.suppliersService.getAllSuppliersEndpoint());
this.suppliers.set(suppliers);
} catch (e) {
} catch {
this.notificationService.error('Erreur', 'Erreur de communication avec l\'API');
}
this.suppliersLoading.set(false);
@@ -79,8 +64,7 @@ export class SupplierTable implements OnInit {
const suppliers = updateSupplierComponent.supplierForm.getRawValue();
await firstValueFrom(this.suppliersService.updateSupplierEndpoint(id, suppliers));
this.notificationService.success('Success', 'Fournisseur modifié');
} catch (e) {
console.error(e);
} catch {
this.notificationService.error('Erreur', 'Erreur lors de la modification');
}
}
@@ -90,7 +74,7 @@ export class SupplierTable implements OnInit {
await firstValueFrom(this.suppliersService.deleteSupplierEndpoint(supplier));
this.notificationService.success('Succès', 'Suppression effectuée');
await this.fetchSuppliers();
} catch (e) {
} catch {
this.notificationService.error('Erreur', 'Impossible de supprimer la ligne');
}
}
@@ -122,13 +106,13 @@ export class SupplierTable implements OnInit {
modal: ModalNav
) {
if (productId == null || supplierId == null || !this.selectedProduct) {
this.notificationService.error('Erreur', 'Identifiants produit/fournisseur manquants');
this.notificationService.error('Erreur', 'Identifiants produit ou fournisseur manquants');
return;
}
await this.editPrice(productId, supplierId, updateProductComponent);
updateProductComponent.priceForm.reset();
modal.isVisible = false;
this.onModalCancel(modal);
await this.fetchSuppliers();
}
@@ -150,8 +134,7 @@ export class SupplierTable implements OnInit {
};
await firstValueFrom(this.pricesService.patchPriceEndpoint(productId, supplierId, body));
this.notificationService.success('Success', 'Prix modifié');
} catch (e) {
console.error(e);
} catch {
this.notificationService.error('Erreur', 'Erreur lors de la modification');
}
}
@@ -162,7 +145,7 @@ export class SupplierTable implements OnInit {
await firstValueFrom(this.suppliersService.deleteProductToSupplierEndpoint(idSupplier, idProduct));
this.notificationService.success('Succès', 'Produit supprimé');
await this.fetchSuppliers();
} catch (e) {
} catch {
this.notificationService.error('Erreur', 'Impossible de supprimer le produit');
}
}