Browse Source

Merge branch 'fix-errors-on-panel-init' into 'main'

TypeError: Cannot read properties of undefined (reading 'useMetric')

See merge request chartwerk/grafana-chartwerk-panel!9
pull/2/head
Alexander Velikiy 2 years ago
parent
commit
0e5ae000b2
  1. 2
      package.json
  2. 10
      src/components/editors/UseMetricEditor.tsx
  3. 10
      src/models/options.ts
  4. 2
      src/models/series.ts
  5. 3
      src/module.ts

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "grafana-chartwerk-panel", "name": "grafana-chartwerk-panel",
"version": "0.4.0", "version": "0.4.1",
"description": "Chartwerk Panel", "description": "Chartwerk Panel",
"scripts": { "scripts": {
"build": "grafana-toolkit plugin:build", "build": "grafana-toolkit plugin:build",

10
src/components/editors/UseMetricEditor.tsx

@ -18,7 +18,7 @@ const fieldNamePickerSettings = {
} as any; } as any;
export function UseMetricEditor({ onChange, value, context }: StandardEditorProps<UseMetricConfig>) { export function UseMetricEditor({ onChange, value, context }: StandardEditorProps<UseMetricConfig>) {
const config = value; let config: UseMetricConfig = value;
const onFieldChange = (field: keyof UseMetricConfig, value: any) => { const onFieldChange = (field: keyof UseMetricConfig, value: any) => {
// @ts-ignore // @ts-ignore
@ -31,22 +31,22 @@ export function UseMetricEditor({ onChange, value, context }: StandardEditorProp
<HorizontalGroup> <HorizontalGroup>
<InlineField label="Use metric"> <InlineField label="Use metric">
<InlineSwitch <InlineSwitch
value={config.useMetric} value={config?.useMetric}
onChange={(evt) => onFieldChange('useMetric', (evt.target as any).checked)} onChange={(evt) => onFieldChange('useMetric', (evt.target as any).checked)}
/> />
</InlineField> </InlineField>
<InlineField> <InlineField>
{config.useMetric ? ( {config?.useMetric ? (
<FieldNamePicker <FieldNamePicker
context={context} context={context}
value={config.metricName as string} value={config?.metricName as string}
onChange={(newVal: any) => onFieldChange('metricName', newVal)} onChange={(newVal: any) => onFieldChange('metricName', newVal)}
item={fieldNamePickerSettings} item={fieldNamePickerSettings}
/> />
) : ( ) : (
<Input <Input
placeholder="value" placeholder="value"
value={config.value} value={config?.value}
width={24} width={24}
onChange={(evt) => onFieldChange('value', (evt.target as any).value)} onChange={(evt) => onFieldChange('value', (evt.target as any).value)}
/> />

10
src/models/options.ts

@ -21,7 +21,10 @@ export class Options {
} }
private _setMin(): void { private _setMin(): void {
if (!this.grafanaOptions.gauge.min.useMetric) { if (!this.grafanaOptions.gauge.min) {
throw new Error(`Min Config is not selected: [See options: Extremum -> Min]`);
}
if (!this.grafanaOptions.gauge.min?.useMetric) {
this.minValue = this.grafanaOptions.gauge.min.value; this.minValue = this.grafanaOptions.gauge.min.value;
return; return;
} }
@ -30,7 +33,10 @@ export class Options {
} }
private _setMax(): void { private _setMax(): void {
if (!this.grafanaOptions.gauge.max.useMetric) { if (!this.grafanaOptions.gauge.max) {
throw new Error(`Max Config is not selected: [See options: Extremum -> Max]`);
}
if (!this.grafanaOptions.gauge.max?.useMetric) {
this.maxValue = this.grafanaOptions.gauge.max.value; this.maxValue = this.grafanaOptions.gauge.max.value;
return; return;
} }

2
src/models/series.ts

@ -10,7 +10,7 @@ export class Series {
private _selectedSerieName; private _selectedSerieName;
constructor(grafanaSeriesList: any, private gaugeValueOptions: ValueOptions) { constructor(grafanaSeriesList: any, private gaugeValueOptions: ValueOptions) {
if (_.isEmpty(this.gaugeValueOptions.metricName)) { if (_.isEmpty(this.gaugeValueOptions?.metricName)) {
throw new Error(`Value: metric is not selected. [See options: Value -> Metric]`); throw new Error(`Value: metric is not selected. [See options: Value -> Metric]`);
} }
this._selectedSerieName = this.gaugeValueOptions.metricName; this._selectedSerieName = this.gaugeValueOptions.metricName;

3
src/module.ts

@ -49,12 +49,12 @@ export const plugin = new PanelPlugin<PanelOptions>(Panel).setPanelOptions((buil
category: ['Value'], category: ['Value'],
showIf: (config) => config.visualizationType === Pod.GAUGE, showIf: (config) => config.visualizationType === Pod.GAUGE,
}) })
// TODO: defaults?
.addCustomEditor({ .addCustomEditor({
id: 'min', id: 'min',
name: 'Min', name: 'Min',
path: 'gauge.min', path: 'gauge.min',
category: ['Extremum'], category: ['Extremum'],
defaultValue: { useMetric: false, value: 0 },
showIf: (config) => config.visualizationType === Pod.GAUGE, showIf: (config) => config.visualizationType === Pod.GAUGE,
editor: UseMetricEditor as any, editor: UseMetricEditor as any,
}) })
@ -63,6 +63,7 @@ export const plugin = new PanelPlugin<PanelOptions>(Panel).setPanelOptions((buil
name: 'Max', name: 'Max',
path: 'gauge.max', path: 'gauge.max',
category: ['Extremum'], category: ['Extremum'],
defaultValue: { useMetric: false },
showIf: (config) => config.visualizationType === Pod.GAUGE, showIf: (config) => config.visualizationType === Pod.GAUGE,
editor: UseMetricEditor as any, editor: UseMetricEditor as any,
}) })

Loading…
Cancel
Save