From 7ed61497d36f31a71d7f28692ad2a07e3e5ef4a0 Mon Sep 17 00:00:00 2001 From: Vaillant Samuel Date: Thu, 30 Apr 2020 12:36:31 +0200 Subject: [PATCH] fix: wip --- src/lib/__tests__/escape-highlight-test.js | 22 +++++++++++++++ src/lib/escape-highlight.ts | 12 +++++---- src/widgets/index/index.ts | 31 +++++++++++++--------- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/lib/__tests__/escape-highlight-test.js b/src/lib/__tests__/escape-highlight-test.js index fa52ace5c2..5354667144 100644 --- a/src/lib/__tests__/escape-highlight-test.js +++ b/src/lib/__tests__/escape-highlight-test.js @@ -246,4 +246,26 @@ describe('escapeHits()', () => { expect(hits).toEqual(output); }); + + it('should not mutate the hit', () => { + const hit = { + _highlightResult: { + foobar: { + value: '', + }, + }, + }; + + const hits = [hit]; + + escapeHits(hits); + + expect(hit).toEqual({ + _highlightResult: { + foobar: { + value: '', + }, + }, + }); + }); }); diff --git a/src/lib/escape-highlight.ts b/src/lib/escape-highlight.ts index a8550d7585..c10cbd2963 100644 --- a/src/lib/escape-highlight.ts +++ b/src/lib/escape-highlight.ts @@ -46,13 +46,15 @@ function recursiveEscape(input: any): any { export default function escapeHits(hits: Hit[]): Hit[] { if ((hits as any).__escaped === undefined) { - hits = hits.map(hit => { - if (hit._highlightResult) { - hit._highlightResult = recursiveEscape(hit._highlightResult); + // We don't overidde the value on hit because it will mutate the raw results + // instead we make a shallow copy and we assigin the escaped values on it. + hits = hits.map(({ _highlightResult, _snippetResult, ...hit }) => { + if (_highlightResult) { + hit._highlightResult = recursiveEscape(_highlightResult); } - if (hit._snippetResult) { - hit._snippetResult = recursiveEscape(hit._snippetResult); + if (_snippetResult) { + hit._snippetResult = recursiveEscape(_snippetResult); } return hit; diff --git a/src/widgets/index/index.ts b/src/widgets/index/index.ts index 49d8d2d83b..7de824cafe 100644 --- a/src/widgets/index/index.ts +++ b/src/widgets/index/index.ts @@ -70,7 +70,8 @@ export function isIndexWidget(widget: Widget): widget is Index { function getLocalWidgetsState( widgets: Widget[], - widgetStateOptions: WidgetStateOptions + widgetStateOptions: WidgetStateOptions, + initialUiState: IndexUiState = {} ): IndexUiState { return widgets .filter(widget => !isIndexWidget(widget)) @@ -80,7 +81,7 @@ function getLocalWidgetsState( } return widget.getWidgetState(uiState, widgetStateOptions); - }, {}); + }, initialUiState); } function getLocalWidgetsSearchParameters( @@ -406,17 +407,21 @@ const index = (props: IndexProps): Index => { // configuration of the widget is pushed in the URL. That's what we want to avoid. // https://github.com/algolia/instantsearch.js/pull/994/commits/4a672ae3fd78809e213de0368549ef12e9dc9454 helper.on('change', ({ state }) => { - if ( - instantSearchInstance.__isServerRendering && - !instantSearchInstance.started - ) { - return; - } - - localUiState = getLocalWidgetsState(localWidgets, { - searchParameters: state, - helper: helper!, - }); + // if ( + // instantSearchInstance.__isServerRendering && + // !instantSearchInstance.started + // ) { + // return; + // } + + localUiState = getLocalWidgetsState( + localWidgets, + { + searchParameters: state, + helper: helper!, + }, + localUiState + ); // We don't trigger an internal change when controlled because it // becomes the responsibility of `setUiState`.