Last active
July 10, 2023 15:59
-
-
Save jrdn91/26482e77a8c972eacde72a0e78108dac to your computer and use it in GitHub Desktop.
Search results useQuery example
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
const [search, setSearch] = useState("") | |
const debouncedSearch = useDebounce(search, 400) | |
const [searchResults, setSearchResults] = useState<Agent[]>([]) | |
const { data: agents, isLoading } = useListAgents({ | |
search: debouncedSearch, | |
}) | |
useEffect(() => { | |
setSearchResults(() => agents || []) | |
}, [agents, search]) | |
return ( | |
<AllAgentsTable | |
editAgent={editAgent} | |
searchResults={searchResults} | |
/> | |
) |
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
const [search, setSearch] = useState("") | |
const debouncedSearch = useDebounce(search, 400) | |
const { data: agents, isLoading } = useListAgents({ | |
search: debouncedSearch, | |
}) | |
return ( | |
<AllAgentsTable | |
editAgent={editAgent} | |
agents={agents} | |
/> | |
) |
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
import queryKeys from "@/api/queryKeys" | |
import { useQuery } from "@tanstack/react-query" | |
import AgentsService from "@/api/AgentsService" | |
import querifyFilters from "@/utils/querifyFilters" | |
import { ListAgentsResponse } from "shared/types/Agent" | |
const listAgents = (data: { search: string }) => | |
AgentsService.get<ListAgentsResponse>(`/${querifyFilters(data)}`).then( | |
(res) => res?.data?.data?.agents | |
) | |
function useListAgents(data: { search: string }) { | |
return useQuery({ | |
queryKey: queryKeys.agent.list(data).queryKey, | |
queryFn: () => listAgents(data), | |
}) | |
} | |
export default useListAgents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment