add debounce on autocomplete in advanced search (#18471)

This commit is contained in:
Karan Hotchandani 2024-10-30 20:44:37 +05:30 committed by GitHub
parent ba4a480bf1
commit a47235581f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,7 +12,7 @@
*/ */
import { t } from 'i18next'; import { t } from 'i18next';
import { isEmpty, sortBy } from 'lodash'; import { debounce, isEmpty, sortBy } from 'lodash';
import { import {
AsyncFetchListValues, AsyncFetchListValues,
AsyncFetchListValuesResult, AsyncFetchListValuesResult,
@ -115,8 +115,9 @@ class AdvancedSearchClassBase {
entityField: EntityFields; entityField: EntityFields;
suggestField?: SuggestionField; suggestField?: SuggestionField;
}) => SelectFieldSettings['asyncFetch'] = ({ searchIndex, entityField }) => { }) => SelectFieldSettings['asyncFetch'] = ({ searchIndex, entityField }) => {
return (search) => { // Wrapping the fetch function in a debounce of 300 ms
return getAggregateFieldOptions( const debouncedFetch = debounce((search, callback) => {
getAggregateFieldOptions(
searchIndex, searchIndex,
entityField, entityField,
search ?? '', search ?? '',
@ -125,13 +126,19 @@ class AdvancedSearchClassBase {
const buckets = const buckets =
response.data.aggregations[`sterms#${entityField}`].buckets; response.data.aggregations[`sterms#${entityField}`].buckets;
return { callback({
values: buckets.map((bucket) => ({ values: buckets.map((bucket) => ({
value: bucket.key, value: bucket.key,
title: bucket.label ?? bucket.key, title: bucket.label ?? bucket.key,
})), })),
hasMore: false, hasMore: false,
}; });
});
}, 300);
return (search) => {
return new Promise((resolve) => {
debouncedFetch(search, resolve);
}); });
}; };
}; };