-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSearchAutocompleteController.js
More file actions
173 lines (148 loc) · 5.98 KB
/
Copy pathSearchAutocompleteController.js
File metadata and controls
173 lines (148 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Controller } from '@hotwired/stimulus';
/**
* Debounced, cached, abortable autocomplete search preview. One instance is connected per
* ".searchFormContainer" (desktop and mobile header variants each get their own).
*/
export default class extends Controller {
static targets = ['input', 'results', 'loading', 'resultsPanel'];
static values = { previewUrl: String };
connect() {
this.abortController = null;
this.debounceTimer = null;
this.queryCache = new Map();
this.lastNonEmptyContent = null;
}
disconnect() {
if (this.abortController) {
this.abortController.abort();
}
if (this.debounceTimer) {
clearTimeout(this.debounceTimer);
}
}
onInput(event) {
const queryText = event.target.value;
if (this.debounceTimer) {
clearTimeout(this.debounceTimer);
this.debounceTimer = null;
}
if (queryText.length >= 3) {
// Keep previous results visible while waiting for debounce
this.resultsPanelTarget.classList.add('show');
this.debounceTimer = setTimeout(() => this.performSearch(), 200);
return;
}
// Also cancel any ongoing request
if (this.abortController) {
this.abortController.abort();
this.abortController = null;
}
// New search session: reset last results so stale content won't reappear
this.lastNonEmptyContent = null;
this.resultsTarget.textContent = '';
this.resultsPanelTarget.classList.remove('show');
}
onFocus(event) {
const queryText = event.target.value;
if (queryText.length < 3) {
return;
}
if (this.resultsTarget.innerHTML.trim() !== '') {
this.resultsPanelTarget.classList.add('show');
} else {
// Search silently: panel will only appear when results arrive via displayResults
this.performSearch({ showWhileLoading: false });
}
}
outsideClick(event) {
if (
this.resultsPanelTarget.classList.contains('show')
&& !this.resultsPanelTarget.contains(event.target)
&& !this.inputTarget.contains(event.target)
) {
this.resultsPanelTarget.classList.remove('show');
}
}
displayResults(content) {
this.loadingTarget.classList.add('d-none');
this.resultsTarget.classList.remove('d-none');
// If response is empty but we have a previous non-empty result, keep showing it
const displayContent = content.htmlResults ? content : this.lastNonEmptyContent;
if (!displayContent || !displayContent.htmlResults) {
this.resultsPanelTarget.classList.remove('show');
return;
}
this.resultsPanelTarget.classList.add('show');
this.resultsTarget.innerHTML = displayContent.htmlResults;
if (this.resultsTarget.querySelector('.products')) {
this.resultsPanelTarget.parentElement.classList.add('start-0');
this.resultsPanelTarget.parentElement.style.width = '100%';
} else {
this.resultsPanelTarget.parentElement.classList.remove('start-0');
this.resultsPanelTarget.parentElement.style.width = 'auto';
}
}
performSearch({ showWhileLoading = true } = {}) {
const form = this.element.querySelector('form');
const formData = new FormData(form);
const plainFormData = Object.fromEntries(formData.entries());
const formDataString = new URLSearchParams(plainFormData).toString();
// Serve from cache if available
if (this.queryCache.has(formDataString)) {
const cached = this.queryCache.get(formDataString);
this.resultsPanelTarget.classList.add('show');
this.displayResults(cached);
if (cached.htmlResults) {
this.lastNonEmptyContent = cached;
}
return;
}
// While loading, show panel only if requested (not on focus)
if (showWhileLoading) {
if (this.lastNonEmptyContent) {
// Keep showing previous results (no spinner)
this.loadingTarget.classList.add('d-none');
this.resultsTarget.classList.remove('d-none');
} else {
// First search: show spinner
this.loadingTarget.classList.remove('d-none');
this.resultsTarget.classList.add('d-none');
}
this.resultsPanelTarget.classList.add('show');
}
if (this.abortController) {
this.abortController.abort();
}
this.abortController = new AbortController();
fetch(this.previewUrlValue, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formDataString,
signal: this.abortController.signal,
})
.then((response) => response.json())
.then((content) => {
// Track last non-empty result
if (content.htmlResults) {
this.lastNonEmptyContent = content;
}
// Cache the result: if empty, store last non-empty result instead
const cachedContent = content.htmlResults ? content : this.lastNonEmptyContent;
if (cachedContent) {
this.queryCache.set(formDataString, cachedContent);
this.displayResults(cachedContent);
} else {
// No result ever received yet, just hide the panel
this.loadingTarget.classList.add('d-none');
this.resultsPanelTarget.classList.remove('show');
}
})
.catch((error) => {
if (error.name !== 'AbortError') {
console.error(error);
}
});
}
}