Skip to content

Commit 650fdc3

Browse files
committed
fix autocomplete display value usage
1 parent 83dd1dc commit 650fdc3

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

web/locales/en/plugin__netobserv-plugin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@
375375
"Equals": "Equals",
376376
"Learn more": "Learn more",
377377
"Filter already exists": "Filter already exists",
378-
"More than operator `>=` is not allowed with `{{searchValue}}`. Use equals or contains operators instead.": "More than operator `>=` is not allowed with `{{searchValue}}`. Use equals or contains operators instead.",
378+
"More than operator is not allowed with `{{searchValue}}`. Use equals or contains operators instead.": "More than operator is not allowed with `{{searchValue}}`. Use equals or contains operators instead.",
379+
"Contains operator is not allowed with `{{searchValue}}`. Use equals or more than operators instead.": "Contains operator is not allowed with `{{searchValue}}`. Use equals or more than operators instead.",
379380
"Can't find filter `{{searchValue}}`": "Can't find filter `{{searchValue}}`",
380381
"Invalid format. The input should be <filter><comparator><value> such as `name=netobserv`.": "Invalid format. The input should be <filter><comparator><value> such as `name=netobserv`.",
381382
"Filter": "Filter",

web/src/components/toolbar/filters/autocomplete-filter.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export const AutocompleteFilter: React.FC<AutocompleteFilterProps> = ({
8484
filterDefinition
8585
.getOptions(newValue)
8686
.then(v => {
87-
console.log('onAutoCompleteChange', v);
8887
setOptions(v);
8988
})
9089
.catch(err => {

web/src/components/toolbar/filters/filter-search-input.tsx

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getHTTPErrorDetails } from '../../../utils/errors';
2222
import { matcher } from '../../../utils/filter-definitions';
2323
import { Indicator, setTargeteableFilters } from '../../../utils/filters-helper';
2424
import { useOutsideClickEvent } from '../../../utils/outside-hook';
25+
import { usePrevious } from '../../../utils/previous-hook';
2526
import { Direction } from '../filters-toolbar';
2627
import AutocompleteFilter from './autocomplete-filter';
2728
import CompareFilter, { FilterCompare, getCompareText } from './compare-filter';
@@ -40,7 +41,7 @@ interface FormUpdateResult {
4041
interface Suggestion {
4142
display?: string;
4243
value: string;
43-
validate: boolean;
44+
validate?: boolean;
4445
}
4546

4647
export interface FilterSearchInputProps {
@@ -101,6 +102,7 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
101102
}, 100);
102103
});
103104
const [suggestions, setSuggestions] = React.useState<Suggestion[]>([]);
105+
const prevSuggestions = usePrevious(suggestions);
104106
const [isPopperOpen, setPopperOpen] = React.useState(false);
105107
const [submitPending, setSubmitPending] = React.useState(false);
106108

@@ -141,6 +143,23 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
141143
[filter, filters, filterDefinitions, compare, setFilters, setMessage]
142144
);
143145

146+
const addFilterFromSuggestions = React.useCallback(
147+
(sug: Suggestion[] | undefined = prevSuggestions) => {
148+
if (!value.length) {
149+
addFilter({ display: t('n/a'), v: `""` });
150+
}
151+
// check if a previous suggestion match value, else just add it as filter
152+
const found = filter.component === 'autocomplete' && sug?.find(s => s.value === value || s.display === value);
153+
if (found) {
154+
addFilter({ display: found.display, v: found.value });
155+
} else {
156+
addFilter({ v: value });
157+
}
158+
},
159+
// eslint-disable-next-line react-hooks/exhaustive-deps
160+
[addFilter, filter.component, prevSuggestions, value]
161+
);
162+
144163
const updateForm = React.useCallback(
145164
(v: string = searchInputValue, submitOnRefresh?: boolean) => {
146165
// parse search input value to form content
@@ -157,7 +176,7 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
157176
if (def.component != 'number') {
158177
setMessage(
159178
t(
160-
'More than operator `>=` is not allowed with `{{searchValue}}`. Use equals or contains operators instead.',
179+
'More than operator is not allowed with `{{searchValue}}`. Use equals or contains operators instead.',
161180
{ searchValue }
162181
)
163182
);
@@ -172,12 +191,23 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
172191
} else if (v.includes(FilterCompare.equal)) {
173192
setCompare(FilterCompare.equal);
174193
result.comparator = FilterCompare.equal;
175-
} else if (v.includes(FilterCompare.notMatch)) {
176-
setCompare(FilterCompare.notMatch);
177-
result.comparator = FilterCompare.notMatch;
178194
} else {
179-
setCompare(FilterCompare.match);
180-
result.comparator = FilterCompare.match;
195+
if (def.component === 'number') {
196+
setMessage(
197+
t(
198+
'Contains operator is not allowed with `{{searchValue}}`. Use equals or more than operators instead.',
199+
{ searchValue }
200+
)
201+
);
202+
setIndicator(ValidatedOptions.error);
203+
return { ...result, hasError: true };
204+
} else if (v.includes(FilterCompare.notMatch)) {
205+
setCompare(FilterCompare.notMatch);
206+
result.comparator = FilterCompare.notMatch;
207+
} else {
208+
setCompare(FilterCompare.match);
209+
result.comparator = FilterCompare.match;
210+
}
181211
}
182212
// set direction
183213
if (def.category === 'source') {
@@ -498,8 +528,19 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
498528
type="submit"
499529
onClick={e => {
500530
e.preventDefault();
501-
console.log('Add filter', e);
502-
addFilter({ v: value });
531+
if (filter.component === 'autocomplete' && _.isEmpty(prevSuggestions)) {
532+
filter
533+
.getOptions(value)
534+
.then(opts => {
535+
addFilterFromSuggestions(opts.map(opts => ({ display: opts.name, value: opts.value })));
536+
})
537+
.catch(err => {
538+
const errorMessage = getHTTPErrorDetails(err);
539+
setMessage(errorMessage);
540+
});
541+
} else {
542+
addFilterFromSuggestions();
543+
}
503544
}}
504545
>
505546
{t('Add filter')}
@@ -538,7 +579,7 @@ export const FilterSearchInput: React.FC<FilterSearchInputProps> = ({
538579
React.useEffect(() => {
539580
if (submitPending) {
540581
setSubmitPending(false);
541-
addFilter(value.length ? { v: value } : { display: t('n/a'), v: `""` });
582+
addFilterFromSuggestions();
542583
}
543584
// eslint-disable-next-line react-hooks/exhaustive-deps
544585
}, [submitPending, setSubmitPending, addFilter, value]);

0 commit comments

Comments
 (0)