@@ -20,7 +20,6 @@ import {
20
20
import {
21
21
CONFIG_STEP ,
22
22
customStringify ,
23
- FETCH_ALL_QUERY ,
24
23
QueryParam ,
25
24
SearchResponse ,
26
25
SearchResponseVerbose ,
@@ -47,6 +46,12 @@ interface QueryProps {
47
46
hasSearchPipeline : boolean ;
48
47
hasIngestResources : boolean ;
49
48
selectedStep : CONFIG_STEP ;
49
+ queryRequest : string ;
50
+ setQueryRequest : ( queryRequest : string ) => void ;
51
+ queryResponse : SearchResponse | undefined ;
52
+ setQueryResponse : ( queryResponse : SearchResponse | undefined ) => void ;
53
+ queryParams : QueryParam [ ] ;
54
+ setQueryParams : ( queryParams : QueryParam [ ] ) => void ;
50
55
}
51
56
52
57
const SEARCH_OPTIONS = [
@@ -66,61 +71,34 @@ export function Query(props: QueryProps) {
66
71
const dispatch = useAppDispatch ( ) ;
67
72
const dataSourceId = getDataSourceId ( ) ;
68
73
const dataSourceVersion = useDataSourceVersion ( dataSourceId ) ;
69
-
70
74
const { loading } = useSelector ( ( state : AppState ) => state . opensearch ) ;
71
-
72
- // Form state
73
75
const { values } = useFormikContext < WorkflowFormValues > ( ) ;
74
76
75
- // query response state
76
- const [ queryResponse , setQueryResponse ] = useState <
77
- SearchResponse | undefined
78
- > ( undefined ) ;
79
-
80
- // Standalone / sandboxed search request state. Users can test things out
81
- // without updating the base form / persisted value.
82
- // Update if the parent form values are changed, or if a newly-created search pipeline is detected.
83
- const [ tempRequest , setTempRequest ] = useState < string > ( '' ) ;
84
- useEffect ( ( ) => {
85
- if ( ! isEmpty ( values ?. search ?. request ) ) {
86
- setTempRequest ( values ?. search ?. request ) ;
87
- } else {
88
- setTempRequest ( customStringify ( FETCH_ALL_QUERY ) ) ;
89
- }
90
- } , [ values ?. search ?. request ] ) ;
91
-
92
77
// state for if to execute search w/ or w/o any configured search pipeline.
93
78
// default based on if there is an available search pipeline or not.
94
79
const [ includePipeline , setIncludePipeline ] = useState < boolean > ( false ) ;
95
80
useEffect ( ( ) => {
96
81
setIncludePipeline ( props . hasSearchPipeline ) ;
97
82
} , [ props . hasSearchPipeline ] ) ;
98
83
99
- // query params state
100
- const [ queryParams , setQueryParams ] = useState < QueryParam [ ] > ( [ ] ) ;
101
-
102
- // Do a few things when the request is changed:
103
- // 1. Check if there is a new set of query parameters, and if so,
104
- // reset the form.
105
- // 2. Clear any stale results
84
+ // Check if there is a new set of query parameters, and if so, reset the form
106
85
useEffect ( ( ) => {
107
- const placeholders = getPlaceholdersFromQuery ( tempRequest ) ;
86
+ const placeholders = getPlaceholdersFromQuery ( props . queryRequest ) ;
108
87
if (
109
88
! containsSameValues (
110
89
placeholders ,
111
- queryParams . map ( ( queryParam ) => queryParam . name )
90
+ props . queryParams . map ( ( queryParam ) => queryParam . name )
112
91
)
113
92
) {
114
- setQueryParams (
93
+ props . setQueryParams (
115
94
placeholders . map ( ( placeholder ) => ( {
116
95
name : placeholder ,
117
96
type : 'Text' ,
118
97
value : '' ,
119
98
} ) )
120
99
) ;
121
100
}
122
- setQueryResponse ( undefined ) ;
123
- } , [ tempRequest ] ) ;
101
+ } , [ props . queryRequest ] ) ;
124
102
125
103
// empty states
126
104
const noSearchIndex = isEmpty ( values ?. search ?. index ?. name ) ;
@@ -192,15 +170,18 @@ export function Query(props: QueryProps) {
192
170
fill = { true }
193
171
isLoading = { loading }
194
172
disabled = {
195
- containsEmptyValues ( queryParams ) ||
173
+ containsEmptyValues ( props . queryParams ) ||
196
174
isEmpty ( indexToSearch )
197
175
}
198
176
onClick = { ( ) => {
199
177
dispatch (
200
178
searchIndex ( {
201
179
apiBody : {
202
180
index : indexToSearch ,
203
- body : injectParameters ( queryParams , tempRequest ) ,
181
+ body : injectParameters (
182
+ props . queryParams ,
183
+ props . queryRequest
184
+ ) ,
204
185
searchPipeline : includePipeline
205
186
? values ?. search ?. pipelineName
206
187
: '_none' ,
@@ -230,11 +211,11 @@ export function Query(props: QueryProps) {
230
211
setSearchPipelineErrors ( { errors : { } } ) ;
231
212
}
232
213
233
- setQueryResponse ( resp ) ;
214
+ props . setQueryResponse ( resp ) ;
234
215
}
235
216
)
236
217
. catch ( ( error : any ) => {
237
- setQueryResponse ( undefined ) ;
218
+ props . setQueryResponse ( undefined ) ;
238
219
setSearchPipelineErrors ( { errors : { } } ) ;
239
220
console . error ( 'Error running query: ' , error ) ;
240
221
} ) ;
@@ -252,12 +233,12 @@ export function Query(props: QueryProps) {
252
233
</ EuiFlexItem >
253
234
{ props . selectedStep === CONFIG_STEP . SEARCH &&
254
235
! isEmpty ( values ?. search ?. request ) &&
255
- values ?. search ?. request !== tempRequest && (
236
+ values ?. search ?. request !== props . queryRequest && (
256
237
< EuiFlexItem grow = { false } style = { { marginBottom : '0px' } } >
257
238
< EuiSmallButtonEmpty
258
239
disabled = { false }
259
240
onClick = { ( ) => {
260
- setTempRequest ( values ?. search ?. request ) ;
241
+ props . setQueryRequest ( values ?. search ?. request ) ;
261
242
} }
262
243
>
263
244
Revert to original query
@@ -272,13 +253,16 @@ export function Query(props: QueryProps) {
272
253
theme = "textmate"
273
254
width = "100%"
274
255
height = { '100%' }
275
- value = { tempRequest }
256
+ value = { props . queryRequest }
276
257
onChange = { ( input ) => {
277
- setTempRequest ( input ) ;
258
+ props . setQueryRequest ( input ) ;
278
259
} }
260
+ // format the JSON on blur
279
261
onBlur = { ( ) => {
280
262
try {
281
- setTempRequest ( customStringify ( JSON . parse ( tempRequest ) ) ) ;
263
+ props . setQueryRequest (
264
+ customStringify ( JSON . parse ( props . queryRequest ) )
265
+ ) ;
282
266
} catch ( error ) { }
283
267
} }
284
268
readOnly = { false }
@@ -299,8 +283,8 @@ export function Query(props: QueryProps) {
299
283
* This may return nothing if the list of params are empty
300
284
*/ }
301
285
< QueryParamsList
302
- queryParams = { queryParams }
303
- setQueryParams = { setQueryParams }
286
+ queryParams = { props . queryParams }
287
+ setQueryParams = { props . setQueryParams }
304
288
/>
305
289
</ EuiFlexItem >
306
290
</ EuiFlexGroup >
@@ -311,7 +295,8 @@ export function Query(props: QueryProps) {
311
295
< EuiText size = "m" > Results</ EuiText >
312
296
</ EuiFlexItem >
313
297
< EuiFlexItem >
314
- { queryResponse === undefined || isEmpty ( queryResponse ) ? (
298
+ { props . queryResponse === undefined ||
299
+ isEmpty ( props . queryResponse ) ? (
315
300
< EuiEmptyPrompt
316
301
title = { < h2 > No results</ h2 > }
317
302
titleSize = "s"
@@ -324,7 +309,7 @@ export function Query(props: QueryProps) {
324
309
}
325
310
/>
326
311
) : (
327
- < Results response = { queryResponse } />
312
+ < Results response = { props . queryResponse } />
328
313
) }
329
314
</ EuiFlexItem >
330
315
</ EuiFlexGroup >
0 commit comments