Refactor PrestashopService and ps-product-dialog component to enhance API integration and improve product management functionality

This commit is contained in:
Vincent Guillet
2025-11-29 11:53:25 +01:00
parent 007cb34c81
commit d64fed8157
2 changed files with 268 additions and 129 deletions

View File

@@ -1,4 +1,5 @@
import {Component, Inject, OnInit, inject, OnDestroy} from '@angular/core';
// File: src/app/components/ps-product-dialog/ps-product-dialog.component.ts
import { Component, Inject, OnInit, inject, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { MatFormField, MatLabel } from '@angular/material/form-field';
@@ -20,6 +21,7 @@ import { catchError, forkJoin, of, Observable } from 'rxjs';
import { PsItem } from '../../interfaces/ps-item';
import { ProductListItem } from '../../interfaces/product-list-item';
import { PrestashopService } from '../../services/prestashop.serivce';
import { PsProduct } from '../../interfaces/ps-product'; // si tu as une interface dédiée
export type ProductDialogData = {
mode: 'create' | 'edit';
@@ -87,6 +89,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
// ---------- Helpers locaux ----------
// utilisé seulement pour pré-afficher un TTC approximatif à partir du HT de la liste
private toTtc(ht: number) {
return Math.round(((ht * 1.2) + Number.EPSILON) * 100) / 100;
}
@@ -148,6 +151,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
if (this.mode === 'edit' && this.productRow) {
const r = this.productRow;
// pré-remplissage rapide avec la ligne de liste (HT -> TTC approximatif)
const immediateTtc = r.priceHt == null ? 0 : this.toTtc(r.priceHt);
this.form.patchValue({
name: r.name,
@@ -158,11 +162,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
});
const details$ = this.ps.getProductDetails(r.id).pipe(
catchError(() => of({
id: r.id, name: r.name, description: '',
id_manufacturer: r.id_manufacturer, id_supplier: r.id_supplier,
id_category_default: r.id_category_default, priceHt: r.priceHt ?? 0
}))
catchError(() => of<PsProduct | null>(null))
);
const qty$ = this.ps.getProductQuantity(r.id).pipe(catchError(() => of(0)));
const imgs$ = this.ps.getProductImageUrls(r.id).pipe(catchError(() => of<string[]>([])));
@@ -172,20 +172,23 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
forkJoin({ details: details$, qty: qty$, imgs: imgs$, flags: flags$ })
.subscribe(({ details, qty, imgs, flags }) => {
const ttc = this.toTtc(details.priceHt ?? 0);
const baseDesc = this.cleanForTextarea(details.description ?? '');
const baseDesc = this.cleanForTextarea(details?.description ?? '');
this.lastLoadedDescription = baseDesc;
this.form.patchValue({
// description depuis le backend (déjà propre / nettoyée normalement)
description: baseDesc,
// flags
complete: flags.complete,
hasManual: flags.hasManual,
conditionLabel: flags.conditionLabel || '',
priceTtc: (ttc || this.form.value.priceTtc || 0),
// TTC "réel" calculé par le backend (si dispo) sinon on garde la valeur actuelle
priceTtc: (details?.priceTtc ?? this.form.value.priceTtc ?? 0),
quantity: qty,
categoryId: (details.id_category_default ?? this.form.value.categoryId) ?? null,
manufacturerId: (details.id_manufacturer ?? this.form.value.manufacturerId) ?? null,
supplierId: (details.id_supplier ?? this.form.value.supplierId) ?? null
// ids de ref issus du DTO backend
categoryId: (details?.categoryId ?? this.form.value.categoryId) ?? null,
manufacturerId: (details?.manufacturerId ?? this.form.value.manufacturerId) ?? null,
supplierId: (details?.supplierId ?? this.form.value.supplierId) ?? null
});
this.existingImageUrls = imgs;
@@ -203,7 +206,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
const fl = (ev.target as HTMLInputElement).files;
// Nettoyage des anciens objectURL
for(let url of this.previewUrls) {
for (let url of this.previewUrls) {
URL.revokeObjectURL(url);
}
this.previewUrls = [];
@@ -261,7 +264,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
}
}
// -------- Save / close inchangés (à part dto.images) --------
// -------- Save / close --------
save() {
if (this.form.invalid) return;
@@ -269,7 +272,7 @@ export class PsProductDialogComponent implements OnInit, OnDestroy {
const v = this.form.getRawValue();
const effectiveDescription = (v.description ?? '').trim() || this.lastLoadedDescription;
const dto = {
const dto: PsProduct = {
name: v.name!,
description: effectiveDescription,
categoryId: +v.categoryId!,