Skip to content

Commit

Permalink
fix: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
samouss committed Apr 30, 2020
1 parent 56f68c9 commit 7ed6149
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
22 changes: 22 additions & 0 deletions src/lib/__tests__/escape-highlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,26 @@ describe('escapeHits()', () => {

expect(hits).toEqual(output);
});

it('should not mutate the hit', () => {
const hit = {
_highlightResult: {
foobar: {
value: '<script>__ais-highlight__foo__/ais-highlight__</script>',
},
},
};

const hits = [hit];

escapeHits(hits);

expect(hit).toEqual({
_highlightResult: {
foobar: {
value: '<script>__ais-highlight__foo__/ais-highlight__</script>',
},
},
});
});
});
12 changes: 7 additions & 5 deletions src/lib/escape-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 18 additions & 13 deletions src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -80,7 +81,7 @@ function getLocalWidgetsState(
}

return widget.getWidgetState(uiState, widgetStateOptions);
}, {});
}, initialUiState);
}

function getLocalWidgetsSearchParameters(
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 7ed6149

Please sign in to comment.