1- import React , { createContext , PropsWithChildren , useCallback , useEffect , useState } from 'react' ;
1+ import React , { createContext , PropsWithChildren , useCallback , useEffect , useState , FunctionComponent } from 'react' ;
22
33import { CoreApp , TimeRange } from '@grafana/data' ;
44
@@ -10,6 +10,9 @@ import { createReducer as createBucketAggsReducer } from './BucketAggregationsEd
1010import { reducer as metricsReducer } from './MetricAggregationsEditor/state/reducer' ;
1111import { aliasPatternReducer , queryReducer , initQuery , initExploreQuery } from './state' ;
1212import { getHook } from '@/utils/context' ;
13+ import { Provider , useDispatch } from "react-redux" ;
14+ import { initDefaults } from '@/store/defaults' ;
15+ import { store } from "@/store"
1316
1417export const RangeContext = createContext < TimeRange | undefined > ( undefined ) ;
1518export const useRange = getHook ( RangeContext ) ;
@@ -29,15 +32,31 @@ interface Props {
2932 range : TimeRange ;
3033}
3134
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 ( ( {
3346 children,
3447 onChange,
3548 onRunQuery,
3649 query,
3750 app,
3851 datasource,
3952 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+
4160 const onStateChange = useCallback (
4261 ( query : ElasticsearchQuery ) => {
4362 onChange ( query ) ;
@@ -77,7 +96,7 @@ export const ElasticsearchProvider = ({
7796 } , [ shouldRunInit , dispatch , isUninitialized , app ] ) ;
7897
7998 if ( isUninitialized ) {
80- return null ;
99+ return ( < > </ > ) ;
81100 }
82101
83102 return (
@@ -89,4 +108,4 @@ export const ElasticsearchProvider = ({
89108 </ QueryContext . Provider >
90109 </ DatasourceContext . Provider >
91110 ) ;
92- } ;
111+ } ) ;
0 commit comments