-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIF-562: add
useDebouncedQuery
hook to fix endless request for `Dyn…
…amicSelection` component
- Loading branch information
1 parent
30b44ad
commit ebb87b3
Showing
5 changed files
with
86 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useDebouncedQuery } from './useDebouncedQuery'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import debounce from 'lodash/debounce'; | ||
import { | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { useQuery } from 'react-query'; | ||
|
||
import { | ||
useNamespace, | ||
useOkapiKy, | ||
} from '@folio/stripes/core'; | ||
|
||
const LIST_ITEMS_LIMIT = 100; | ||
const DEBOUNCE_DELAY = 500; | ||
|
||
export const useDebouncedQuery = ({ | ||
api, | ||
queryBuilder, | ||
dataFormatter, | ||
debounceDelay = DEBOUNCE_DELAY, | ||
limit = LIST_ITEMS_LIMIT, | ||
}) => { | ||
const [inputValue, setInputValue] = useState(''); | ||
const [options, setOptions] = useState([]); | ||
const ky = useOkapiKy(); | ||
const [namespace] = useNamespace({ key: 'locations' }); | ||
|
||
const debouncedSetInputValue = useMemo(() => { | ||
return debounce((value) => setInputValue(value), debounceDelay); | ||
}, [debounceDelay]); | ||
|
||
const { isLoading } = useQuery({ | ||
queryKey: [namespace, inputValue], | ||
queryFn: async ({ signal }) => { | ||
if (!inputValue) return []; | ||
|
||
const searchParams = { | ||
query: queryBuilder(inputValue), | ||
limit, | ||
}; | ||
|
||
const res = await ky.get(api, { searchParams, signal }).json(); | ||
|
||
return dataFormatter(res); | ||
}, | ||
enabled: Boolean(inputValue), | ||
onSuccess: (data) => { | ||
setOptions(data); | ||
}, | ||
onError: () => { | ||
setOptions([]); | ||
}, | ||
}); | ||
|
||
return { | ||
options, | ||
isLoading, | ||
inputValue, | ||
setInputValue: debouncedSetInputValue, | ||
}; | ||
}; |