@@ -7,7 +7,7 @@ import { Button, Card, Empty, Form, List } from 'antd';
7
7
import * as codemirror from 'codemirror' ;
8
8
import 'codemirror/addon/display/placeholder' ;
9
9
import 'codemirror/mode/javascript/javascript' ;
10
- import { useEffect , useRef , useState } from 'react' ;
10
+ import { FC , MutableRefObject , useEffect , useRef , useState } from 'react' ;
11
11
import { UnControlled as CodeMirror } from 'react-codemirror2' ;
12
12
import ReactJson from 'react-json-view' ;
13
13
@@ -28,7 +28,7 @@ export type NexusESError = {
28
28
} ;
29
29
30
30
// TODO this needs to be broken into Input, Result, and Form components.
31
- const ElasticSearchQueryForm : React . FunctionComponent < {
31
+ const ElasticSearchQueryForm : FC < {
32
32
query : object ;
33
33
response ?: ElasticSearchViewQueryResponse < any > | null ;
34
34
busy : boolean ;
@@ -49,36 +49,45 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
49
49
onQueryChange,
50
50
onChangePageSize,
51
51
} ) : JSX . Element => {
52
- const [ initialQuery , setInitialQuery ] = useState ( '' ) ;
52
+ const [ _initialQuery , setInitialQuery ] = useState ( '' ) ;
53
53
const [ valid , setValid ] = useState ( true ) ;
54
- const [ value , setValue ] = useState < string > ( ) ;
54
+ // TODO Clean multiple states
55
+ const [ value , _setValue ] = useState < string > ( ) ;
55
56
const [ pageSize , setPageSize ] = useState < number > ( size ) ;
56
57
const editor = useRef < codemirror . Editor > ( ) ;
57
58
const wrapper = useRef ( null ) ;
58
59
60
+ const [ editorValue , setEditorValue ] = useState ( '' ) ; // New state to manage the editor content
61
+
59
62
useEffect ( ( ) => {
60
- // only on first render!
61
63
const formattedInitialQuery = JSON . stringify ( query , null , 2 ) ;
62
64
setInitialQuery ( formattedInitialQuery ) ;
63
- } , [ ] ) ;
64
-
65
- const data =
66
- response && response . hits . hits . map ( result => result . _source || [ ] ) ;
67
- const total =
68
- ( response && response . hits . total && response . hits . total . value ) || 0 ;
69
- const totalPages = Math . ceil ( total / size ) ;
70
- const current = Math . floor ( ( totalPages / total ) * from + 1 ) ;
65
+ setEditorValue ( formattedInitialQuery ) ; // Initialize the editor value with the formatted query
66
+ } , [ query ] ) ; // Dependency on `query` to update initial value when the prop changes
71
67
72
68
const handleChange = ( _ : any , __ : any , value : string ) => {
69
+ setEditorValue ( value ) ; // Update editor value directly
73
70
try {
74
71
JSON . parse ( value ) ;
75
- setValue ( value ) ;
76
72
setValid ( true ) ;
77
73
} catch ( error ) {
78
74
setValid ( false ) ;
79
75
}
80
76
} ;
81
77
78
+ useEffect ( ( ) => {
79
+ // only on first render!
80
+ const formattedInitialQuery = JSON . stringify ( query , null , 2 ) ;
81
+ setInitialQuery ( formattedInitialQuery ) ;
82
+ } , [ ] ) ;
83
+
84
+ const data =
85
+ response && response . hits . hits . map ( result => result . _source || [ ] ) ;
86
+ const total =
87
+ ( response && response . hits . total && response . hits . total . value ) || 0 ;
88
+ const totalPages = Math . ceil ( total / size ) ;
89
+ const current = Math . floor ( ( totalPages / total ) * from + 1 ) ;
90
+
82
91
const changePageSize = ( _ : number , size : number ) => {
83
92
setPageSize ( size ) ;
84
93
onChangePageSize ( size ) ;
@@ -106,7 +115,7 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
106
115
</ div >
107
116
</ div >
108
117
< CodeMirror
109
- value = { initialQuery }
118
+ value = { editorValue }
110
119
options = { {
111
120
mode : { name : 'javascript' , json : true } ,
112
121
theme : 'base16-light' ,
@@ -115,12 +124,12 @@ const ElasticSearchQueryForm: React.FunctionComponent<{
115
124
} }
116
125
onChange = { handleChange }
117
126
editorDidMount = { editorElement => {
118
- ( editor as React . MutableRefObject <
127
+ ( editor as MutableRefObject <
119
128
codemirror . Editor
120
129
> ) . current = editorElement ;
121
130
} }
122
131
editorWillUnmount = { ( ) => {
123
- const editorWrapper = ( editor as React . MutableRefObject <
132
+ const editorWrapper = ( editor as MutableRefObject <
124
133
CodeMirror . Editor
125
134
> ) . current . getWrapperElement ( ) ;
126
135
if ( editor ) editorWrapper . remove ( ) ;
0 commit comments