Skip to content

Commit b35fe40

Browse files
committed
MAGE-986 Centralize component state
1 parent f61b0ba commit b35fe40

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

view/frontend/web/js/autocomplete.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@ define([
4242
const DEBOUNCE_MS = algoliaConfig.autocomplete.debounceMilliseconds;
4343
const MIN_SEARCH_LENGTH_CHARS = algoliaConfig.autocomplete.minimumCharacters;
4444

45-
// Global state
46-
const state = {
47-
hasRendered: false,
48-
hasSuggestionSection: false
49-
};
50-
5145
return Component.extend({
5246
DEFAULT_HITS_PER_SECTION,
5347
DEBOUNCE_MS,
5448
MIN_SEARCH_LENGTH_CHARS,
5549

56-
hasRedirect: false,
50+
state: {
51+
hasRendered: false,
52+
hasSuggestionSection: false,
53+
hasRedirect: false
54+
},
5755

5856
navigator: {
5957
navigate({itemUrl}) {
@@ -119,7 +117,7 @@ define([
119117
},
120118

121119
handleAutocompleteSubmit({ state: { query } }) {
122-
if (query && !this.hasRedirect) {
120+
if (query && !this.state.hasRedirect) {
123121
this.navigator.navigate({ itemUrl: this.getSearchResultsUrl(query) });
124122
}
125123
},
@@ -425,7 +423,7 @@ define([
425423
source.transformResponse = ({results, hits}) => {
426424
const resDetail = results[0];
427425
const redirectUrl = resDetail?.renderingContent?.redirect?.url;
428-
this.hasRedirect = !!redirectUrl;
426+
this.state.hasRedirect = !!redirectUrl;
429427

430428
return hits.map((res) => {
431429
return res.map((hit, i) => {
@@ -555,7 +553,7 @@ define([
555553
const plugins = [];
556554

557555
if (algoliaConfig.autocomplete.nbOfQueriesSuggestions > 0) {
558-
state.hasSuggestionSection = true;
556+
this.state.hasSuggestionSection = true;
559557
plugins.push(this.buildSuggestionsPlugin(searchClient));
560558
}
561559

@@ -917,9 +915,9 @@ define([
917915
},
918916

919917
handleAutocompleteStateChange(autocompleteState) {
920-
if (!state.hasRendered && autocompleteState.isOpen) {
918+
if (!this.state.hasRendered && autocompleteState.isOpen) {
921919
this.addPanelObserver();
922-
state.hasRendered = true;
920+
this.state.hasRendered = true;
923921
}
924922
},
925923

0 commit comments

Comments
 (0)