intershop-pwa icon indicating copy to clipboard operation
intershop-pwa copied to clipboard

explore: what if the ICM API could fetch multiple products at once?

Open dhhyi opened this issue 3 years ago • 3 comments

PR Type

[x] Other: Showcase

What Is the Current Behavior?

Feature request #764

What Is the New Behavior?

Solution if the API could actually fetch multiple products at once.

Does this PR Introduce a Breaking Change?

[ ] Yes [x] No

dhhyi avatar Jul 01 '21 14:07 dhhyi

@shauke I think this would actually be a nice feature to have for the ICM API. It shouldn't be any API break either 😉

dhhyi avatar Jul 01 '21 14:07 dhhyi

Waiting for ICM internal request to provide a multiple SKUs REST API call: https://dev.azure.com/intershop-com/Products/_workitems/edit/64436 https://dev.azure.com/intershop-com/Products/_workitems/edit/54822

shauke avatar Jan 06 '23 16:01 shauke

2 years later, this is still an interesting feature. We're using this with a slightly modified effect in PWA 3.2.0 to reduce the mass execution of product calls when working with multiple product carousels:

  loadProducts$ = createEffect(() =>
    this.actions$.pipe(
      ofType(loadProduct),
      // accumulate all actions to windows
      window(this.actions$.pipe(ofType(loadProduct), debounceTime(250))),
      mergeMap(window$ =>
        window$.pipe(
          mapToPayloadProperty('sku'),
          // take slices
          bufferCount(50),
          // filter out duplicates
          map(skus => skus.filter((v, i, a) => a.indexOf(v) === i)),
          filter(skus => !!skus.length),
          // make call
          mergeMap(skus =>
            this.productsService.getProducts(skus)?.pipe(
              concatMap(({ products }) => [...products.map(product => loadProductSuccess({ product }))]),
              mapErrorToAction(loadProductsBySkusFail, { skus })
            )
          )
        )
      )
    )
  );

philippsauer avatar Jul 03 '23 14:07 philippsauer