Skip to Content
0
Jun 14, 2020 at 03:27 PM

Retrieve list of available facets in Spartacus

685 Views Last edit Jun 14, 2020 at 03:29 PM 2 rev

In `spartacus-storefront` how can I retrieve the list of available facets? I took a look in the source code at `product/product-list/product-facet-navigation`[1] component which internally uses the `ProductFacetService`[2] to fetch the list of facets which are going to be displayed in the navigation component in the side of the category page. At line `42`:

      /**
   * Observes the facets and active facets for the given page. The facet data
   * is provided in a `FacetList`.
   */

  readonly facetList$: Observable<FacetList> = this.searchResult$.pipe(
    map(
      (result: ProductSearchPage) =>
        ({
          facets: result.facets,
          activeFacets: result.breadcrumbs,
        } as FacetList)
    )
  );

And in the FacetService [3] they get the list of facets like this:

  facetList$: Observable<FacetList> = this.productFacetService.facetList$.pipe(
    tap((facetList) => {
      facetList.facets.forEach((facet) => this.initialize(facet));
    })
  );

And iterate over the facets list in the model like this:

  <cx-facet
    *ngFor="let facet of facets"
    #facetRef
    [facet]="facet"
    [cxFocus]="{ lock: true, trap: true, autofocus: 'a' }"
    (unlock)="expandFacetGroup(facet, facetRef)"
    [class.expanded]="isExpanded(facet) | async"
    [class.collapsed]="isCollapsed(facet) | async"
  ></cx-facet>
</div>

I want to build a custom component that display a part of the facets available in the left side navigation component. The FacetService or FacetProductService used by built in component seem to not be available for import. And implementing the mechanism from FacetProductService for retrieving the facets seems like a tedious job. Isn't there an easier mechanism for retrieving the facets list?

[1] - https://github.com/SAP/spartacus/tree/adb901bf44da5806662465a1c8c39916c3829f51/projects/storefrontlib/src/cms-components/product/product-list/product-facet-navigation

[2] - https://github.com/SAP/spartacus/blob/adb901bf44da5806662465a1c8c39916c3829f51/projects/storefrontlib/src/cms-components/product/product-list/product-facet-navigation/services/product-facet.service.ts

[3] - https://github.com/SAP/spartacus/blob/e24033a8627cbcce97ce676a5675ce6aa338f689/projects/storefrontlib/src/cms-components/product/product-list/product-facet-navigation/services/facet.service.ts