Skip to content

Commit fd68bd5

Browse files
committed
chore: functional update to v4
1 parent 9604544 commit fd68bd5

File tree

4 files changed

+195
-593
lines changed

4 files changed

+195
-593
lines changed

js/search.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ const search = instantsearch({
1414
routing: {
1515
stateMapping: {
1616
stateToRoute: function (uiState) {
17+
const state = uiState[algoliaConfig.index_name];
1718
return {
18-
query: uiState.query && uiState.query.replace(new RegExp('([^\\s])--', 'g'), '$1-'),
19-
type: uiState.menu && uiState.menu.type,
20-
tags: uiState.refinementList && uiState.refinementList.tags && uiState.refinementList.tags.join('~'),
21-
page: uiState.page,
19+
query: state.query && state.query.replace(new RegExp('([^\\s])--', 'g'), '$1-'),
20+
type: state.menu && state.menu.type,
21+
tags: state.refinementList && state.refinementList.tags && state.refinementList.tags.join('~'),
22+
page: state.page,
2223
};
2324
},
2425
routeToState: function (routeState) {
@@ -35,24 +36,27 @@ const search = instantsearch({
3536
}
3637

3738
return {
38-
query: routeState.query || '',
39-
menu: {
40-
type: routeState.type
41-
},
42-
refinementList: {
43-
tags: routeState.tags && routeState.tags.replace(/[\s-]+/g, ' ').split('~'),
44-
},
45-
page: routeState.page
46-
};
39+
[algoliaConfig.index_name]: {
40+
query: routeState.query || '',
41+
menu: {
42+
type: routeState.type
43+
},
44+
refinementList: {
45+
tags: routeState.tags && routeState.tags.replace(/[\s-]+/g, ' ').split('~'),
46+
},
47+
page: routeState.page
48+
}
49+
}
4750
},
4851
},
4952
},
50-
searchFunction: function(helper) {
51-
var searchResults = document.querySelector('#search-container');
53+
onStateChange: function({uiState, setUiState}) {
54+
const searchResults = document.querySelector('#search-container');
55+
const state = uiState[algoliaConfig.index_name];
5256

53-
if (helper.state.query === ''
54-
&& helper.state.hierarchicalFacetsRefinements.type === undefined
55-
&& (helper.state.disjunctiveFacetsRefinements.tags === undefined || helper.state.disjunctiveFacetsRefinements.tags.length === 0)
57+
if (state.query === ''
58+
&& state.hierarchicalFacetsRefinements.type === undefined
59+
&& (state.disjunctiveFacetsRefinements.tags === undefined || state.disjunctiveFacetsRefinements.tags.length === 0)
5660
) {
5761
searchResults.classList.add('hidden');
5862
return;
@@ -65,18 +69,18 @@ const search = instantsearch({
6569
}
6670

6771
// force focus to prevent algolia from updating the search field input with the modified value
68-
if (helper.state.query.match(/-/)) {
72+
if (state.query.match(/-/)) {
6973
document.getElementById('search_query_query').focus();
7074
}
7175

72-
if (helper.state.query.match(/^PKSA-.{14}$/) || helper.state.query.match(/^GHSA-.{14}$/) || helper.state.query.match(/^CVE-\d{4}-\d+$/)) {
73-
document.location.href = "/security-advisories/" + helper.state.query;
76+
if (state.query.match(/^PKSA-.{14}$/) || state.query.match(/^GHSA-.{14}$/) || state.query.match(/^CVE-\d{4}-\d+$/)) {
77+
document.location.href = "/security-advisories/" + state.query;
7478
}
7579

76-
helper.state.query = helper.state.query.replace(new RegExp('([^\\s])-', 'g'), '$1--');
80+
state.query = state.query.replace(new RegExp('([^\\s])-', 'g'), '$1--');
7781

78-
searchThrottle = setTimeout(function () {
79-
helper.search();
82+
searchThrottle = setTimeout(() => {
83+
setUiState({[algoliaConfig.index_name]: state});
8084
}, 300);
8185
},
8286
});

0 commit comments

Comments
 (0)