11import { FC , useContext , useMemo , useState } from 'react'
2- import { bind , isEmpty } from 'lodash'
2+ import { bind , debounce , isEmpty } from 'lodash'
33import { toast } from 'react-toastify'
44import classNames from 'classnames'
55
66import { profileContext , ProfileContextData } from '~/libs/core'
77import { Button , IconSolid , InputDatePicker , InputMultiselectOption ,
8- InputRadio , InputSelect , InputSelectOption , InputSelectReact , InputText , InputTextarea } from '~/libs/ui'
8+ InputRadio , InputSelect , InputSelectReact , InputText , InputTextarea } from '~/libs/ui'
99import { InputSkillSelector } from '~/libs/shared'
1010
11- import { ProjectsResponse , useProjects } from '../../services/projects'
11+ import { getProjects } from '../../services/projects'
1212import { ProjectTypes , ProjectTypeValues } from '../../constants'
1313import { saveCopilotRequest } from '../../services/copilot-requests'
1414
@@ -20,20 +20,9 @@ const CopilotRequestForm: FC<{}> = () => {
2020 const [ formValues , setFormValues ] = useState < any > ( { } )
2121 const [ isFormChanged , setIsFormChanged ] = useState ( false )
2222 const [ formErrors , setFormErrors ] = useState < any > ( { } )
23- const [ searchTerm , setSearchTerm ] = useState < string > ( '' )
24- const { data : projectsData } : ProjectsResponse = useProjects ( searchTerm )
2523 const [ existingCopilot , setExistingCopilot ] = useState < string > ( '' )
2624 const [ paymentType , setPaymentType ] = useState < string > ( '' )
2725
28- const projects = useMemo (
29- ( ) => (
30- projectsData
31- ? projectsData . map ( project => ( { label : project . name , value : project . id } ) )
32- : [ ]
33- ) ,
34- [ projectsData ] ,
35- )
36-
3726 const projectTypes = ProjectTypes ? ProjectTypes . map ( project => ( {
3827 label : project ,
3928 value : ProjectTypeValues [ project ] ,
@@ -63,18 +52,12 @@ const CopilotRequestForm: FC<{}> = () => {
6352 setPaymentType ( t )
6453 }
6554
66- function filterProjects ( option : InputSelectOption , value : string ) : boolean {
67- setSearchTerm ( value )
68- return (
69- option . label
70- ?. toString ( )
71- . toLowerCase ( )
72- . includes ( value . toLowerCase ( ) ) ?? false
73- )
74- }
75-
76- function handleProjectSearch ( inputValue : string ) : void {
77- setSearchTerm ( inputValue )
55+ async function handleProjectSearch ( inputValue : string ) : Promise < Array < {
56+ label : string ;
57+ value : string ;
58+ } > > {
59+ const response = await getProjects ( inputValue )
60+ return response . map ( project => ( { label : project . name , value : project . id } ) )
7861 }
7962
8063 function handleProjectSelect ( option : React . ChangeEvent < HTMLInputElement > ) : void {
@@ -268,6 +251,11 @@ const CopilotRequestForm: FC<{}> = () => {
268251 setFormErrors ( updatedFormErrors )
269252 }
270253
254+ const debouncedProjectSearch = useMemo ( ( ) => debounce ( ( inputValue : string , callback : ( options : any [ ] ) => void ) => {
255+ handleProjectSearch ( inputValue )
256+ . then ( callback )
257+ } , 300 ) , [ ] )
258+
271259 return (
272260 < div className = { classNames ( 'd-flex flex-column justify-content-center align-items-center' , styles . container ) } >
273261 < div className = { styles . form } >
@@ -290,15 +278,14 @@ const CopilotRequestForm: FC<{}> = () => {
290278 < p className = { styles . formRow } > Select the project you want the copilot for</ p >
291279 < InputSelectReact
292280 tabIndex = { 0 }
293- options = { projects }
294281 value = { formValues . projectId || '' }
295282 onChange = { handleProjectSelect }
296- onInputChange = { handleProjectSearch }
283+ loadOptions = { debouncedProjectSearch }
284+ async
297285 name = 'project'
298286 label = 'Project'
299287 placeholder = 'Start typing the name of the project'
300288 dirty
301- filterOption = { filterProjects }
302289 error = { formErrors . projectId }
303290 />
304291 < p className = { styles . formRow } >
0 commit comments