@@ -13,36 +13,27 @@ import {
13
13
InfoBox ,
14
14
} from '@grafana/ui' ;
15
15
import { SelectableValue , FieldType , QueryEditorProps } from '@grafana/data' ;
16
- import { JsonApiQuery , defaultQuery , JsonApiDataSourceOptions } from '../types' ;
16
+ import { JsonApiQuery , JsonApiDataSourceOptions , defaultQuery } from '../types' ;
17
17
import { JsonPathQueryField } from './JsonPathQueryField' ;
18
18
import { KeyValueEditor } from './KeyValueEditor' ;
19
19
import AutoSizer from 'react-virtualized-auto-sizer' ;
20
20
import { css } from 'emotion' ;
21
21
import { Pair } from '../types' ;
22
22
import { JsonDataSource } from 'datasource' ;
23
23
24
+ // Display a warning message when user adds any of the following headers.
25
+ const sensitiveHeaders = [ 'authorization' , 'proxy-authorization' , 'x-api-key' ] ;
26
+
24
27
interface Props extends QueryEditorProps < JsonDataSource , JsonApiQuery , JsonApiDataSourceOptions > {
25
28
limitFields ?: number ;
26
29
}
27
30
28
- export const QueryEditor : React . FC < Props > = ( { onRunQuery, onChange, query , limitFields, datasource, range } ) => {
31
+ export const QueryEditor : React . FC < Props > = ( { onRunQuery, onChange, limitFields, datasource, range, ... props } ) => {
29
32
const [ bodyType , setBodyType ] = useState ( 'plaintext' ) ;
30
33
const [ tabIndex , setTabIndex ] = useState ( 0 ) ;
31
34
const theme = useTheme ( ) ;
32
35
33
- const { fields } = defaults ( query , { ...defaultQuery , fields : [ { name : '' , jsonPath : '' } ] } ) as JsonApiQuery ;
34
-
35
- // Display a warning message when user adds any of the following headers.
36
- const sensitiveHeaders = [ 'authorization' , 'proxy-authorization' , 'x-api-key' ] ;
37
-
38
- const params : Array < Pair < string , string > > = query . params ?? [ ] ;
39
-
40
- // Backwards-compatibility with old queryString property.
41
- if ( ! query . params ) {
42
- new URLSearchParams ( '?' + query . queryParams ) . forEach ( ( value : string , key : string ) => {
43
- params . push ( [ key , value ] ) ;
44
- } ) ;
45
- }
36
+ const query = defaults ( props . query , defaultQuery ) ;
46
37
47
38
const onMethodChange = ( method : string ) => {
48
39
onChange ( { ...query , method } ) ;
@@ -56,20 +47,25 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
56
47
57
48
const onParamsChange = ( params : Array < Pair < string , string > > ) => {
58
49
onChange ( { ...query , params } ) ;
50
+ onRunQuery ( ) ;
59
51
} ;
60
52
61
53
const onHeadersChange = ( headers : Array < Pair < string , string > > ) => {
62
54
onChange ( { ...query , headers } ) ;
55
+ onRunQuery ( ) ;
63
56
} ;
64
57
65
58
const onChangePath = ( i : number ) => ( e : string ) => {
59
+ const { fields } = query ;
66
60
onChange ( {
67
61
...query ,
68
62
fields : fields . map ( ( field , n ) => ( i === n ? { ...fields [ i ] , jsonPath : e } : field ) ) ,
69
63
} ) ;
64
+ onRunQuery ( ) ;
70
65
} ;
71
66
72
67
const onChangeType = ( i : number ) => ( e : SelectableValue < string > ) => {
68
+ const { fields } = query ;
73
69
onChange ( {
74
70
...query ,
75
71
fields : fields . map ( ( field , n ) =>
@@ -80,15 +76,18 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
80
76
} ;
81
77
82
78
const addField = ( i : number ) => ( ) => {
79
+ const { fields } = query ;
83
80
if ( ! limitFields || fields . length < limitFields ) {
84
81
onChange ( {
85
82
...query ,
86
83
fields : [ ...fields . slice ( 0 , i + 1 ) , { name : '' , jsonPath : '' } , ...fields . slice ( i + 1 ) ] ,
87
84
} ) ;
85
+ onRunQuery ( ) ;
88
86
}
89
87
} ;
90
88
91
89
const removeField = ( i : number ) => ( ) => {
90
+ const { fields } = query ;
92
91
onChange ( {
93
92
...query ,
94
93
fields : [ ...fields . slice ( 0 , i ) , ...fields . slice ( i + 1 ) ] ,
@@ -99,8 +98,8 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
99
98
const tabs = [
100
99
{
101
100
title : 'Fields' ,
102
- content : fields
103
- ? fields . map ( ( field , index ) => (
101
+ content : query . fields
102
+ ? query . fields . map ( ( field , index ) => (
104
103
< InlineFieldRow key = { index } >
105
104
< InlineField
106
105
label = "Field"
@@ -137,13 +136,13 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
137
136
/>
138
137
</ InlineField >
139
138
140
- { ( ! limitFields || fields . length < limitFields ) && (
139
+ { ( ! limitFields || query . fields . length < limitFields ) && (
141
140
< a className = "gf-form-label" onClick = { addField ( index ) } >
142
141
< Icon name = "plus" />
143
142
</ a >
144
143
) }
145
144
146
- { fields . length > 1 ? (
145
+ { query . fields . length > 1 ? (
147
146
< a className = "gf-form-label" onClick = { removeField ( index ) } >
148
147
< Icon name = "minus" />
149
148
</ a >
@@ -182,7 +181,7 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
182
181
< KeyValueEditor
183
182
addRowLabel = { 'Add param' }
184
183
columns = { [ 'Key' , 'Value' ] }
185
- values = { params ?? [ ] }
184
+ values = { query . params ?? [ ] }
186
185
onChange = { onParamsChange }
187
186
onBlur = { ( ) => onRunQuery ( ) }
188
187
/>
@@ -287,9 +286,7 @@ export const QueryEditor: React.FC<Props> = ({ onRunQuery, onChange, query, limi
287
286
) ;
288
287
} ;
289
288
290
- const defaultCacheDuration = 300 ;
291
-
292
- export const formatCacheTimeLabel = ( s : number = defaultCacheDuration ) => {
289
+ export const formatCacheTimeLabel = ( s : number ) => {
293
290
if ( s < 60 ) {
294
291
return s + 's' ;
295
292
} else if ( s < 3600 ) {
0 commit comments