Skip to content

Commit

Permalink
MWPW-143591 Fix Contextual Search Duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
meganthecoder committed Mar 11, 2024
1 parent 05f767f commit 01f4a9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
10 changes: 5 additions & 5 deletions libs/blocks/article-feed/article-feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ export function readBlockConfig(block) {
* fetches blog article index.
* @returns {object} index with data and path lookup
*/
export async function fetchBlogArticleIndex() {
export async function fetchBlogArticleIndex(config) {
if (blogIndex.complete) return (blogIndex);
const pageSize = 500;
const { feed } = blogIndex.config;
const { feed } = config || blogIndex.config;
const queryParams = `?limit=${pageSize}&offset=${blogIndex.offset}`;
blogIndex.offset += pageSize;
const defaultPath = updateLinkWithLangRoot(`${getConfig().locale.contentRoot}/query-index.json`);
const indexPath = feed
? `${feed}${queryParams}`
: `${defaultPath}${queryParams}`;

if (blogIndex.complete) return (blogIndex);

return fetch(indexPath)
.then((response) => response.json())
.then((json) => {
Expand All @@ -90,8 +90,8 @@ export async function fetchBlogArticleIndex() {
blogIndex.data.push(post);
blogIndex.byPath[post.path.split('.')[0]] = post;
});

blogIndex.complete = complete;
blogIndex.offset = json.offset + pageSize;

return blogIndex;
});
Expand Down
47 changes: 23 additions & 24 deletions libs/blocks/gnav/gnav-contextual-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { getArticleTaxonomy, buildArticleCard } from '../article-feed/article-he
import { createTag, getConfig } from '../../utils/utils.js';
import { replaceKey } from '../../features/placeholders.js';

const LIMIT = 12;
let abortController;
let articles = [];
const LIMIT = 12;
let complete = false;
let lastSearch = null;

function highlightTextElements(terms, elements) {
Expand Down Expand Up @@ -52,33 +53,31 @@ function highlightTextElements(terms, elements) {
});
}

async function fetchResults(signal, terms) {
let data = [];
let complete = false;
const hits = [];
if (!articles.length) {
({ data } = await fetchBlogArticleIndex());
articles = data;
}
while (hits.length < LIMIT && !complete && !signal.aborted) {
articles.forEach((article) => {
if (hits.length === LIMIT) {
return;
}
const { category } = getArticleTaxonomy(article);
const text = [category, article.title, article.description].join(' ').toLowerCase();
if (terms.every((term) => text.includes(term))) {
hits.push(article);
}
});
if (hits.length < LIMIT && !complete) {
({ data, complete } = await fetchBlogArticleIndex());
async function fetchResults(signal, terms, config) {
let hits = [];

for (const article of articles) {
if (hits.length === LIMIT || signal.aborted) break;
const { category } = getArticleTaxonomy(article);
const text = [category, article.title, article.description].join(' ').toLowerCase();
if (terms.every((term) => text.includes(term))) {
hits.push(article);
}
}

if (!signal.aborted && (!articles.length || (hits.length !== LIMIT && !complete))) {
const index = await fetchBlogArticleIndex(config);
articles = index.data;
complete = index.complete;
hits = await fetchResults(signal, terms, config);
}

return hits;
}

export default async function onSearchInput({ value, resultsEl, searchInputEl, advancedSearchEl }) {
export default async function onSearchInput(
{ value, resultsEl, searchInputEl, advancedSearchEl, contextualConfig: config },
) {
if (!value.length) {
resultsEl.innerHTML = '';
searchInputEl.classList.remove('gnav-search-input--isPopulated');
Expand All @@ -99,7 +98,7 @@ export default async function onSearchInput({ value, resultsEl, searchInputEl, a
const terms = value.toLowerCase().split(' ').filter(Boolean);
if (!terms.length) return;

const hits = await fetchResults(abortController.signal, terms);
const hits = await fetchResults(abortController.signal, terms, config);

if (currentSearch === lastSearch) {
if (!hits.length) {
Expand Down
16 changes: 13 additions & 3 deletions libs/blocks/gnav/gnav.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,24 @@ class Gnav {
this.searchType = SEARCH_TYPE_CONTEXTUAL;
}

const advancedSearchEl = searchBlock.querySelector('a');
const advancedSearchEl = searchBlock.querySelector('a:not([href$=".json"])');
let advancedSearchWrapper = null;
if (advancedSearchEl) {
advancedSearchWrapper = createTag('li', null, advancedSearchEl);
}

const contextualConfig = {};
[...searchBlock.children].forEach(({ children }, index) => {
if (index === 0 || children.length !== 2) return;
const key = children[0].textContent?.toLowerCase();
const link = children[1].querySelector('a');
const value = link ? link.href : children[1].textContent;
contextualConfig[key] = value;
});

const label = searchBlock.querySelector('p').textContent;
const searchEl = createTag('div', { class: `gnav-search ${isContextual ? SEARCH_TYPE_CONTEXTUAL : ''}` });
const searchBar = this.decorateSearchBar(label, advancedSearchWrapper);
const searchBar = this.decorateSearchBar(label, advancedSearchWrapper, contextualConfig);
const searchButton = createTag(
'button',
{
Expand All @@ -395,7 +404,7 @@ class Gnav {
return searchEl;
};

decorateSearchBar = (label, advancedSearchEl) => {
decorateSearchBar = (label, advancedSearchEl, contextualConfig) => {
const searchBar = createTag('aside', { id: 'gnav-search-bar', class: 'gnav-search-bar' });
const searchField = createTag('div', { class: 'gnav-search-field' }, SEARCH_ICON);
const searchInput = createTag('input', {
Expand All @@ -417,6 +426,7 @@ class Gnav {
locale,
searchInputEl: searchInput,
advancedSearchEl,
contextualConfig, // ToDo: Pick back up here
});
});

Expand Down

0 comments on commit 01f4a9d

Please sign in to comment.