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

Commit

Permalink
fix(MultiIndex): derived helper were using main index specifics params (
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuret authored Apr 18, 2017
1 parent d4fc854 commit 991fea6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ jest.mock('algoliasearch-helper/src/algoliasearch.helper.js', () => {
);
Helper.prototype._dispatchAlgoliaResponse = function(state) {
state.forEach(s => {
this.emit('result', { query: s.state.query, index: s.state.index }, s);
this.emit(
'result',
{ query: s.state.query, page: s.state.page, index: s.state.index },
s
);
});
};
Helper.prototype.searchForFacetValues = () =>
Expand Down Expand Up @@ -58,6 +62,11 @@ describe('createInstantSearchManager', () => {
multiIndexContext: { targetedIndex: 'second' },
});

ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setPage(3),
multiIndexContext: { targetedIndex: 'first' },
});

expect(ism.store.getState().results).toBe(null);

return Promise.resolve().then(() => {
Expand All @@ -66,10 +75,12 @@ describe('createInstantSearchManager', () => {
const store = ism.store.getState();
expect(store.results.first).toEqual({
query: 'first query 1',
page: 3,
index: 'first',
});
expect(store.results.second).toEqual({
query: 'second query 1',
page: 0,
index: 'second',
});
expect(store.error).toBe(null);
Expand All @@ -87,10 +98,12 @@ describe('createInstantSearchManager', () => {
const store1 = ism.store.getState();
expect(store.results.first).toEqual({
query: 'first query 2',
page: 3,
index: 'first',
});
expect(store.results.second).toEqual({
query: 'second query 2',
page: 0,
index: 'second',
});
expect(store1.error).toBe(null);
Expand All @@ -114,6 +127,10 @@ describe('createInstantSearchManager', () => {
getSearchParameters: params => params.setQuery('second query 1'),
multiIndexContext: { targetedIndex: 'second' },
});
ism.widgetsManager.registerWidget({
getSearchParameters: params => params.setPage(3),
multiIndexContext: { targetedIndex: 'first' },
});

ism.onExternalStateUpdate({});

Expand All @@ -124,10 +141,12 @@ describe('createInstantSearchManager', () => {
const store = ism.store.getState();
expect(store.results.first).toEqual({
query: 'first query 1',
page: 3,
index: 'first',
});
expect(store.results.second).toEqual({
query: 'second query 1',
page: 0,
index: 'second',
});
expect(store.error).toBe(null);
Expand Down
42 changes: 27 additions & 15 deletions packages/react-instantsearch/src/core/createInstantSearchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,15 @@ export default function createInstantSearchManager(

function getSearchParameters() {
indexMapping = {};
const mainParameters = widgetsManager
const sharedParameters = widgetsManager
.getWidgets()
.filter(widget => Boolean(widget.getSearchParameters))
.filter(
widget =>
!widget.multiIndexContext ||
(widget.multiIndexContext &&
widget.multiIndexContext.targetedIndex === indexName)
)
.filter(widget => !widget.multiIndexContext)
.reduce(
(res, widget) => widget.getSearchParameters(res),
initialSearchParameters
);
indexMapping[mainParameters.index] = indexName;
indexMapping[sharedParameters.index] = indexName;

const derivatedWidgets = widgetsManager
.getWidgets()
Expand All @@ -97,25 +92,40 @@ export default function createInstantSearchManager(
[]
);

return { mainParameters, derivatedWidgets };
const mainIndexParameters = widgetsManager
.getWidgets()
.filter(widget => Boolean(widget.getSearchParameters))
.filter(
widget =>
widget.multiIndexContext &&
widget.multiIndexContext.targetedIndex === indexName
)
.reduce(
(res, widget) => widget.getSearchParameters(res),
sharedParameters
);

return { sharedParameters, mainIndexParameters, derivatedWidgets };
}

function search() {
const { mainParameters, derivatedWidgets } = getSearchParameters(
helper.state
);
const {
sharedParameters,
mainIndexParameters,
derivatedWidgets,
} = getSearchParameters(helper.state);

Object.values(derivedHelpers).forEach(d => d.detach());
derivedHelpers = {};

helper.setState(mainParameters);
helper.setState(sharedParameters);

derivatedWidgets.forEach(derivatedSearchParameters => {
const index = derivatedSearchParameters.targetedIndex;
const derivedHelper = helper.derive(sp => {
const derivedHelper = helper.derive(() => {
const parameters = derivatedSearchParameters.widgets.reduce(
(res, widget) => widget.getSearchParameters(res),
sp.setIndex(index)
sharedParameters.setIndex(index)
);
indexMapping[parameters.index] = index;
return parameters;
Expand All @@ -125,6 +135,8 @@ export default function createInstantSearchManager(
derivedHelpers[index] = derivedHelper;
});

helper.setState(mainIndexParameters);

helper.search();
}

Expand Down

0 comments on commit 991fea6

Please sign in to comment.