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

Custom AlgoliaWidget with multi index #71

Closed
Geoffrey63 opened this issue Feb 8, 2019 · 1 comment
Closed

Custom AlgoliaWidget with multi index #71

Geoffrey63 opened this issue Feb 8, 2019 · 1 comment

Comments

@Geoffrey63
Copy link

Hi,

I use a collectionView (HitCollectionWidget) to display a collection of products.
I need to sort those items by price.

As seen in #34, i switched to a multi-index configuration in order to be able to switch from the regular index to the price-sorted index.

For some the HitsCollectionWidget and the SearchBarWidget, I had to specify the used index, using getSearched to retrieve the index :

searchBar.index = (InstantSearch.shared.getSearcher(named: algoliaBackOfficeOfferChannel)?.indexName)!

Everything works fine but I have an issue on a custom Widget meant to dispaly the number of results that keeps displaying 0 résultat (it works fine with a single index).
I changed from AlgoliaWidget to AlgoliaIndexWidget and set the index as this :

nbResultsLabel.index = (InstantSearch.shared.getSearcher(named: algoliaBackOfficeOfferChannel)?.indexName)!

Here is the code of this widget :

import UIKit
import InstantSearch
import InstantSearchCore

class NbResultsWidget: UILabel, AlgoliaIndexWidget, ResultingDelegate {
    
    @IBInspectable public var index: String = ""
    @IBInspectable public var variant: String = ""
    
    func on(results: SearchResults?, error: Error?, userInfo: [String: Any]) {
        
        guard let results = results else {
            return
        }
        
        if results.nbHits > 1 {
            self.text = "\(results.nbHits) résultats"
        } else {
            self.text = "\(results.nbHits) résultat"
        }
    }
}

Did I miss something in the widget configuration ?
Thank you for your help.

@spinach
Copy link
Contributor

spinach commented Feb 11, 2019

Hi @Geoffrey63 ,

Thanks a lot for the detailed explanation of your issue. We're working on a new version of InstantSearch which will simplify its customisation and extensibility.

In the meantime, there is another person which had a similar issue to yours, and you can see the answer here #43 (comment). Also make sure you specify the index value before registering your widgets to InstantSearch. I hope that answers your question!

@spinach spinach closed this as completed Feb 11, 2019
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

No branches or pull requests

2 participants