From 34e64f2c64edd522cd59b0eeaa25d1d2d6647f57 Mon Sep 17 00:00:00 2001 From: adrian mustea Date: Mon, 1 Apr 2024 23:21:01 +0300 Subject: [PATCH] debounced member search --- .../pages/quests/members/MemberSearchBar.tsx | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/wondrous-bot-admin/src/pages/quests/members/MemberSearchBar.tsx b/wondrous-bot-admin/src/pages/quests/members/MemberSearchBar.tsx index 438891049..1583117e2 100644 --- a/wondrous-bot-admin/src/pages/quests/members/MemberSearchBar.tsx +++ b/wondrous-bot-admin/src/pages/quests/members/MemberSearchBar.tsx @@ -1,28 +1,16 @@ import { useLazyQuery } from "@apollo/client"; import { useMediaQuery } from "@mui/material"; import AutocompleteOptionsComponent from "components/AddFormEntity/components/AutocompleteComponent"; -import { GET_COMMUNITY_USERS_FOR_ORG, SEARCH_COMMUNITY_USERS_FOR_ORG } from "graphql/queries"; -import { useEffect, useState } from "react"; +import { SEARCH_COMMUNITY_USERS_FOR_ORG } from "graphql/queries"; +import { useCallback, useEffect, useState } from "react"; import { useGlobalContext } from "utils/hooks"; +import { debounce } from "lodash"; export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => { const [searchCmtyUsersForOrg, { data, loading, error }] = useLazyQuery(SEARCH_COMMUNITY_USERS_FOR_ORG, { fetchPolicy: "cache-and-network", }); - const [searchString, setSearchString] = useState(""); const { activeOrg } = useGlobalContext(); - useEffect(() => { - if (activeOrg?.id && searchString) { - searchCmtyUsersForOrg({ - variables: { - input: { - orgId: activeOrg?.id, - searchString, - }, - }, - }); - } - }, [activeOrg?.id, searchString]); const options = data?.searchCmtyUsersForOrg?.map((option) => { const username = option?.username || option?.discordUsername || option?.telegramUsername || "N/A"; return { @@ -37,6 +25,20 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => { } }, [member, data?.searchCmtyUsersForOrg]); + const search = useCallback(debounce(searchCmtyUsersForOrg, 1000), []); + + const handleOnChange = (e) => { + const value = e.target.value; + search({ + variables: { + input: { + orgId: activeOrg?.id, + searchString: value, + }, + }, + }); + }; + const isMobile = useMediaQuery((theme: any) => theme.breakpoints.down("md")); return ( @@ -45,15 +47,11 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => { bgColor="white" fullWidth={isMobile} value={member} - onChange={(value) => { - onChange(value); - }} + onChange={onChange} disableClearable={false} onClear={() => setMemberInfo(null)} inputProps={{ - onChange: (e) => { - setSearchString(e.target.value); - }, + onChange: handleOnChange, }} /> );