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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

combinedAnswers.attributesToDisplay?.filter is not a function #6023

Open
1 task done
omidontop opened this issue Jan 30, 2024 · 4 comments
Open
1 task done

combinedAnswers.attributesToDisplay?.filter is not a function #6023

omidontop opened this issue Jan 30, 2024 · 4 comments
Labels
Library: Create InstantSearch App Issues in create-instantsearch-app Type: Feature

Comments

@omidontop
Copy link

omidontop commented Jan 30, 2024

馃悰 Current behavior

In a custom template, we used to be able to send in a dictionary as the attributesToDisplay in order to provide some additional attributes for the template engine to display. The referenced line breaks that possibility.

attributesToDisplay: combinedAnswers.attributesToDisplay?.filter(

Here's en example of how our config looks like:

    "attributesToDisplay": {
        "title": {
            "href": "url",
            "text": "hierarchy.lvl2",
            "highlight": true
        },
        "subtitle": {
            "text": "hierarchy.lvl1"
        },
    },

馃攳 Steps to reproduce

  1. Use a dictionary as the value of attributesToDisplay parameter.
  2. Run create-instantsearch-app.

Live reproduction

https://codesandbox.io/p/devbox/lh2cmc

馃挱 Expected behavior

Run ./create in a terminal and witness the error.

Package version

4.64.2

Operating system

macOS 14.2.1

Browser

Firefox 122.0

Code of Conduct

  • I agree to follow this project's Code of Conduct
@omidontop omidontop added the triage Issues to be categorized by the team label Jan 30, 2024
@Haroenv
Copy link
Contributor

Haroenv commented Jan 30, 2024

attributesToDisplay is meant to be an array of strings, I wonder if what you're looking for is a separate data-bag to store these more "static" information. Would that help your use case?

Something along the lines of:

{
  // ... other configuration
  "attributesToDisplay": [],
  "data": {
    "hit": {
      "title": {
        "href": "url",
        "text": "hierarchy.lvl2",
        "highlight": true
      },
      "subtitle": {
        "text": "hierarchy.lvl1"
      }
    }
  }
}

If you're interested in that, we'd be happy to see a PR adding a new data field from config that's copied over to the template variables.

@Haroenv Haroenv added Type: Feature Library: Create InstantSearch App Issues in create-instantsearch-app and removed triage Issues to be categorized by the team labels Jan 30, 2024
@omidontop
Copy link
Author

omidontop commented Jan 30, 2024

Thanks for the response @Haroenv. Well the benefit of being able to use a dictionary rather than an array is extending its usability which also eliminates the need for a loop when it comes to using them in the template. Unless there is an easier way to know whether attributesToDisplay contains title for example which seems to be a handlebars shortcoming. Consider this usage:

{{#if attributesToDisplay.title}}
{{#if attributesToDisplay.title.href}}
<a class="text-decoration-none" href="\{{attributesToDisplay.title.href}}">{{attributesToDisplay.title}}</a>
{{/if}}
{{/if}}

The attributesForFaceting seems to tolerate non-array structures:

{
    attributesForFaceting:
        Array.isArray(combinedAnswers.attributesForFaceting) &&
        combinedAnswers.attributesForFaceting.filter(
            (attribute) => attribute !== 'ais.dynamicWidgets'
        ),
}

@Haroenv
Copy link
Contributor

Haroenv commented Jan 31, 2024

I see your use case, but attributesForFaceting also doesn't tolerate non-array shapes, it in fact completely ignores the value and will put false if you put anything but an array of strings. I'm still happy to approve a purpose-built feature though. You're right that attributesForFaceting and attributesToDisplay are inconsistent, so I'll make a fix to reject non-arrays in both in the same way.

Haroenv added a commit that referenced this issue Jan 31, 2024
This makes it consistent with `attributesForFaceting`. See  #6023 for more info, although it doesn't change that use case.
@omidontop
Copy link
Author

@Haroenv Perfect! Since I'm using it in a custom template, I can live with that for now. Thank you :)

Haroenv added a commit that referenced this issue Feb 2, 2024
This makes it consistent with `attributesForFaceting`. See  #6023 for more info, although it doesn't change that use case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library: Create InstantSearch App Issues in create-instantsearch-app Type: Feature
Projects
None yet
Development

No branches or pull requests

2 participants