3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import React , { useEffect , useState } from 'react' ;
7
- import {
8
- EuiCodeEditor ,
9
- EuiFieldText ,
10
- EuiFlexGroup ,
11
- EuiFlexItem ,
12
- EuiFormRow ,
13
- EuiTitle ,
14
- } from '@elastic/eui' ;
15
- import { Field , FieldProps } from 'formik' ;
6
+ import React , { useEffect } from 'react' ;
7
+ import { EuiFlexGroup , EuiFlexItem , EuiTitle } from '@elastic/eui' ;
8
+ import { useFormikContext } from 'formik' ;
9
+ import { IConfigField , WorkspaceFormValues } from '../../../../../common' ;
10
+ import { JsonField } from '../input_fields' ;
16
11
17
12
interface ConfigureSearchRequestProps {
18
- setQuery : ( query : { } ) => void ;
13
+ setQuery : ( query : string ) => void ;
14
+ onFormChange : ( ) => void ;
19
15
}
20
16
21
17
/**
22
18
* Input component for configuring a search request
23
19
*/
24
20
export function ConfigureSearchRequest ( props : ConfigureSearchRequestProps ) {
25
- const indexFieldPath = 'ingest.index.name' ;
26
-
27
- // query state
28
- const [ queryStr , setQueryStr ] = useState < string > ( '{}' ) ;
21
+ const { values } = useFormikContext < WorkspaceFormValues > ( ) ;
29
22
23
+ // Hook to listen when the query form value changes.
24
+ // Try to set the query request if possible
30
25
useEffect ( ( ) => {
31
- try {
32
- const query = JSON . parse ( queryStr ) ;
33
- props . setQuery ( query ) ;
34
- } catch ( e ) {
35
- props . setQuery ( { } ) ;
26
+ if ( values ?. search ?. request ) {
27
+ props . setQuery ( values . search . request ) ;
36
28
}
37
- } , [ queryStr ] ) ;
29
+ } , [ values ?. search ?. request ] ) ;
38
30
39
31
return (
40
32
< EuiFlexGroup direction = "column" >
@@ -43,46 +35,20 @@ export function ConfigureSearchRequest(props: ConfigureSearchRequestProps) {
43
35
< h2 > Configure query</ h2 >
44
36
</ EuiTitle >
45
37
</ EuiFlexItem >
46
- < EuiFlexItem >
47
- < EuiFlexGroup direction = "column" >
48
- < EuiFlexItem grow = { false } >
49
- < Field name = { indexFieldPath } >
50
- { ( { field, form } : FieldProps ) => {
51
- return (
52
- // TODO: make this dynamic depending on if ingest is defined or not.
53
- // 1/ (incomplete) if no ingest, make this a dropdown to select existing indices
54
- // 2/ (complete) if ingest, show the defined index from ingest config, make it readonly
55
- < EuiFormRow key = { indexFieldPath } label = { 'Retrieval index' } >
56
- < EuiFieldText
57
- { ...field }
58
- compressed = { false }
59
- value = { field . value }
60
- readOnly = { true }
61
- />
62
- </ EuiFormRow >
63
- ) ;
64
- } }
65
- </ Field >
66
- </ EuiFlexItem >
67
- < EuiFlexItem grow = { false } >
68
- < EuiCodeEditor
69
- mode = "json"
70
- theme = "textmate"
71
- width = "100%"
72
- height = "25vh"
73
- value = { queryStr }
74
- onChange = { ( input ) => {
75
- setQueryStr ( input ) ;
76
- } }
77
- readOnly = { false }
78
- setOptions = { {
79
- fontSize : '14px' ,
80
- } }
81
- aria-label = "Code Editor"
82
- tabSize = { 2 }
83
- />
84
- </ EuiFlexItem >
85
- </ EuiFlexGroup >
38
+ < EuiFlexItem grow = { false } >
39
+ < JsonField
40
+ // We want to integrate query into the form, but not persist in the config.
41
+ // So, we create the ConfigField explicitly inline, instead of pulling
42
+ // from the config.
43
+ field = {
44
+ {
45
+ label : 'Define query' ,
46
+ } as IConfigField
47
+ }
48
+ fieldPath = { 'search.request' }
49
+ onFormChange = { props . onFormChange }
50
+ editorHeight = "25vh"
51
+ />
86
52
</ EuiFlexItem >
87
53
</ EuiFlexGroup >
88
54
) ;
0 commit comments