Skip to content

Commit

Permalink
Set searching by properties
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Karski <daniel.karski@polidea.com>
  • Loading branch information
Daniel Karski committed Mar 27, 2020
1 parent 9fdb650 commit 3cd8be6
Showing 1 changed file with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function SelectWrapper({

const value = get(modifiedData, name, null);
const [state, setState] = useState({
_q: '',
_contains: '',
_limit: 20,
_start: 0,
});
Expand All @@ -53,15 +53,13 @@ function SelectWrapper({

if (!isDraggingComponent) {
try {
const params = cloneDeep(state);
const requestUrl = `/${pluginId}/explorer/${targetModel}`;

if (isEmpty(params._q)) {
delete params._q;
} else {
delete params._limit;
delete params._start;
}
const containsKey = `${mainField}_contains`;
const { _contains, ...restState } = cloneDeep(state);
const params = isEmpty(state._contains)
? restState
: { [containsKey]: _contains, ...restState };

const data = await request(requestUrl, {
method: 'GET',
Expand All @@ -73,13 +71,6 @@ function SelectWrapper({
return { value: obj, label: obj[mainField] };
});

if (!isEmpty(params._q)) {
setOptions(formattedData);
setState(prev => ({ ...prev, _start: 0 }));

return;
}

setOptions(prevState =>
prevState.concat(formattedData).filter((obj, index) => {
const objIndex = prevState.findIndex(el => el.value.id === obj.value.id);
Expand All @@ -101,7 +92,7 @@ function SelectWrapper({
};

useEffect(() => {
if (state._q !== '') {
if (state._contains !== '') {
let timer = setTimeout(() => {
ref.current();
}, 300);
Expand All @@ -114,7 +105,7 @@ function SelectWrapper({
return () => {
abortController.abort();
};
}, [state._q]);
}, [state._contains]);

useEffect(() => {
if (state._start !== 0) {
Expand All @@ -129,11 +120,11 @@ function SelectWrapper({
const onInputChange = (inputValue, { action }) => {
if (action === 'input-change') {
setState(prevState => {
if (prevState._q === inputValue) {
if (prevState._contains === inputValue) {
return prevState;
}

return { ...prevState, _q: inputValue };
return { ...prevState, _contains: inputValue, _start: 0 };
});
}

Expand Down Expand Up @@ -196,7 +187,7 @@ function SelectWrapper({
}}
onInputChange={onInputChange}
onMenuClose={() => {
setState(prevState => ({ ...prevState, _q: '' }));
setState(prevState => ({ ...prevState, _contains: '' }));
}}
onMenuScrollToBottom={onMenuScrollToBottom}
onRemove={onRemoveRelation}
Expand Down

0 comments on commit 3cd8be6

Please sign in to comment.