From 5ec8fc885b378d448c3d678dbd1f1c2134c15e43 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 28 Aug 2020 18:03:23 +0300 Subject: [PATCH] Config: fix direct db connection ds selector, closes #1027 --- .../components/ConfigEditor.tsx | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/datasource-zabbix/components/ConfigEditor.tsx b/src/datasource-zabbix/components/ConfigEditor.tsx index 1ef1b8a0e..66f1bb2cc 100644 --- a/src/datasource-zabbix/components/ConfigEditor.tsx +++ b/src/datasource-zabbix/components/ConfigEditor.tsx @@ -1,8 +1,8 @@ import React, { useEffect, useState } from 'react'; import { getDataSourceSrv } from '@grafana/runtime'; import { DataSourcePluginOptionsEditorProps, DataSourceSettings, SelectableValue } from '@grafana/data'; -import { DataSourceHttpSettings, LegacyForms, Field, Input, Button, InlineFormLabel } from '@grafana/ui'; -const { Select, FormField, Switch } = LegacyForms; +import { DataSourceHttpSettings, LegacyForms, Field, Input, Button, InlineFormLabel, Select } from '@grafana/ui'; +const { FormField, Switch } = LegacyForms; import { ZabbixDSOptions, ZabbixSecureJSONData } from '../types'; const SUPPORTED_SQL_DS = ['mysql', 'postgres', 'influxdb']; @@ -67,7 +67,7 @@ export const ConfigEditor = (props: Props) => { return ( <> { <>
Data Source -
{currentDSType === 'influxdb' &&
@@ -265,6 +270,26 @@ const resetSecureJsonField = ( }); }; +const directDBDatasourceChanegeHandler = ( + options: DataSourceSettings, + onChange: Props['onOptionsChange'], + setSelectedDS: React.Dispatch, + setSelectedDSType: React.Dispatch, +) => ( + value: SelectableValue +) => { + const selectedDs = getDirectDBDatasources().find(dsOption => dsOption.id === value.value); + setSelectedDS({ label: selectedDs.name, value: selectedDs.id }); + setSelectedDSType(selectedDs.type); + onChange({ + ...options, + jsonData: { + ...options.jsonData, + dbConnectionDatasourceId: value.value + }, + }); +}; + const getDirectDBDatasources = () => { let dsList = (getDataSourceSrv() as any).getAll(); dsList = dsList.filter(ds => SUPPORTED_SQL_DS.includes(ds.type)); @@ -273,6 +298,6 @@ const getDirectDBDatasources = () => { const getDirectDBDSOptions = () => { const dsList = getDirectDBDatasources(); - const dsOpts: Array> = dsList.map(ds => ({ label: ds.name, value: ds.id })); + const dsOpts: Array> = dsList.map(ds => ({ label: ds.name, value: ds.id, description: ds.type })); return dsOpts; };