Skip to content

Commit 5c1fc3f

Browse files
committed
Fix search error
Resolves #179
1 parent 5eb4aef commit 5c1fc3f

File tree

2 files changed

+16
-33
lines changed

2 files changed

+16
-33
lines changed

src/js/vendor/page-search.js

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,6 @@
1212

1313
let lastRenderArgs
1414

15-
const transformItems = (items) => {
16-
return items.map((item) => {
17-
let label = Object.keys(item.hierarchy).reduce((acc, key) => {
18-
const highlight = item._highlightResult.hierarchy[key]
19-
if (highlight && highlight.matchLevel !== 'none') {
20-
return item._highlightResult.hierarchy[key]
21-
}
22-
return acc
23-
}, '')
24-
if (!label) {
25-
label = Object.keys(item.hierarchy).reduce((acc, key) => {
26-
const highlight = item._highlightResult.hierarchy[key]
27-
if (highlight) {
28-
return item._highlightResult.hierarchy[key]
29-
}
30-
return acc
31-
}, '')
32-
}
33-
return {
34-
...item,
35-
_highlightResult: {
36-
...item._highlightResult,
37-
label: label,
38-
},
39-
}
40-
})
41-
}
42-
4315
const infiniteHits = instantsearch.connectors.connectInfiniteHits((renderArgs, isFirstRender) => {
4416
const { hits, showMore, widgetParams } = renderArgs
4517
const { container } = widgetParams
@@ -51,7 +23,7 @@
5123
container.appendChild(sentinel)
5224
return
5325
}
54-
const _hits = transformItems(hits)
26+
const _hits = [...hits]
5527
if (container.querySelector('#page-showmore')) {
5628
container.removeChild(container.querySelector('#page-showmore'))
5729
}
@@ -72,6 +44,7 @@
7244
.map((hit) => {
7345
let content = ''
7446
let breadcrumbs = ''
47+
let label = ''
7548

7649
if (hit.content) {
7750
content = `<p class="hit-description">
@@ -90,10 +63,20 @@
9063
</div>`
9164
}
9265

66+
label = Object.keys(hit.hierarchy)
67+
.map((key, index) => {
68+
if (index > 0 && hit) {
69+
return instantsearch.highlight({ hit: hit, attribute: 'hierarchy.' + key })
70+
}
71+
return null
72+
})
73+
.filter((item) => !!item)
74+
.join(' - ')
75+
9376
return `<li>
9477
<a href="${hit.url}" class="ais-Hits-item">
9578
<div class="hit-name">
96-
${instantsearch.highlight({ hit: hit, attribute: 'label' })}
79+
${label}
9780
</div>
9881
${breadcrumbs}
9982
${content}

src/js/vendor/search.bundle.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@
7070
.join(' > ')}
7171
</div>`
7272
}
73-
74-
label = Object.keys(hit._highlightResult.hierarchy)
73+
label = Object.keys(hit.hierarchy)
7574
.map((key, index) => {
76-
if (index > 0) {
75+
if (index > 0 && hit) {
7776
return instantsearch.highlight({ hit: hit, attribute: 'hierarchy.' + key })
7877
}
7978
return null
8079
})
8180
.filter((item) => !!item)
8281
.join(' - ')
82+
8383
console.log(label)
8484
return `<li>
8585
<a href="${hit.url}" class="ais-Hits-item">

0 commit comments

Comments
 (0)