Skip to content

Commit bc0f9e2

Browse files
samoussHaroenv
authored andcommitted
fix(connectSortBy): never update the initial index (#4015)
* fix(connectSortBy): never update the initial index * chore: review comment wording
1 parent e3c0629 commit bc0f9e2

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

src/connectors/sort-by/__tests__/connectSortBy-test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,49 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/sort-by/js/
373373
});
374374
expect(uiStateAfter).toBe(uiStateBefore);
375375
});
376+
377+
test('should use the top-level `indexName` for the initial index', () => {
378+
const render = () => {};
379+
const makeWidget = connectSortBy(render);
380+
const instantSearchInstance = instantSearch({
381+
indexName: 'initial_index_name',
382+
searchClient: { search() {} },
383+
});
384+
385+
const widget = makeWidget({
386+
items: [
387+
{ label: 'Sort products', value: 'initial_index_name' },
388+
{ label: 'Sort products by price', value: 'index_name_price' },
389+
{ label: 'Sort products by magic', value: 'index_name_magic' },
390+
],
391+
});
392+
393+
const helper = jsHelper({}, 'initial_index_name');
394+
helper.search = jest.fn();
395+
396+
// Simulate an URLSync
397+
helper.setQueryParameter('index', 'index_name_price');
398+
399+
widget.init({
400+
helper,
401+
state: helper.state,
402+
createURL: () => '#',
403+
onHistoryChange: () => {},
404+
instantSearchInstance,
405+
});
406+
407+
const actual = widget.getWidgetState(
408+
{},
409+
{
410+
searchParameters: helper.state,
411+
helper,
412+
}
413+
);
414+
415+
expect(actual).toEqual({
416+
sortBy: 'index_name_price',
417+
});
418+
});
376419
});
377420

378421
describe('getWidgetSearchParameters', () => {

src/connectors/sort-by/connectSortBy.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,35 @@ export default function connectSortBy(renderFn, unmountFn = noop) {
102102

103103
return {
104104
init({ helper, instantSearchInstance }) {
105-
const initialIndex = helper.state.index;
106-
const isInitialIndexInItems = find(
105+
const currentIndex = helper.state.index;
106+
const isCurrentIndexInItems = find(
107107
items,
108-
item => item.value === initialIndex
108+
item => item.value === currentIndex
109109
);
110110

111-
this.initialIndex = initialIndex;
111+
// The `initialIndex` is the one set at the top level not the one used
112+
// at `init`. The value of `index` at `init` could come from the URL. We
113+
// want the "real" initial value, this one should never change. If it changes
114+
// between the lifecycles of the widget the current refinement won't be
115+
// pushed into the `uiState`. Because we never push the "initial" value to
116+
// avoid to pollute the URL.
117+
// Note that it might be interesting to manage this at the state mapping
118+
// level and always push the index value into the `uiState`. It is a
119+
// breaking change.
120+
// @MAJOR
121+
this.initialIndex = instantSearchInstance.indexName;
112122
this.setIndex = indexName => {
113123
helper.setIndex(indexName).search();
114124
};
115125

116126
warning(
117-
isInitialIndexInItems,
118-
`The index named "${initialIndex}" is not listed in the \`items\` of \`sortBy\`.`
127+
isCurrentIndexInItems,
128+
`The index named "${currentIndex}" is not listed in the \`items\` of \`sortBy\`.`
119129
);
120130

121131
renderFn(
122132
{
123-
currentRefinement: initialIndex,
133+
currentRefinement: currentIndex,
124134
options: transformItems(items),
125135
refine: this.setIndex,
126136
hasNoResults: true,

0 commit comments

Comments
 (0)