1
- import React , { useState , useEffect } from 'react' ;
1
+ import React , { useState , useRef } from 'react' ;
2
2
import { message , Form , Row , Col , AutoComplete , Spin , Select , Input } from 'antd' ;
3
3
import { authHeader , handleError } from '../../common/AuthHeader.js' ;
4
4
import { useSelector } from 'react-redux' ;
@@ -11,71 +11,24 @@ const BasicTab = ({
11
11
setSelectedOrbitBuild,
12
12
monitoringDetails,
13
13
setMonitoringDetails,
14
+ businessUnits,
15
+ products,
16
+ domainLoading,
17
+ domainStatus,
18
+ productLoading,
19
+ productStatus,
14
20
} ) => {
15
- const [ products , setProducts ] = useState ( [ ] ) ;
16
- const [ businessUnits , setBusinessUnits ] = useState ( [ ] ) ;
21
+ const [ orbitBuildSuggestions , setOrbitBuildSuggestions ] = useState ( [ ] ) ;
22
+ const [ displayBuildInfo , setDisplayBuildInfo ] = useState ( ) ;
23
+ const [ loading , setLoading ] = useState ( false ) ;
24
+ const [ status , setStatus ] = useState ( null ) ;
25
+
26
+ const searchRef = useRef ( null ) ;
17
27
18
28
const {
19
29
application : { applicationId } ,
20
30
} = useSelector ( ( state ) => state . applicationReducer ) ;
21
31
22
- useEffect ( ( ) => {
23
- getProducts ( ) ;
24
- getDomains ( ) ;
25
- } , [ applicationId ] ) ;
26
-
27
- const getProducts = async ( ) => {
28
- try {
29
- const options = {
30
- method : 'GET' ,
31
- headers : authHeader ( ) ,
32
- } ;
33
- const response = await fetch ( `/api/orbit/getProducts/${ applicationId } ` , options ) ;
34
- if ( ! response . ok ) handleError ( response ) ;
35
-
36
- const productOptions = await response . json ( ) ;
37
-
38
- let finalProductOptions = [ ] ;
39
-
40
- productOptions . map ( ( product ) => {
41
- finalProductOptions . push ( { label : product . product_name , value : product . product_name } ) ;
42
- } ) ;
43
-
44
- setProducts ( finalProductOptions ) ;
45
- } catch ( error ) {
46
- console . log ( error ) ;
47
- message . error ( 'There was an error getting Products from Fido' ) ;
48
- }
49
- } ;
50
-
51
- const getDomains = async ( ) => {
52
- try {
53
- const options = {
54
- method : 'GET' ,
55
- headers : authHeader ( ) ,
56
- } ;
57
- const response = await fetch ( `/api/orbit/getDomains/${ applicationId } ` , options ) ;
58
- if ( ! response . ok ) handleError ( response ) ;
59
-
60
- const domainOptions = await response . json ( ) ;
61
-
62
- let finalDomainOptions = [ ] ;
63
-
64
- domainOptions . map ( ( domain ) => {
65
- finalDomainOptions . push ( { label : domain . business_unit , value : domain . business_unit } ) ;
66
- } ) ;
67
-
68
- setBusinessUnits ( finalDomainOptions ) ;
69
- } catch ( error ) {
70
- console . log ( error ) ;
71
- message . error ( 'There was an error getting Domains from Fido' ) ;
72
- }
73
- } ;
74
-
75
- const [ orbitBuildSuggestions , setOrbitBuildSuggestions ] = useState ( [ ] ) ;
76
- const [ displayBuildInfo , setDisplayBuildInfo ] = useState ( ) ;
77
- const [ loading , setLoading ] = useState ( false ) ;
78
-
79
32
const handleOrbitBuildSelect = async ( selectedOrbitBuild ) => {
80
33
setSelectedOrbitBuild ( selectedOrbitBuild ) ;
81
34
try {
@@ -107,9 +60,11 @@ const BasicTab = ({
107
60
//loader to get suggestions
108
61
const loadOrbitBuildSuggestions = async ( searchText ) => {
109
62
setLoading ( true ) ;
63
+ setStatus ( 'warning' ) ;
110
64
setSelectedOrbitBuild ( searchText ) ;
111
65
if ( searchText . length <= 3 ) {
112
66
setLoading ( false ) ;
67
+ setStatus ( null ) ;
113
68
return ;
114
69
}
115
70
if ( ! searchText . match ( / ^ [ a - z A - Z 0 - 9 : _ - ] * $ / ) ) {
@@ -131,11 +86,17 @@ const BasicTab = ({
131
86
finalSuggestions . push ( { value : build . Name , label : build . Name } ) ;
132
87
} ) ;
133
88
134
- setOrbitBuildSuggestions ( finalSuggestions ) ;
135
- setLoading ( false ) ;
89
+ //if the searchText being returned isn't the same as the current value, then don't update the suggestions
90
+ if ( searchText === searchRef . current ) {
91
+ setOrbitBuildSuggestions ( finalSuggestions ) ;
92
+ setLoading ( false ) ;
93
+ setStatus ( null ) ;
94
+ }
136
95
} catch ( error ) {
137
96
console . log ( error ) ;
138
97
message . error ( 'There was an error getting Builds from Orbit' ) ;
98
+ setLoading ( false ) ;
99
+ setStatus ( 'error' ) ;
139
100
}
140
101
} ;
141
102
@@ -147,9 +108,11 @@ const BasicTab = ({
147
108
name = "businessUnit"
148
109
rules = { [ { required : true , message : 'Required field' } ] } >
149
110
< Select
150
- placeholder = " Select one"
111
+ placeholder = { domainLoading ? 'Fetching Options..' : ' Select one' }
151
112
mode = "single"
152
113
options = { businessUnits }
114
+ loading = { domainLoading }
115
+ status = { domainStatus }
153
116
onChange = { ( value ) => {
154
117
setMonitoringDetails ( {
155
118
...monitoringDetails ,
@@ -163,9 +126,11 @@ const BasicTab = ({
163
126
name = "product"
164
127
rules = { [ { required : true , message : 'Required field' } ] } >
165
128
< Select
166
- placeholder = " Select one"
129
+ placeholder = { productLoading ? 'Fetching Options..' : ' Select one' }
167
130
mode = "single"
168
131
options = { products }
132
+ loading = { productLoading }
133
+ status = { productStatus }
169
134
onChange = { ( value ) => {
170
135
setMonitoringDetails ( {
171
136
...monitoringDetails ,
@@ -195,9 +160,13 @@ const BasicTab = ({
195
160
< AutoComplete
196
161
options = { orbitBuildSuggestions }
197
162
onSelect = { handleOrbitBuildSelect }
198
- onSearch = { ( searchText ) => loadOrbitBuildSuggestions ( searchText ) }
163
+ onSearch = { ( searchText ) => {
164
+ searchRef . current = searchText ;
165
+
166
+ loadOrbitBuildSuggestions ( searchText ) ;
167
+ } }
199
168
value = { selectedOrbitBuild }
200
- status = { loading ? 'warning' : null } > </ AutoComplete >
169
+ status = { status } > </ AutoComplete >
201
170
< Spin spinning = { loading } style = { { marginTop : '-1.6rem' , float : 'right' , marginRight : '1rem' } } > </ Spin >
202
171
</ Col >
203
172
</ Row >
0 commit comments