Skip to content

Commit 9e7d3d8

Browse files
samoussHaroenv
authored andcommitted
feat: update UiState definition (#4075)
This PR updates the type definitions to match the new format of the `UiState`. We introduce a new type called `IndexUiState`. The `UiState` is the global structure that represents the InstantSearch instance with each indices. The `IndexUiState` is the previous version of the `UiState`, it represents the state of an "index". Here is their definitions: ```ts export type IndexUiState = { query?: string; hitsPerPage?: number; // ... }; export type UiState = { [indexId: string]: IndexUiState; }; ``` The introduction of this type mainly impact the `StateMapping` & `RoutingManager`. Both have been updated. The changes applied to `RoutingManager` are there to make the tests pass don't pay too much attention to them since we'll rework this part soon. **Note**: Name are just a proposition, we could use something else like `WidgetsState`, ... ideas are welcome! We could rename `UiState` for consistency if we find a better name for both.
1 parent de00707 commit 9e7d3d8

File tree

8 files changed

+195
-105
lines changed

8 files changed

+195
-105
lines changed

src/lib/RoutingManager.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isEqual } from './utils';
33
import {
44
InstantSearch,
55
UiState,
6+
IndexUiState,
67
Router,
78
StateMapping,
89
Widget,
@@ -21,6 +22,7 @@ class RoutingManager implements Widget {
2122
private readonly stateMapping: StateMapping;
2223

2324
private isFirstRender: boolean = true;
25+
private indexId: string;
2426
private currentUiState: UiState;
2527
private initState?: UiState;
2628
private renderURLFromState?: (event: HelperChangeEvent) => void;
@@ -33,6 +35,7 @@ class RoutingManager implements Widget {
3335
this.router = router;
3436
this.stateMapping = stateMapping;
3537
this.instantSearchInstance = instantSearchInstance;
38+
this.indexId = this.instantSearchInstance.indexName;
3639
this.currentUiState = this.stateMapping.routeToState(this.router.read());
3740
}
3841

@@ -44,14 +47,15 @@ class RoutingManager implements Widget {
4447
uiState: UiState;
4548
}): SearchParameters {
4649
const widgets = this.instantSearchInstance.mainIndex.getWidgets();
50+
const indexUiState = uiState[this.indexId] || {};
4751

4852
return widgets.reduce((parameters, widget) => {
4953
if (!widget.getWidgetSearchParameters) {
5054
return parameters;
5155
}
5256

5357
return widget.getWidgetSearchParameters(parameters, {
54-
uiState,
58+
uiState: indexUiState,
5559
});
5660
}, currentSearchParameters);
5761
}
@@ -64,16 +68,18 @@ class RoutingManager implements Widget {
6468
const widgets = this.instantSearchInstance.mainIndex.getWidgets();
6569
const helper = this.instantSearchInstance.mainIndex.getHelper()!;
6670

67-
return widgets.reduce<UiState>((state, widget) => {
68-
if (!widget.getWidgetState) {
69-
return state;
70-
}
71-
72-
return widget.getWidgetState(state, {
73-
helper,
74-
searchParameters,
75-
});
76-
}, {});
71+
return {
72+
[this.indexId]: widgets.reduce<IndexUiState>((state, widget) => {
73+
if (!widget.getWidgetState) {
74+
return state;
75+
}
76+
77+
return widget.getWidgetState(state, {
78+
helper,
79+
searchParameters,
80+
});
81+
}, {}),
82+
};
7783
}
7884

7985
private setupRouting(state: SearchParameters): void {

src/lib/__tests__/InstantSearch-test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,9 @@ describe('createURL', () => {
10291029

10301030
expect(search.createURL()).toBe('http://algolia.com');
10311031
expect(router.createURL).toHaveBeenCalledWith({
1032-
query: 'Apple',
1032+
indexName: {
1033+
query: 'Apple',
1034+
},
10331035
});
10341036
});
10351037

@@ -1060,8 +1062,10 @@ describe('createURL', () => {
10601062

10611063
expect(search.createURL({ page: 5 })).toBe('http://algolia.com');
10621064
expect(router.createURL).toHaveBeenCalledWith({
1063-
query: 'Apple',
1064-
page: 5,
1065+
indexName: {
1066+
query: 'Apple',
1067+
page: 5,
1068+
},
10651069
});
10661070
});
10671071
});

src/lib/__tests__/RoutingManager-test.ts

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import qs from 'qs';
44
import { createSearchClient } from '../../../test/mock/createSearchClient';
55
import { createWidget } from '../../../test/mock/createWidget';
66
import { runAllMicroTasks } from '../../../test/utils/runAllMicroTasks';
7-
import { Router, Widget, StateMapping, RouteState } from '../../types';
7+
import { Router, Widget, UiState, StateMapping, RouteState } from '../../types';
88
import historyRouter from '../routers/history';
99
import RoutingManager from '../RoutingManager';
1010
import instantsearch from '../main';
@@ -119,7 +119,7 @@ describe('RoutingManager', () => {
119119
test('reads the state of widgets with a getWidgetState implementation', () => {
120120
const searchClient = createSearchClient();
121121
const search = instantsearch({
122-
indexName: '',
122+
indexName: 'indexName',
123123
searchClient,
124124
});
125125

@@ -135,7 +135,9 @@ describe('RoutingManager', () => {
135135
search.addWidget(widget);
136136

137137
const actualInitialState = {
138-
query: 'query',
138+
indexName: {
139+
query: 'query',
140+
},
139141
};
140142

141143
search.start();
@@ -156,7 +158,9 @@ describe('RoutingManager', () => {
156158
searchParameters: search.mainIndex.getHelper()!.state,
157159
});
158160

159-
expect(uiStates).toEqual(widgetState);
161+
expect(uiStates).toEqual({
162+
indexName: widgetState,
163+
});
160164

161165
expect(widget.getWidgetState).toHaveBeenCalledTimes(1);
162166
expect(widget.getWidgetState).toHaveBeenCalledWith(
@@ -171,7 +175,7 @@ describe('RoutingManager', () => {
171175
test('Does not read UI state from widgets without an implementation of getWidgetState', () => {
172176
const searchClient = createSearchClient();
173177
const search = instantsearch({
174-
indexName: '',
178+
indexName: 'indexName',
175179
searchClient,
176180
});
177181

@@ -182,7 +186,9 @@ describe('RoutingManager', () => {
182186
search.start();
183187

184188
const actualInitialState = {
185-
query: 'query',
189+
indexName: {
190+
query: 'query',
191+
},
186192
};
187193

188194
const router = new RoutingManager({
@@ -201,15 +207,17 @@ describe('RoutingManager', () => {
201207
searchParameters: search.mainIndex.getHelper()!.state,
202208
});
203209

204-
expect(uiStates).toEqual({});
210+
expect(uiStates).toEqual({
211+
indexName: {},
212+
});
205213
});
206214
});
207215

208216
describe('getAllSearchParameters', () => {
209217
test('should get searchParameters from widget that implements getWidgetSearchParameters', () => {
210218
const searchClient = createSearchClient();
211219
const search = instantsearch({
212-
indexName: '',
220+
indexName: 'indexName',
213221
searchClient,
214222
});
215223

@@ -221,7 +229,9 @@ describe('RoutingManager', () => {
221229
search.addWidget(widget);
222230

223231
const actualInitialState = {
224-
query: 'query',
232+
indexName: {
233+
query: 'query',
234+
},
225235
};
226236

227237
search.start();
@@ -299,7 +309,7 @@ describe('RoutingManager', () => {
299309
});
300310

301311
const search = instantsearch({
302-
indexName: 'instant_search',
312+
indexName: 'indexName',
303313
searchClient,
304314
routing: {
305315
router,
@@ -330,7 +340,9 @@ describe('RoutingManager', () => {
330340

331341
expect(router.write).toHaveBeenCalledTimes(1);
332342
expect(router.write).toHaveBeenCalledWith({
333-
q: 'q',
343+
indexName: {
344+
q: 'q',
345+
},
334346
});
335347

336348
done();
@@ -340,15 +352,15 @@ describe('RoutingManager', () => {
340352
test('should update the searchParameters on router state update', done => {
341353
const searchClient = createSearchClient();
342354

343-
let onRouterUpdateCallback: (args: object) => void;
355+
let onRouterUpdateCallback: (args: UiState) => void;
344356
const router = createFakeRouter({
345357
onUpdate: fn => {
346358
onRouterUpdateCallback = fn;
347359
},
348360
});
349361

350362
const search = instantsearch({
351-
indexName: 'instant_search',
363+
indexName: 'indexName',
352364
searchClient,
353365
routing: {
354366
router,
@@ -358,9 +370,10 @@ describe('RoutingManager', () => {
358370
const widget = {
359371
render: jest.fn(),
360372
getWidgetSearchParameters: jest.fn((searchParameters, { uiState }) =>
361-
searchParameters.setQuery(uiState.q)
373+
searchParameters.setQuery(uiState.query)
362374
),
363375
};
376+
364377
search.addWidget(widget);
365378

366379
search.start();
@@ -370,9 +383,11 @@ describe('RoutingManager', () => {
370383

371384
expect(search.mainIndex.getHelper()!.state.query).toBeUndefined();
372385

373-
// this simulates a router update with a uiState of {q: 'a'}
386+
// this simulates a router update with a uiState of {query: 'a'}
374387
onRouterUpdateCallback({
375-
q: 'a',
388+
indexName: {
389+
query: 'a',
390+
},
376391
});
377392

378393
search.once('render', () => {
@@ -393,14 +408,21 @@ describe('RoutingManager', () => {
393408

394409
const stateMapping = createFakeStateMapping({
395410
stateToRoute(uiState) {
396-
return {
397-
query: uiState.query && uiState.query.toUpperCase(),
398-
};
411+
return Object.keys(uiState).reduce((state, indexId) => {
412+
const indexState = uiState[indexId];
413+
414+
return {
415+
...state,
416+
[indexId]: {
417+
query: indexState.query && indexState.query.toUpperCase(),
418+
},
419+
};
420+
}, {});
399421
},
400422
});
401423

402424
const search = instantsearch({
403-
indexName: 'instant_search',
425+
indexName: 'indexName',
404426
searchFunction: helper => {
405427
helper.setQuery('test').search();
406428
},
@@ -431,7 +453,9 @@ describe('RoutingManager', () => {
431453
expect(search.mainIndex.getHelper()!.state.query).toEqual('test');
432454

433455
expect(router.write).toHaveBeenLastCalledWith({
434-
query: 'TEST',
456+
indexName: {
457+
query: 'TEST',
458+
},
435459
});
436460

437461
done();
@@ -446,7 +470,7 @@ describe('RoutingManager', () => {
446470
});
447471

448472
const search = instantsearch({
449-
indexName: 'instant_search',
473+
indexName: 'indexName',
450474
searchClient,
451475
routing: {
452476
stateMapping,
@@ -469,7 +493,9 @@ describe('RoutingManager', () => {
469493

470494
expect(router.write).toHaveBeenCalledTimes(1);
471495
expect(router.write).toHaveBeenLastCalledWith({
472-
query: 'Apple',
496+
indexName: {
497+
query: 'Apple',
498+
},
473499
});
474500

475501
await runAllMicroTasks();
@@ -481,7 +507,9 @@ describe('RoutingManager', () => {
481507

482508
expect(router.write).toHaveBeenCalledTimes(2);
483509
expect(router.write).toHaveBeenLastCalledWith({
484-
query: 'Apple',
510+
indexName: {
511+
query: 'Apple',
512+
},
485513
});
486514
});
487515

@@ -493,7 +521,7 @@ describe('RoutingManager', () => {
493521
});
494522

495523
const search = instantsearch({
496-
indexName: 'instant_search',
524+
indexName: 'indexName',
497525
searchFunction(helper) {
498526
// Force the value of the query
499527
helper.setQuery('Apple iPhone').search();
@@ -518,7 +546,9 @@ describe('RoutingManager', () => {
518546

519547
expect(router.write).toHaveBeenCalledTimes(1);
520548
expect(router.write).toHaveBeenLastCalledWith({
521-
query: 'Apple iPhone',
549+
indexName: {
550+
query: 'Apple iPhone',
551+
},
522552
});
523553

524554
// Trigger getConfiguration
@@ -528,7 +558,9 @@ describe('RoutingManager', () => {
528558

529559
expect(router.write).toHaveBeenCalledTimes(2);
530560
expect(router.write).toHaveBeenLastCalledWith({
531-
query: 'Apple iPhone',
561+
indexName: {
562+
query: 'Apple iPhone',
563+
},
532564
});
533565
});
534566

@@ -548,7 +580,7 @@ describe('RoutingManager', () => {
548580
});
549581

550582
const search = instantsearch({
551-
indexName: 'instant_search',
583+
indexName: 'indexName',
552584
searchClient,
553585
routing: {
554586
router,
@@ -571,15 +603,19 @@ describe('RoutingManager', () => {
571603

572604
expect(router.write).toHaveBeenCalledTimes(1);
573605
expect(router.write).toHaveBeenLastCalledWith({
574-
query: 'Apple',
606+
indexName: {
607+
query: 'Apple',
608+
},
575609
});
576610

577611
// Trigger an update - push a change
578612
fakeSearchBox.refine('Apple iPhone');
579613

580614
expect(router.write).toHaveBeenCalledTimes(2);
581615
expect(router.write).toHaveBeenLastCalledWith({
582-
query: 'Apple iPhone',
616+
indexName: {
617+
query: 'Apple iPhone',
618+
},
583619
});
584620

585621
await runAllMicroTasks();
@@ -596,7 +632,9 @@ describe('RoutingManager', () => {
596632

597633
expect(router.write).toHaveBeenCalledTimes(3);
598634
expect(router.write).toHaveBeenLastCalledWith({
599-
query: 'Apple',
635+
indexName: {
636+
query: 'Apple',
637+
},
600638
});
601639
});
602640
});

0 commit comments

Comments
 (0)