1
- import React , { createContext , PropsWithChildren , useCallback , useEffect , useState } from 'react' ;
1
+ import React , { createContext , PropsWithChildren , useCallback , useEffect , useState , FunctionComponent } from 'react' ;
2
2
3
3
import { CoreApp , TimeRange } from '@grafana/data' ;
4
4
@@ -10,6 +10,9 @@ import { createReducer as createBucketAggsReducer } from './BucketAggregationsEd
10
10
import { reducer as metricsReducer } from './MetricAggregationsEditor/state/reducer' ;
11
11
import { aliasPatternReducer , queryReducer , initQuery , initExploreQuery } from './state' ;
12
12
import { getHook } from '@/utils/context' ;
13
+ import { Provider , useDispatch } from "react-redux" ;
14
+ import { initDefaults } from '@/store/defaults' ;
15
+ import { store } from "@/store"
13
16
14
17
export const RangeContext = createContext < TimeRange | undefined > ( undefined ) ;
15
18
export const useRange = getHook ( RangeContext ) ;
@@ -29,15 +32,31 @@ interface Props {
29
32
range : TimeRange ;
30
33
}
31
34
32
- export const ElasticsearchProvider = ( {
35
+ function withStore < P extends PropsWithChildren < Props > > ( Component : FunctionComponent < P > ) : FunctionComponent < P > {
36
+ const newComp = ( props : P ) => (
37
+ < Provider store = { store } >
38
+ < Component { ...props } />
39
+ </ Provider >
40
+ )
41
+ newComp . displayName = Component . displayName
42
+ return newComp
43
+ }
44
+
45
+ export const ElasticsearchProvider = withStore ( ( {
33
46
children,
34
47
onChange,
35
48
onRunQuery,
36
49
query,
37
50
app,
38
51
datasource,
39
52
range,
40
- } : PropsWithChildren < Props > ) => {
53
+ } : PropsWithChildren < Props > ) : JSX . Element => {
54
+
55
+ const storeDispatch = useDispatch ( ) ;
56
+ useEffect ( ( ) => {
57
+ storeDispatch ( initDefaults ( datasource . queryEditorConfig ?. defaults ) )
58
+ } , [ storeDispatch , datasource ] )
59
+
41
60
const onStateChange = useCallback (
42
61
( query : ElasticsearchQuery ) => {
43
62
onChange ( query ) ;
@@ -77,7 +96,7 @@ export const ElasticsearchProvider = ({
77
96
} , [ shouldRunInit , dispatch , isUninitialized , app ] ) ;
78
97
79
98
if ( isUninitialized ) {
80
- return null ;
99
+ return ( < > </ > ) ;
81
100
}
82
101
83
102
return (
@@ -89,4 +108,4 @@ export const ElasticsearchProvider = ({
89
108
</ QueryContext . Provider >
90
109
</ DatasourceContext . Provider >
91
110
) ;
92
- } ;
111
+ } ) ;
0 commit comments