How to trigger multiple calls using dispatch function in ngrx

Viewed 257

I m using NGRX to dispatch the id's from the array but i need to make multiple calls at once not looping then on ngOninit

below is the code for dispatch:

loadEmloyeePricing(): void {
  let id = [];
  this.cartItem.forEach(a => {
    id.push(a.service.parameters.item_number)

  })
  // id -> list of array ids
  this.site$.pipe(take(1)).subscribe((site: number | null): void => {
    if (site) {
      this.store.dispatch(
        LOAD_EMPLOYEE_PRICING({
          payload: {
            pathParams: [site.toString(), id],
          },
        })
      );
    }
  });
}

ngOnInit(): void {
  this.loadEmloyeePricing();
}

Above in the code i need to pass the ids which is in array to dispatch function to make a single call in payload currently passing single id and looping the dispatch function which i dnt want.

i never done this please provide me some idea or solutions how to achieve this?

1 Answers

There's no way to dispatch multiple actions in NgRx (unless, as you do, dispatch them "one by one").

But it's really not the recommended way of doing this, instead consider this:

Dispatch a single action, called (just an example) LOAD_MULTIPLE_EMPLOYEE_PRICINGS that takes in an array of the ids to load.

Related