Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

feat(ais-dynamic-widgets): add implementation #922

Merged
merged 14 commits into from
Jul 9, 2021
Merged

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Mar 4, 2021

Summary

Adds a new widget ais-experimental-dynamic-widgets that can be used to conditionally render other widgets. This condition is based on the search results, by default results.facetOrdering.facet.values, but can be overridden using transformItems

Result

<template>
  <ais-experimental-dynamic-widgets :transform-items="transformItems">
    <ais-refinement-list attribute="test1" />
    <ais-menu attribute="test2" />
    <ais-panel>
      <ais-hierarchical-menu :attributes="hierarchicalAttributes" />
    </ais-panel>
  </ais-experimental-dynamic-widgets>
</template>

<script>
export default {
  data() {
    return {
      hierarchicalAttributes: ["test3", "unused"],
    };
  },
  methods(_attributes, { results }) {
    // modified if you want to use userData instead of facet ordering
    return results.userData[0].ordering;
  },
};
</script>

See also:

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 4, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit c0d1823:

Sandbox Source
vue-instantsearch-e-commerce Configuration

@Haroenv Haroenv force-pushed the poc/dynamic-widgets branch from 2dcf646 to 6651f6c Compare April 20, 2021 12:39
@Haroenv Haroenv force-pushed the poc/dynamic-widgets branch from 52c2475 to 27ecb1f Compare May 7, 2021 13:35
@Haroenv Haroenv changed the title poc: dynamic widgets feat(ais-dynamic-widgets): add implementation May 7, 2021
@Haroenv Haroenv force-pushed the poc/dynamic-widgets branch from 27ecb1f to 0992be7 Compare May 7, 2021 13:37
Adds a new widget _ais-experimental-dynamic-widgets_ that can be used to conditionally render other widgets. This condition is based on the search results. At the moment there isn't yet a default way to enforce facet ordering in the Algolia engine, thus the data available to `transformItems` is an empty array. This will change once the Algolia engine adds support for facet ordering and this widget will move out of experimental mode.

```vue
<template>
  <ais-experimental-dynamic-widgets :transform-items="transformItems">
    <ais-refinement-list attribute="test1" />
    <ais-menu attribute="test2" />
    <ais-panel>
      <ais-hierarchical-menu :attributes="hierarchicalAttributes" />
    </ais-panel>
  </ais-experimental-dynamic-widgets>
</template>

<script>
export default {
  data() {
    return {
      hierarchicalAttributes: ["test3", "unused"],
    };
  },
  methods(_attributes, { results }) {
    // add a condition based on the results, eg. if you add the ordering via a query rule:
    return results.userData[0].facetOrdering;
  },
};
</script>
```

See also: algolia/instantsearch#4687
@Haroenv Haroenv force-pushed the poc/dynamic-widgets branch from 0992be7 to 895435a Compare May 7, 2021 13:38
@Haroenv Haroenv marked this pull request as ready for review May 7, 2021 13:38
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com>
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request May 20, 2021
<!--
  Thanks for submitting a pull request!
  Please provide enough information so that others can review your pull request.
-->

**Summary**

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

Adds a new widget _ExperimentalDynamicWidgets_ that can be used to conditionally render other widgets. This condition is based on the search results. At the moment there isn't yet a default way to enforce facet ordering in the Algolia engine, thus the data available to `transformItems` is an empty array. This will change once the Algolia engine adds support for facet ordering and this widget will move out of experimental mode.

**Result**

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.

  You will be able to test out these changes on the deploy
  preview (address will be commented by a bot):

  1. the documentation site (/)
  2. a widget playground (/stories)
-->

```jsx
<DynamicWidgets
  transformItems={(_attributes, { results }) => {
    // add a condition based on the results, eg. if you add the ordering via a query rule:
    return results.userData[0].facetOrdering;
  }}
>
  <HierarchicalMenu
    attributes={[
      'hierarchicalCategories.lvl0',
      'hierarchicalCategories.lvl1',
      'hierarchicalCategories.lvl2',
      'hierarchicalCategories.lvl3',
    ]}
  />
  <Panel>
    <RefinementList attribute="brand" />
  </Panel>
  <Menu attribute="categories" />
</DynamicWidgets>
```

See also:

- algolia/instantsearch#4687
- algolia/vue-instantsearch#922
- DX-1665
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request May 20, 2021
<!--
  Thanks for submitting a pull request!
  Please provide enough information so that others can review your pull request.
-->

**Summary**

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

Adds a new widget _ExperimentalDynamicWidgets_ that can be used to conditionally render other widgets. This condition is based on the search results. At the moment there isn't yet a default way to enforce facet ordering in the Algolia engine, thus the data available to `transformItems` is an empty array. This will change once the Algolia engine adds support for facet ordering and this widget will move out of experimental mode.

**Result**

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.

  You will be able to test out these changes on the deploy
  preview (address will be commented by a bot):

  1. the documentation site (/)
  2. a widget playground (/stories)
-->

```jsx
<DynamicWidgets
  transformItems={(_attributes, { results }) => {
    // add a condition based on the results, eg. if you add the ordering via a query rule:
    return results.userData[0].facetOrdering;
  }}
>
  <HierarchicalMenu
    attributes={[
      'hierarchicalCategories.lvl0',
      'hierarchicalCategories.lvl1',
      'hierarchicalCategories.lvl2',
      'hierarchicalCategories.lvl3',
    ]}
  />
  <Panel>
    <RefinementList attribute="brand" />
  </Panel>
  <Menu attribute="categories" />
</DynamicWidgets>
```

See also:

- algolia/instantsearch#4687
- algolia/vue-instantsearch#922
- DX-1665
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/DynamicWidgets.js Show resolved Hide resolved
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/DynamicWidgets.js Outdated Show resolved Hide resolved
}
});

// by default, render everything, but hidden so that the routing doesn't disappear
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this behavior result in an unexpected UI flash were some widget will appear and then get removed because computed based on state.attributesToRender?

What if we don't render anything when we don't have a state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that the widgets we render initially are hidden, so this won't cause a flash, but possibly slightly delayed visuals compared to server side rendering (which solves this, as the widget information is available on that server + initial frontend render)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I think I rather meant: won't this results in an unnecessary search parameters computation because it mounts widgets that will likely get removed?

Why don't we render nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we render nothing, the parameters of the URL can't be applied unfortunately, this was the best workaround I found

src/components/__tests__/DynamicWidgets.js Show resolved Hide resolved
Haroenv added a commit to algolia/react-instantsearch that referenced this pull request Jul 1, 2021
* feat(DynamicWidgets): add implementation

<!--
  Thanks for submitting a pull request!
  Please provide enough information so that others can review your pull request.
-->

**Summary**

<!--
  Explain the **motivation** for making this change.
  What existing problem does the pull request solve?
  Are there any linked issues?
-->

Adds a new widget _ExperimentalDynamicWidgets_ that can be used to conditionally render other widgets. This condition is based on the search results. At the moment there isn't yet a default way to enforce facet ordering in the Algolia engine, thus the data available to `transformItems` is an empty array. This will change once the Algolia engine adds support for facet ordering and this widget will move out of experimental mode.

**Result**

<!--
  Demonstrate the code is solid.
  Example: The exact commands you ran and their output,
  screenshots / videos if the pull request changes UI.

  You will be able to test out these changes on the deploy
  preview (address will be commented by a bot):

  1. the documentation site (/)
  2. a widget playground (/stories)
-->

```jsx
<DynamicWidgets
  transformItems={(_attributes, { results }) => {
    // add a condition based on the results, eg. if you add the ordering via a query rule:
    return results.userData[0].facetOrdering;
  }}
>
  <HierarchicalMenu
    attributes={[
      'hierarchicalCategories.lvl0',
      'hierarchicalCategories.lvl1',
      'hierarchicalCategories.lvl2',
      'hierarchicalCategories.lvl3',
    ]}
  />
  <Panel>
    <RefinementList attribute="brand" />
  </Panel>
  <Menu attribute="categories" />
</DynamicWidgets>
```

See also:

- algolia/instantsearch#4687
- algolia/vue-instantsearch#922
- DX-1665

* fix TS

* feat: retrieve from results

* update dependencies

* read from facet ordering

* expose widget from root

* change error message and test more

* make and expose a dom component

* update bundlesizes

* clarify test

* redo some of the tests
src/components/DynamicWidgets.js Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Outdated Show resolved Hide resolved
src/components/__tests__/DynamicWidgets.js Show resolved Hide resolved
@Haroenv Haroenv requested a review from eunjae-lee July 8, 2021 15:28
@Haroenv Haroenv requested a review from eunjae-lee July 9, 2021 07:36
Copy link
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's go!

@Haroenv Haroenv merged commit 41ee905 into master Jul 9, 2021
@Haroenv Haroenv deleted the poc/dynamic-widgets branch July 9, 2021 09:08
Haroenv added a commit to algolia/instantsearch that referenced this pull request Dec 28, 2022
…arch#922)

* feat(ais-dynamic-widgets): add implementation

Adds a new widget _ais-experimental-dynamic-widgets_ that can be used to conditionally render other widgets. This condition is based on the search results. At the moment there isn't yet a default way to enforce facet ordering in the Algolia engine, thus the data available to `transformItems` is an empty array. This will change once the Algolia engine adds support for facet ordering and this widget will move out of experimental mode.

```vue
<template>
  <ais-experimental-dynamic-widgets :transform-items="transformItems">
    <ais-refinement-list attribute="test1" />
    <ais-menu attribute="test2" />
    <ais-panel>
      <ais-hierarchical-menu :attributes="hierarchicalAttributes" />
    </ais-panel>
  </ais-experimental-dynamic-widgets>
</template>

<script>
export default {
  data() {
    return {
      hierarchicalAttributes: ["test3", "unused"],
    };
  },
  methods: {
    transformItems(_attributes, { results }) {
      // add a condition based on the results, eg. if you add the ordering via a query rule:
      return results.userData[0].facetOrdering;
    }
  },
};
</script>
```

See also: #4687


Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants