1
1
import { useLazyQuery } from "@apollo/client" ;
2
2
import { useMediaQuery } from "@mui/material" ;
3
3
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" ;
6
6
import { useGlobalContext } from "utils/hooks" ;
7
+ import { debounce } from "lodash" ;
7
8
8
9
export const MemberPageSearchBar = ( { onChange, member, setMemberInfo } ) => {
9
10
const [ searchCmtyUsersForOrg , { data, loading, error } ] = useLazyQuery ( SEARCH_COMMUNITY_USERS_FOR_ORG , {
10
11
fetchPolicy : "cache-and-network" ,
11
12
} ) ;
12
- const [ searchString , setSearchString ] = useState ( "" ) ;
13
13
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 ] ) ;
26
14
const options = data ?. searchCmtyUsersForOrg ?. map ( ( option ) => {
27
15
const username = option ?. username || option ?. discordUsername || option ?. telegramUsername || "N/A" ;
28
16
return {
@@ -37,6 +25,20 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => {
37
25
}
38
26
} , [ member , data ?. searchCmtyUsersForOrg ] ) ;
39
27
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
+
40
42
const isMobile = useMediaQuery ( ( theme : any ) => theme . breakpoints . down ( "md" ) ) ;
41
43
42
44
return (
@@ -45,15 +47,11 @@ export const MemberPageSearchBar = ({ onChange, member, setMemberInfo }) => {
45
47
bgColor = "white"
46
48
fullWidth = { isMobile }
47
49
value = { member }
48
- onChange = { ( value ) => {
49
- onChange ( value ) ;
50
- } }
50
+ onChange = { onChange }
51
51
disableClearable = { false }
52
52
onClear = { ( ) => setMemberInfo ( null ) }
53
53
inputProps = { {
54
- onChange : ( e ) => {
55
- setSearchString ( e . target . value ) ;
56
- } ,
54
+ onChange : handleOnChange ,
57
55
} }
58
56
/>
59
57
) ;
0 commit comments