Skip to content

Commit 70b32cd

Browse files
committed
MAGE-899 clean up transformSources / buildAutocompleteOptions
1 parent af3f579 commit 70b32cd

File tree

1 file changed

+47
-52
lines changed

1 file changed

+47
-52
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ define([
132132
buildAutocompleteOptions(searchClient, sources, plugins) {
133133
const debounced = this.debounce(items => Promise.resolve(items), DEBOUNCE_MS);
134134

135-
const autocompleteConfig = this.transformSources(searchClient, sources);
136-
137135
let options = algoliaCommon.triggerHooks('beforeAutocompleteOptions', {});
138136

139137
options = {
@@ -142,29 +140,24 @@ define([
142140
placeholder : algoliaConfig.translations.placeholder,
143141
debug : algoliaConfig.autocomplete.isDebugEnabled,
144142
detachedMediaQuery: 'none',
145-
onSubmit(data) {
146-
if (
147-
data.state.query &&
148-
data.state.query !== null &&
149-
data.state.query !== ''
150-
) {
143+
onSubmit: ({ state: { query } }) => {
144+
if (query) {
151145
window.location.href =
152146
algoliaConfig.resultPageUrl +
153-
`?q=${encodeURIComponent(data.state.query)}`;
147+
`?q=${encodeURIComponent(query)}`;
154148
}
155149
},
156150
getSources: ({query}) => {
157-
return this.filterMinChars(query, debounced(autocompleteConfig));
151+
return this.filterMinChars(query, debounced(this.transformSources(searchClient, sources)));
158152
},
159-
shouldPanelOpen({state}) {
153+
shouldPanelOpen: ({state}) => {
160154
return state.query.length >= MIN_SEARCH_LENGTH_CHARS;
161155
},
162156
// Set debug to true, to be able to remove keyboard and be able to scroll in autocomplete menu
163157
debug: algoliaCommon.isMobile(),
164158
plugins
165159
};
166160

167-
168161
// DEPRECATED Do not use - Retained for backward compatibility but `algoliaHookBeforeAutocompleteStart` will be removed in a future version
169162
if (typeof algoliaHookBeforeAutocompleteStart === 'function') {
170163
console.warn(
@@ -196,49 +189,51 @@ define([
196189
* @returns Algolia sources
197190
*/
198191
transformSources(searchClient, sources) {
199-
const autocompleteConfig = [];
200-
sources.forEach((data) => {
201-
if (!data.sourceId) {
202-
console.error(
203-
'Algolia Autocomplete: sourceId is required for custom sources'
204-
);
205-
return;
206-
}
207-
const getItems = ({query}) => {
208-
return autocomplete.getAlgoliaResults({
209-
searchClient,
210-
queries: [
211-
{
212-
query,
213-
indexName: data.indexName,
214-
params : data.options,
215-
},
216-
],
217-
// only set transformResponse if defined (necessary check for custom sources)
218-
...(data.transformResponse && {
219-
transformResponse: data.transformResponse,
220-
}),
221-
});
222-
};
223-
const fallbackTemplates = {
224-
noResults: () => 'No results',
225-
header : () => data.sourceId,
226-
item : ({item}) => {
192+
return sources
193+
.filter(data => {
194+
if (!data.sourceId) {
227195
console.error(
228-
`Algolia Autocomplete: No template defined for source "${data.sourceId}"`
196+
'Algolia Autocomplete: sourceId is required for custom sources'
229197
);
230-
return '[ITEM TEMPLATE MISSING]';
231-
},
232-
};
233-
autocompleteConfig.push({
234-
sourceId : data.sourceId,
235-
getItems,
236-
templates: {...fallbackTemplates, ...(data.templates || {})},
237-
// only set getItemUrl if defined (necessary check for custom sources)
238-
...(data.getItemUrl && {getItemUrl: data.getItemUrl}),
198+
return false;
199+
}
200+
return true;
201+
})
202+
.map((data) => {
203+
const getItems = ({query}) => {
204+
return autocomplete.getAlgoliaResults({
205+
searchClient,
206+
queries: [
207+
{
208+
query,
209+
indexName: data.indexName,
210+
params : data.options,
211+
},
212+
],
213+
// only set transformResponse if defined (necessary check for custom sources)
214+
...(data.transformResponse && {
215+
transformResponse: data.transformResponse,
216+
}),
217+
});
218+
};
219+
const fallbackTemplates = {
220+
noResults: () => 'No results',
221+
header : () => data.sourceId,
222+
item : ({item}) => {
223+
console.error(
224+
`Algolia Autocomplete: No template defined for source "${data.sourceId}"`
225+
);
226+
return '[ITEM TEMPLATE MISSING]';
227+
},
228+
};
229+
return {
230+
sourceId : data.sourceId,
231+
getItems,
232+
templates: {...fallbackTemplates, ...(data.templates || {})},
233+
// only set getItemUrl if defined (necessary check for custom sources)
234+
...(data.getItemUrl && {getItemUrl: data.getItemUrl}),
235+
};
239236
});
240-
});
241-
return autocompleteConfig;
242237
},
243238

244239
/**

0 commit comments

Comments
 (0)