added totalQuantity in stock page
This commit is contained in:
@@ -6,10 +6,14 @@ import {StockForm} from "../stock-form/stock-form";
|
||||
import {NzDividerComponent} from "ng-zorro-antd/divider";
|
||||
import {FormsModule} from "@angular/forms";
|
||||
import {NzCheckboxComponent} from "ng-zorro-antd/checkbox";
|
||||
import {GetProductDto, ProductsService} from "../../services/api";
|
||||
import {GetProductDto, ProductsService, WarehouseproductsService} from "../../services/api";
|
||||
import {NzNotificationService} from "ng-zorro-antd/notification";
|
||||
import {firstValueFrom} from "rxjs";
|
||||
|
||||
interface ProductWithQuantity extends GetProductDto {
|
||||
totalQuantity?: number;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-stock-table',
|
||||
imports: [
|
||||
@@ -28,8 +32,9 @@ import {firstValueFrom} from "rxjs";
|
||||
|
||||
export class StockTable implements OnInit {
|
||||
private productsService = inject(ProductsService);
|
||||
private wareHousseProductsService = inject(WarehouseproductsService)
|
||||
private notificationService = inject(NzNotificationService)
|
||||
products = signal<GetProductDto[]>([]);
|
||||
products = signal<ProductWithQuantity[]>([]);
|
||||
productsLoading = signal<boolean>(false);
|
||||
updateProduct = viewChild.required<StockForm>('stockForm');
|
||||
modal = viewChild.required<ModalNav>('modalNav');
|
||||
@@ -84,19 +89,43 @@ export class StockTable implements OnInit {
|
||||
await this.fetchProducts();
|
||||
}
|
||||
|
||||
async fetchProducts() {
|
||||
this.productsLoading.set(true)
|
||||
|
||||
async fetchTotalQuantity(product: ProductWithQuantity) {
|
||||
try {
|
||||
const products = await firstValueFrom(this.productsService.getAllProductsEndpoint())
|
||||
this.products.set(products);
|
||||
const res = await firstValueFrom(
|
||||
this.wareHousseProductsService.getTotalQuantityEndpoint(product.id)
|
||||
);
|
||||
product.totalQuantity = res.totalQuantity;
|
||||
} catch (e) {
|
||||
product.totalQuantity = 0;
|
||||
this.notificationService.error(
|
||||
'Erreur',
|
||||
`Impossible de récupérer la quantité pour le produit n°${product.id}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fetchProducts() {
|
||||
this.productsLoading.set(true);
|
||||
try {
|
||||
const products = await firstValueFrom(this.productsService.getAllProductsEndpoint());
|
||||
|
||||
// transforme chaque produit en ProductWithQuantity
|
||||
const productsWithQuantity: ProductWithQuantity[] = products.map(p => ({ ...p }));
|
||||
|
||||
this.products.set(productsWithQuantity);
|
||||
|
||||
// récupérer la quantité pour chaque produit en parallèle
|
||||
await Promise.all(productsWithQuantity.map(p => this.fetchTotalQuantity(p)));
|
||||
|
||||
// déclencher la mise à jour du signal
|
||||
this.products.set([...productsWithQuantity]);
|
||||
} catch (e) {
|
||||
this.notificationService.error(
|
||||
'Erreur',
|
||||
'Erreur de communication avec l\'API'
|
||||
)
|
||||
);
|
||||
}
|
||||
this.productsLoading.set(false)
|
||||
this.productsLoading.set(false);
|
||||
}
|
||||
|
||||
async delete(productId:number) {
|
||||
|
||||
Reference in New Issue
Block a user