Skip to content

Commit 1a97f3d

Browse files
authored
Merge pull request #279 from cmu-delphi/development
Debounce filters
2 parents ba87d28 + f65dd0f commit 1a97f3d

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/assets/js/indicatorSetsFilters.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ if (document.readyState === 'loading') {
177177
initializeGroupSublistVisibility();
178178
}
179179

180+
// Debounce function to delay execution
181+
function debounce(func, wait) {
182+
let timeout;
183+
return function() {
184+
const context = this;
185+
const args = arguments;
186+
clearTimeout(timeout);
187+
timeout = setTimeout(() => {
188+
func.apply(context, args);
189+
}, wait);
190+
};
191+
}
192+
193+
// Function to handle form submission with UI feedback
194+
function submitFilterForm(form) {
195+
persistCheckedIndicators();
196+
showLoader();
197+
form.submit();
198+
}
199+
200+
// Create debounced submission function
201+
const debouncedSubmit = debounce(function(form) {
202+
submitFilterForm(form);
203+
}, 2000);
204+
180205
// Handle group checkbox clicks (e.g., "U.S. States")
181206
document.addEventListener('change', function(event) {
182207
if (event.target.classList.contains('original-data-provider-group-checkbox')) {
@@ -205,11 +230,9 @@ document.addEventListener('change', function(event) {
205230
}
206231
}
207232

208-
// Trigger form submission once after all checkboxes are updated
233+
// Trigger debounced form submission
209234
if (event.target.form) {
210-
persistCheckedIndicators();
211-
showLoader();
212-
event.target.form.submit();
235+
debouncedSubmit(event.target.form);
213236
}
214237
}
215238
});
@@ -264,18 +287,25 @@ function showLoader() {
264287
}
265288

266289
$("#filterIndicatorSetsForm").find("input[type='checkbox']").on("change", function (e) {
267-
// Show loader and fade table
268-
persistCheckedIndicators();
269-
showLoader();
270-
this.form.submit();
290+
// Skip if it's a group checkbox (handled by separate event listener)
291+
if (this.classList.contains('original-data-provider-group-checkbox')) {
292+
return;
293+
}
294+
295+
// Trigger debounced submission
296+
debouncedSubmit(this.form);
271297
});
272298

273299
$("#location_search").on({
274300
"change": function (e) {
275-
// Show loader and fade table
276-
persistCheckedIndicators();
277-
showLoader();
278-
this.form.submit();
301+
// Trigger debounced submission
302+
debouncedSubmit(this.form);
303+
},
304+
// Also debounce on keyup for better UX if needed, or just keep change
305+
"keyup": function(e) {
306+
if (e.key === 'Enter') {
307+
debouncedSubmit(this.form);
308+
}
279309
}
280310
});
281311

0 commit comments

Comments
 (0)