0

I have an angular application and inside of it I've setup ngrx data but I've noticed every time I call the entity service

i.e

this.purchaseOrderEntityService.update(updatedPurchaseOrder);

it seems to run twice and hit the http endpoint twice to that effect, can't seem to figure out why

this is one of the calls

 if (this.editItem) {
      this.subs.sink = this.purchaseService
        .updatePurchaseOrderItem(newPoItem)
        .subscribe((res: IPurchaseOrderItems) => {
          this.itemsService.updateItem(this.purchaseOrderItems, (u) => u.id === res.id, res);
          this.settingsService.addinfo("Updated");
          this.closeEditor(sender);

          this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
        });
    } else {
      newPoItem.purchaseOrderId = this.purchaseOrder?.id;
      this.subs.sink = this.purchaseService
        .addPurchaseOrderItem(newPoItem)
        .subscribe((res: IPurchaseOrderItems) => {
          this.itemsService.addItemToStart(this.purchaseOrderItems, res);
          this.settingsService.addinfo("Added");
          this.closeEditor(sender);

          this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
        });
    }

also call it when we delete an item

this.subs.sink = this.updatePurchaseOrderPrices().subscribe();

the full update function:

updatePurchaseOrderPrices(): Observable<IPurchaseOrder> {
    return this.purchaseOrderEntityService.entities$.pipe(
      map((po) => po.find((p) => p.id == this.purchaseOrder.id)),
      tap((order) => {
        const totalNetPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.netAmount, 0); // total amount of the purchase order items including net price
        const totalItemPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.totalAmount, 0); // total amount of the purchase order including tax
        const totalTaxAmount = totalItemPrice - totalNetPrice;
        const updPo: Partial<IPurchaseOrder> = {
          id: order.id,
          netAmount: totalNetPrice, // should be net item price
          taxAmount: totalTaxAmount,
          totalAmount: totalItemPrice, //* 0.2,
        };

        const updatePurchaseOrder: IPurchaseOrder = {
          ...order,
          ...updPo,
        };

        console.log(updatePurchaseOrder);

        this.purchaseOrderEntityService.update(updatePurchaseOrder);
      }),
      first()
    );
  }

The overrided update function

update(details: Update<IPurchaseOrder>): Observable<IPurchaseOrder> {
    return this.http
      .put<IPurchaseOrder>(
        `${this.baseUrl}/purchaseorder/poorder/${details.id}`,
        {
          ...details.changes,
        }
      )
      .pipe(
        map((res: IPurchaseOrder) => {
          return res;
        })
      );
  }

Any ideas what might be happening?

Corey James Carney
  • 167
  • 1
  • 2
  • 14
  • When is called this method? On click? If not, maybe your issue is in [Change Detection](https://blog.angular-university.io/how-does-angular-2-change-detection-really-work/) – ciekals11 Nov 10 '21 at 11:28
  • @ciekals11 I'm calling the update method whenever we add or remove an item from a list so that we can update the prices across the views, I've updated the question to show the method – Corey James Carney Nov 10 '21 at 11:29

0 Answers0