Last active
June 19, 2020 16:58
-
-
Save GenesisSam/19d095efb87306ded3eb5579acafc420 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import * as React from "react"; | |
| import { useDebounce } from "react-use"; | |
| import { useActions, useStoreState } from "app/store"; | |
| import { searchedUsersSelector, searchedUsersWithoutCurrentUserSelector } from "app/selectors/search"; | |
| import useCancelToken from "common/hooks/useCancelToken"; | |
| import { | |
| ActionCreators, | |
| getSearchUsers as getSearchUsersAction, | |
| } from "app/actions/user"; | |
| const useUserSearch = ({limitResult, withoutCurrentUser}:{limitResult?: number; withoutCurrentUser?: boolean}) => { | |
| const cancelToken = useCancelToken(); | |
| const { searchedUsers, isLoading } = useStoreState(storeState => ({ | |
| searchedUsers: withoutCurrentUser ? searchedUsersWithoutCurrentUserSelector(storeState) : searchedUsersSelector(storeState), | |
| isLoading: storeState.search.searchUsersLoading, | |
| })); | |
| const { getSearchUsers, clearSearchedUsers } = useActions({ | |
| getSearchUsers: getSearchUsersAction, | |
| clearSearchedUsers: ActionCreators.clearSearchedUsers, | |
| }); | |
| const debouncedGetUser = React.useCallback( | |
| (query: string) => { | |
| cancelToken.cancelAndRenewal(); | |
| clearSearchedUsers(); | |
| getSearchUsers( | |
| { query: query.trim(), limit: limitResult }, | |
| cancelToken.current.token, | |
| ); | |
| }, | |
| [cancelToken, clearSearchedUsers, getSearchUsers, limitResult], | |
| ); | |
| useDebounce(debouncedGetUser, 500); | |
| return [debouncedGetUser, searchedUsers, isLoading]; | |
| }; | |
| export default useUserSearch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment