Skip to content

Commit 34e64f2

Browse files
committed
debounced member search
1 parent abfa012 commit 34e64f2

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

wondrous-bot-admin/src/pages/quests/members/MemberSearchBar.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
import { useLazyQuery } from "@apollo/client";
22
import { useMediaQuery } from "@mui/material";
33
import AutocompleteOptionsComponent from "components/AddFormEntity/components/AutocompleteComponent";
4-
import { GET_COMMUNITY_USERS_FOR_ORG, SEARCH_COMMUNITY_USERS_FOR_ORG } from "graphql/queries";
5-
import { useEffect, useState } from "react";
4+
import { SEARCH_COMMUNITY_USERS_FOR_ORG } from "graphql/queries";
5+
import { useCallback, useEffect, useState } from "react";
66
import { useGlobalContext } from "utils/hooks";
7+
import { debounce } from "lodash";
78

89
export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => {
910
const [searchCmtyUsersForOrg, { data, loading, error }] = useLazyQuery(SEARCH_COMMUNITY_USERS_FOR_ORG, {
1011
fetchPolicy: "cache-and-network",
1112
});
12-
const [searchString, setSearchString] = useState("");
1313
const { activeOrg } = useGlobalContext();
14-
useEffect(() => {
15-
if (activeOrg?.id && searchString) {
16-
searchCmtyUsersForOrg({
17-
variables: {
18-
input: {
19-
orgId: activeOrg?.id,
20-
searchString,
21-
},
22-
},
23-
});
24-
}
25-
}, [activeOrg?.id, searchString]);
2614
const options = data?.searchCmtyUsersForOrg?.map((option) => {
2715
const username = option?.username || option?.discordUsername || option?.telegramUsername || "N/A";
2816
return {
@@ -37,6 +25,20 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => {
3725
}
3826
}, [member, data?.searchCmtyUsersForOrg]);
3927

28+
const search = useCallback(debounce(searchCmtyUsersForOrg, 1000), []);
29+
30+
const handleOnChange = (e) => {
31+
const value = e.target.value;
32+
search({
33+
variables: {
34+
input: {
35+
orgId: activeOrg?.id,
36+
searchString: value,
37+
},
38+
},
39+
});
40+
};
41+
4042
const isMobile = useMediaQuery((theme: any) => theme.breakpoints.down("md"));
4143

4244
return (
@@ -45,15 +47,11 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => {
4547
bgColor="white"
4648
fullWidth={isMobile}
4749
value={member}
48-
onChange={(value) => {
49-
onChange(value);
50-
}}
50+
onChange={onChange}
5151
disableClearable={false}
5252
onClear={() => setMemberInfo(null)}
5353
inputProps={{
54-
onChange: (e) => {
55-
setSearchString(e.target.value);
56-
},
54+
onChange: handleOnChange,
5755
}}
5856
/>
5957
);

0 commit comments

Comments
 (0)