Skip to content

Commit

Permalink
Fix search error
Browse files Browse the repository at this point in the history
Resolves #179
  • Loading branch information
oodamien committed Oct 13, 2023
1 parent 5eb4aef commit 5c1fc3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
43 changes: 13 additions & 30 deletions src/js/vendor/page-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,6 @@

let lastRenderArgs

const transformItems = (items) => {
return items.map((item) => {
let label = Object.keys(item.hierarchy).reduce((acc, key) => {
const highlight = item._highlightResult.hierarchy[key]
if (highlight && highlight.matchLevel !== 'none') {
return item._highlightResult.hierarchy[key]
}
return acc
}, '')
if (!label) {
label = Object.keys(item.hierarchy).reduce((acc, key) => {
const highlight = item._highlightResult.hierarchy[key]
if (highlight) {
return item._highlightResult.hierarchy[key]
}
return acc
}, '')
}
return {
...item,
_highlightResult: {
...item._highlightResult,
label: label,
},
}
})
}

const infiniteHits = instantsearch.connectors.connectInfiniteHits((renderArgs, isFirstRender) => {
const { hits, showMore, widgetParams } = renderArgs
const { container } = widgetParams
Expand All @@ -51,7 +23,7 @@
container.appendChild(sentinel)
return
}
const _hits = transformItems(hits)
const _hits = [...hits]
if (container.querySelector('#page-showmore')) {
container.removeChild(container.querySelector('#page-showmore'))
}
Expand All @@ -72,6 +44,7 @@
.map((hit) => {
let content = ''
let breadcrumbs = ''
let label = ''

if (hit.content) {
content = `<p class="hit-description">
Expand All @@ -90,10 +63,20 @@
</div>`
}

label = Object.keys(hit.hierarchy)
.map((key, index) => {
if (index > 0 && hit) {
return instantsearch.highlight({ hit: hit, attribute: 'hierarchy.' + key })
}
return null
})
.filter((item) => !!item)
.join(' - ')

return `<li>
<a href="${hit.url}" class="ais-Hits-item">
<div class="hit-name">
${instantsearch.highlight({ hit: hit, attribute: 'label' })}
${label}
</div>
${breadcrumbs}
${content}
Expand Down
6 changes: 3 additions & 3 deletions src/js/vendor/search.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@
.join(' > ')}
</div>`
}

label = Object.keys(hit._highlightResult.hierarchy)
label = Object.keys(hit.hierarchy)
.map((key, index) => {
if (index > 0) {
if (index > 0 && hit) {
return instantsearch.highlight({ hit: hit, attribute: 'hierarchy.' + key })
}
return null
})
.filter((item) => !!item)
.join(' - ')

console.log(label)
return `<li>
<a href="${hit.url}" class="ais-Hits-item">
Expand Down

0 comments on commit 5c1fc3f

Please sign in to comment.