1.0.0 • Published 4 years ago

@coveops/dynamic-facet-generator v1.0.0

Weekly downloads
22
License
Apache-2.0
Repository
-
Last release
4 years ago

DynamicFacetGenerator

Generates Facets dynamicaly for a specific category

Disclaimer: This component was built by the community at large and is not an official Coveo JSUI Component. Use this component at your own risk.

Documentation

Visit documentation page for full component detail:

Getting Started

  1. Install the component into your project.
npm i @coveops/dynamic-facet-generator
  1. Use the Component or extend it

Typescript:

import { DynamicFacetGenerator, IDynamicFacetGeneratorOptions } from '@coveops/dynamic-facet-generator';

Javascript

const DynamicFacetGenerator = require('@coveops/dynamic-facet-generator').DynamicFacetGenerator;
  1. You can also expose the component alongside other components being built in your project.
export * from '@coveops/dynamic-facet-generator'
  1. Include the component in your template as follows:

Place the component in your markup:

<div class="CoveoDynamicFacetGenerator"></div>

Usage

Add the CoveoDynamicFacetGenerator along with the rest of the Facets.

<div class="coveo-facet-column">
  <div class="CoveoDynamicFacetManager">
    <div class="CoveoDynamicFacet" data-field="@prd_brand" data-title="Brand"></div>
    <div class="CoveoDynamicFacet" data-field="@prd_category" data-title="Category Type"></div>
    <div class="CoveoDynamicFacetGenerator" data-depends-on="@prd_category"></div>
  </div>
</div>

Before you initialize the UI, make sure you pass the dictionary to the DynamicFacetGenerator component

document.addEventListener('DOMContentLoaded', function () {
  Coveo.SearchEndpoint.configureSampleEndpointV2();
  Coveo.init(document.body, {
    DynamicFacetGenerator: {
      dictionary: {
        "Computer": ["Total Storage", "Processor Type"],
        "Phone": ["Screen Size", "Battery Life", "Color"]
      },
      transformer: (value) => {
        return { field: (`@prd_${value.replace(/\W/g, '')}` || '').toLowerCase(), facetTitle: value };
      },
    }
  });
})

Dictionary

If the dictionary is not defined at the moment you configure the component options, use the getDictionaryPromise option. That way, instead of passing the facet dictionary as an object, it is passed as an asynchronous function.

Transfomer

This function allows to transform the dictionary values into fields available in the index. In the above example, the component will generate Facets for @prd_total_storage and @prd_processor_type whenever the user selects the computer category.

Refer to the Reference Documentation page for more options.

Extending

Extending the component can be done as follows:

import { DynamicFacetGenerator, IDynamicFacetGeneratorOptions } from "@coveops/dynamic-facet-generator";

export interface IExtendedDynamicFacetGeneratorOptions extends IDynamicFacetGeneratorOptions {}

export class ExtendedDynamicFacetGenerator extends DynamicFacetGenerator {}

Contribute

  1. Clone the project
  2. Copy .env.dist to .env and update the COVEO_ORG_ID and COVEO_TOKEN fields in the .env file to use your Coveo credentials and SERVER_PORT to configure the port of the sandbox - it will use 8080 by default.
  3. Build the code base: npm run build
  4. Serve the sandbox for live development npm run serve