-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2529 from NDLANO/feat/delayed-query
feat: introduce a delayed query helper and use it in combobox queries.
- Loading branch information
Showing
6 changed files
with
52 additions
and
28 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
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,34 @@ | ||
/** | ||
* Copyright (c) 2024-present, NDLA. | ||
* | ||
* This source code is licensed under the GPLv3 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import { useEffect, useState } from "react"; | ||
import useDebounce from "./useDebounce"; | ||
|
||
interface UseDelayedQuery { | ||
defaultQuery?: string; | ||
defaultPage?: number; | ||
debounceTime?: number; | ||
} | ||
|
||
export const usePaginatedQuery = ({ defaultQuery = "", defaultPage = 1, debounceTime = 300 }: UseDelayedQuery = {}) => { | ||
const [query, setQuery] = useState(defaultQuery); | ||
const [page, setPage] = useState(defaultPage); | ||
const delayedQuery = useDebounce(query, debounceTime); | ||
|
||
useEffect(() => { | ||
setPage(1); | ||
}, [delayedQuery]); | ||
|
||
return { | ||
page, | ||
setPage, | ||
query, | ||
delayedQuery, | ||
setQuery, | ||
}; | ||
}; |