1
1
import { FC , useContext , useMemo , useState } from 'react'
2
- import { bind , isEmpty } from 'lodash'
2
+ import { bind , debounce , isEmpty } from 'lodash'
3
3
import { toast } from 'react-toastify'
4
4
import classNames from 'classnames'
5
5
6
6
import { profileContext , ProfileContextData } from '~/libs/core'
7
7
import { Button , IconSolid , InputDatePicker , InputMultiselectOption ,
8
- InputRadio , InputSelect , InputSelectOption , InputSelectReact , InputText , InputTextarea } from '~/libs/ui'
8
+ InputRadio , InputSelect , InputSelectReact , InputText , InputTextarea } from '~/libs/ui'
9
9
import { InputSkillSelector } from '~/libs/shared'
10
10
11
- import { ProjectsResponse , useProjects } from '../../services/projects'
11
+ import { getProjects } from '../../services/projects'
12
12
import { ProjectTypes , ProjectTypeValues } from '../../constants'
13
13
import { saveCopilotRequest } from '../../services/copilot-requests'
14
14
@@ -20,20 +20,9 @@ const CopilotRequestForm: FC<{}> = () => {
20
20
const [ formValues , setFormValues ] = useState < any > ( { } )
21
21
const [ isFormChanged , setIsFormChanged ] = useState ( false )
22
22
const [ formErrors , setFormErrors ] = useState < any > ( { } )
23
- const [ searchTerm , setSearchTerm ] = useState < string > ( '' )
24
- const { data : projectsData } : ProjectsResponse = useProjects ( searchTerm )
25
23
const [ existingCopilot , setExistingCopilot ] = useState < string > ( '' )
26
24
const [ paymentType , setPaymentType ] = useState < string > ( '' )
27
25
28
- const projects = useMemo (
29
- ( ) => (
30
- projectsData
31
- ? projectsData . map ( project => ( { label : project . name , value : project . id } ) )
32
- : [ ]
33
- ) ,
34
- [ projectsData ] ,
35
- )
36
-
37
26
const projectTypes = ProjectTypes ? ProjectTypes . map ( project => ( {
38
27
label : project ,
39
28
value : ProjectTypeValues [ project ] ,
@@ -63,18 +52,12 @@ const CopilotRequestForm: FC<{}> = () => {
63
52
setPaymentType ( t )
64
53
}
65
54
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 } ) )
78
61
}
79
62
80
63
function handleProjectSelect ( option : React . ChangeEvent < HTMLInputElement > ) : void {
@@ -268,6 +251,11 @@ const CopilotRequestForm: FC<{}> = () => {
268
251
setFormErrors ( updatedFormErrors )
269
252
}
270
253
254
+ const debouncedProjectSearch = useMemo ( ( ) => debounce ( ( inputValue : string , callback : ( options : any [ ] ) => void ) => {
255
+ handleProjectSearch ( inputValue )
256
+ . then ( callback )
257
+ } , 300 ) , [ ] )
258
+
271
259
return (
272
260
< div className = { classNames ( 'd-flex flex-column justify-content-center align-items-center' , styles . container ) } >
273
261
< div className = { styles . form } >
@@ -290,15 +278,14 @@ const CopilotRequestForm: FC<{}> = () => {
290
278
< p className = { styles . formRow } > Select the project you want the copilot for</ p >
291
279
< InputSelectReact
292
280
tabIndex = { 0 }
293
- options = { projects }
294
281
value = { formValues . projectId || '' }
295
282
onChange = { handleProjectSelect }
296
- onInputChange = { handleProjectSearch }
283
+ loadOptions = { debouncedProjectSearch }
284
+ async
297
285
name = 'project'
298
286
label = 'Project'
299
287
placeholder = 'Start typing the name of the project'
300
288
dirty
301
- filterOption = { filterProjects }
302
289
error = { formErrors . projectId }
303
290
/>
304
291
< p className = { styles . formRow } >
0 commit comments