Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(queryRules): add queryRuleCustomData widget #3600

Merged

Conversation

francoischalifour
Copy link
Member

@francoischalifour francoischalifour commented Mar 13, 2019

This is the 2nd PR in the series "Merchandized Query Rules".

Summary

This creates the queryRuleCustomData widget, responsible for showing custom data (userData) coming from Query Rules.

Current solution

Right now, we can leverage the connectHits connector to show a banner with InstantSearch.js.

const promotedResults = instantsearch.connectors.connectHits(
  (options, isFirstRender) => {
    const { widgetParams, results = {} } = options;
    const { container } = widgetParams;

    if (!results.userData) {
      container.innerHTML = '';
      return;
    }

    const [data] = results.userData;

    container.innerHTML = `<img src="${data.banner}" alt="${data.link}">`;
  }
);

search.addWidget(
  promotedResults({
    container: document.querySelector('#promoted-results'),
  })
);

See live example →

The goal of the widget is to simplify this solution.

Widget usage

Given this userData:

{
  "banner": "https://website.com/image.png",
  "title": "This is a revolution.",
  "link": "https://website.com/"
}

When the query rule is matched, the engine returns:

[
  {
    "banner": "https://website.com/image.png",
    "title": "This is a revolution.",
    "link": "https://website.com/"
  }
]

You would use the queryRuleCustomData widget:

instantsearch.widgets.queryRuleCustomData({
  container: widgetContainer,
  transformItems: items => items[0],
  templates: {
    default({ title, banner, link }) {
      if (!banner) {
        return '';
      }

      return `
        <h2>${title}</h2>

        <a href="${link}">
          <img src="${banner}" alt="${title}">
        </a>
      `;
    },
  },
});

API

transformItems

object[] => any – defaults to items => items

The function to change the data sent by the engine before giving it to the template.

container

string | HTMLElement – no default

The container of the widget.

templates

object – defaults to { default: '' }

The templates to display for the custom data (userData).

Stories

See stories →

Related

@francoischalifour francoischalifour requested a review from a team March 13, 2019 11:39
@algobot
Copy link
Contributor

algobot commented Mar 13, 2019

Deploy preview for instantsearchjs ready!

Built with commit 2684259

https://deploy-preview-3600--instantsearchjs.netlify.com

@francoischalifour francoischalifour force-pushed the feat/mqr-queryRuleCustomData branch 2 times, most recently from e3f8f79 to 01dac7f Compare March 14, 2019 12:40
@francoischalifour francoischalifour changed the base branch from chore/mqr-storybook-playgrounds to feat/mqr-connectQueryRules March 14, 2019 12:45
@francoischalifour francoischalifour changed the base branch from feat/mqr-connectQueryRules to chore/mqr-storybook-playgrounds March 14, 2019 12:45
@francoischalifour
Copy link
Member Author

francoischalifour commented Mar 14, 2019

GitHub seems to also display the changes introduced in #3599, that's weird.

@Haroenv
Copy link
Contributor

Haroenv commented Mar 14, 2019

These changes seem to be a different PR? Not sure what to review here

@Haroenv
Copy link
Contributor

Haroenv commented Mar 14, 2019

I think the base might be wrong :)

@francoischalifour
Copy link
Member Author

francoischalifour commented Mar 14, 2019

As said in #3600 (comment) I think that's a GitHub bug. You can ignore the changes related to Storybook.

Edit: I rebased again, should be fixed!

@francoischalifour
Copy link
Member Author

francoischalifour commented Mar 20, 2019

Without custom CSS classes, the inserted div container is not easily customizable because nothing identifies it.

I think we should create an InstantSearch.css spec for this widget with the following markup:

<div class="ais-QueryRuleCustomData"></div>

It would imply adding a cssClasses option to the widget, like in most widgets.

WDYT?

@Haroenv
Copy link
Contributor

Haroenv commented Mar 20, 2019

makes sense to me!

src/lib/utils/isDomElement.ts Outdated Show resolved Hide resolved
@francoischalifour francoischalifour force-pushed the chore/mqr-storybook-playgrounds branch 2 times, most recently from d1321b2 to a624a14 Compare March 22, 2019 13:58
@francoischalifour francoischalifour merged commit ff5d37e into chore/mqr-storybook-playgrounds Mar 22, 2019
francoischalifour added a commit that referenced this pull request Mar 22, 2019
* chore(storybook): add support for playgrounds (#3599)

* feat(queryRules): add queryRuleCustomData widget (#3600)
francoischalifour added a commit that referenced this pull request Mar 22, 2019
* feat(queryRules): add connectQueryRules connector

* chore(storybook): add support for playgrounds (#3599)

* feat(queryRules): add queryRuleCustomData widget (#3600)
francoischalifour added a commit that referenced this pull request Apr 11, 2019
# [3.3.0](v3.2.1...v3.3.0) (2019-04-11)

### Bug Fixes

* **connectQueryRules:** improve tracked refinement type ([#3648](#3648)) ([e16ad57](e16ad57))
* **currentRefinements:** don't rely on  ([#3672](#3672)) ([cd64bcf](cd64bcf))
* **queryRuleCustomData:** add default template ([#3650](#3650)) ([83e9eaa](83e9eaa))
* **QueryRuleCustomData:** pass data as object to templates ([#3647](#3647)) ([b8f8b4e](b8f8b4e))
* **queryRules:** fix types and stories ([#3670](#3670)) ([ba6e2e6](ba6e2e6))
* **routing:** apply windowTitle on first load ([#3669](#3669)) ([d553502](d553502)), closes [#3667](#3667)
* **routing:** support parsing URLs with up to 100 refinements ([#3671](#3671)) ([6ddcfb6](6ddcfb6))
* **RoutingManager:** avoid stale uiState ([#3630](#3630)) ([e1588aa](e1588aa))
* **types:** improve InstantSearch types ([#3651](#3651)) ([db9b91e](db9b91e))
* **ua:** Update the User-Agent to use the new format ([#3616](#3616)) ([ab84c57](ab84c57))

### Features

* **infiniteHits:** add previous button ([#3645](#3645)) ([2c9e38d](2c9e38d))
* **queryRules:** add connectQueryRules connector ([#3597](#3597)) ([924cd99](924cd99)), closes [#3599](#3599) [#3600](#3600)
* **queryRules:** add context features to Query Rules ([#3617](#3617)) ([922879e](922879e)), closes [#3602](#3602)

### Reverts

* feat(infiniteHits): add previous button ([214c0fc](214c0fc))
@francoischalifour francoischalifour deleted the feat/mqr-queryRuleCustomData branch May 29, 2019 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants