-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniil Suleiman
committed
Mar 2, 2021
1 parent
e9a4409
commit b1b1aed
Showing
6 changed files
with
110 additions
and
88 deletions.
There are no files selected for viewing
85 changes: 0 additions & 85 deletions
85
src/plugins/vis_type_timeseries/public/application/components/panel_config.js
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
src/plugins/vis_type_timeseries/public/application/components/panel_config.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useState, useEffect, useCallback, useRef } from 'react'; | ||
import { FormattedMessage } from '@kbn/i18n/react'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { IUiSettingsClient } from 'kibana/public'; | ||
import { FormValidationContext } from '../contexts/form_validation_context'; | ||
import { VisDataContext } from '../contexts/vis_data_context'; | ||
import { panelConfigTypes } from './panel_config/index'; | ||
import { TimeseriesVisParams } from '../../types'; | ||
import { VisFields } from '../lib/fetch_fields'; | ||
|
||
interface FormValidationResults { | ||
[key: string]: boolean; | ||
} | ||
|
||
interface PanelConfigProps { | ||
fields?: VisFields; | ||
model: TimeseriesVisParams; | ||
visData$: Observable<TimeseriesVisParams>; | ||
getConfig: IUiSettingsClient['get']; | ||
onChange: (partialModel: Partial<TimeseriesVisParams>) => void; | ||
} | ||
|
||
const checkModelValidity = (validationResults: FormValidationResults) => | ||
Object.values(validationResults).every((isValid) => isValid); | ||
|
||
export function PanelConfig(props: PanelConfigProps) { | ||
const { model, onChange } = props; | ||
const Component = panelConfigTypes[model.type]; | ||
const formValidationResults = useRef<FormValidationResults>({}); | ||
const [visData, setVisData] = useState(model); | ||
|
||
useEffect(() => { | ||
const visDataSubscription = props.visData$.subscribe(setVisData); | ||
|
||
return function cleanup() { | ||
visDataSubscription.unsubscribe(); | ||
}; | ||
}, [model.id, props.visData$]); | ||
|
||
const updateControlValidity = useCallback( | ||
(controlKey: string, isControlValid: boolean) => { | ||
formValidationResults.current[controlKey] = isControlValid; | ||
onChange({ isModelInvalid: !checkModelValidity(formValidationResults.current) }); | ||
}, | ||
[onChange] | ||
); | ||
|
||
if (Component) { | ||
return ( | ||
<FormValidationContext.Provider value={updateControlValidity}> | ||
<VisDataContext.Provider value={visData}> | ||
<div data-test-subj={`tvbPanelConfig__${model.type}`}> | ||
<Component {...props} /> | ||
</div> | ||
</VisDataContext.Provider> | ||
</FormValidationContext.Provider> | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
<FormattedMessage | ||
id="visTypeTimeseries.missingPanelConfigDescription" | ||
defaultMessage="Missing panel config for “{modelType}”" | ||
values={{ modelType: model.type }} | ||
/> | ||
</div> | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/plugins/vis_type_timeseries/public/application/components/panel_config/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
// these are not typed yet | ||
// @ts-expect-error | ||
import { TimeseriesPanelConfig as timeseries } from './panel_config/timeseries'; | ||
// @ts-expect-error | ||
import { MetricPanelConfig as metric } from './panel_config/metric'; | ||
// @ts-expect-error | ||
import { TopNPanelConfig as topN } from './panel_config/top_n'; | ||
// @ts-expect-error | ||
import { TablePanelConfig as table } from './panel_config/table'; | ||
// @ts-expect-error | ||
import { GaugePanelConfig as gauge } from './panel_config/gauge'; | ||
// @ts-expect-error | ||
import { MarkdownPanelConfig as markdown } from './panel_config/markdown'; | ||
|
||
export const panelConfigTypes = { | ||
timeseries, | ||
table, | ||
metric, | ||
top_n: topN, | ||
gauge, | ||
markdown, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.