algolia / angular-instantsearch

⚡️Lightning-fast search for Angular apps, by Algolia
https://algolia.com/doc/deprecated/instantsearch/angular/v4/api-reference/instantsearch/
MIT License
261 stars 73 forks source link

StateToRoute method not updating url if searching in multiple indexes #953

Closed IvanVolkov42 closed 2 years ago

IvanVolkov42 commented 2 years ago

Hi, i am trying to use stateMapping like this:

defineConfig() {
    const collectionName = this.indexName; // it's main index name, for example companies
    return {
      indexName: collectionName,
      searchClient: this.searchClient,
      routing: {
        stateMapping: {
          stateToRoute(uiState) {
            const indexUiState = uiState[collectionName];
            return {
              q: indexUiState.query,
              page: indexUiState.page,
            };
          },
          routeToState(routeState) {
            return {
              [collectionName]: {
                query: routeState.q,
                page: routeState.page,
              }
            };
          }
        }
      }
    };
  }

I use search in two collections companies and workers via ais-search-box, so if enter in searchbox text and trigger search, it searchs in both collections, but ?q parameter not passed to URL. But if manually add ?q=test to URL and click on it, after page reloaded it passed to searchbox throw routeToState and search is triggered in both collections and query parameter passed to searchbox. So if i look on my uiState, it looks like this: companies: {} workers: {query: 'test'} i don't know why it's working in case routeToState and don't working in case stateToRoute i am using "algoliasearch": "^4.11.0", "instantsearch.js": "^4.32.0", "angular-instantsearch": "^4.0.1",

Haroenv commented 2 years ago

Can you create what you have so far in a sandbox environment? We have a template available for that purpose here. Thanks!

IvanVolkov42 commented 2 years ago

@Haroenv while creating example i found the reason of my problem: https://codesandbox.io/s/compassionate-diffie-61v3x4?file=/src/app/app.component.ts as you can see i define in config index "actors" and then in html use actors and players as ais-index and routing not working, but if in config change index name to instant_search routing starts to working. So the question is why can't i use one of index names, that i will actually search in, as index name in config? In case of using instant_search as a index in config looks like strange, because i want to search in other collections(actors and players)

IvanVolkov42 commented 2 years ago

@Haroenv Sorry, i just understoud, that i was using indexes wrongly:) Will close issue