diff --git a/src/ButtonPanel.tsx b/src/ButtonPanel.tsx index fa8ce9f..3c18a75 100644 --- a/src/ButtonPanel.tsx +++ b/src/ButtonPanel.tsx @@ -1,8 +1,7 @@ import React, { PureComponent } from 'react'; -import { Button } from '@grafana/ui'; +import { Button, IconName } from '@grafana/ui'; import { PanelProps } from '@grafana/data'; import { ButtonPanelOptions, ButtonPanelState } from 'types'; -import { IconName } from '@grafana/ui'; interface Props extends PanelProps {} @@ -28,7 +27,7 @@ export class ButtonPanel extends PureComponent { }; const exeucte = () => { this.setState({ api_call: 'IN_PROGRESS' }); - console.log(options.method, ' to ', options.url, ' with key as ', options.type?.value); + console.log(options.method?.value, ' to ', options.url, ' with key as ', options.type?.value); const url = new URL(options.url); @@ -46,11 +45,14 @@ export class ButtonPanel extends PureComponent { //referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url }; - debugger; if (options.type?.value === 'HEADER') { - requestHeaders.set('X-API-Key', options.key); + options.params.forEach(e => { + requestHeaders.set(e[0], e[1]); + }); } else if (options.type?.value === 'QUERY') { - url.searchParams.append('api-key', options.key); + options.params.forEach(e => { + url.searchParams.append(e[0], e[1]); + }); } else { console.error('Unknown api key type', options.type); } diff --git a/src/ButtonPanelEditor.tsx b/src/ButtonPanelEditor.tsx index 9d8ead6..fc08df7 100644 --- a/src/ButtonPanelEditor.tsx +++ b/src/ButtonPanelEditor.tsx @@ -1,19 +1,33 @@ import React, { PureComponent } from 'react'; -import { Select } from '@grafana/ui'; -import { Field, Input } from '@grafana/ui'; -import { PanelEditorProps } from '@grafana/data'; +import { Field, IconButton, Input, Select, HorizontalGroup, VerticalGroup } from '@grafana/ui'; +import { DataFrame, FieldType, MutableDataFrame, PanelEditorProps } from '@grafana/data'; import { ButtonPanelOptions } from './types'; export class ButtonPanelEditor extends PureComponent> { + onParamRemove = (key: string) => ({ target }: any) => { + let newParams = this.props.options.params.filter(e => e[0] !== key); + this.props.onOptionsChange({ ...this.props.options, params: newParams }); + }; + onParamAdd = ({ target }: any) => { + const key = this.props.options.newParamName; + let newParams = this.props.options.params.filter(e => e[0] !== key); + newParams.push([key, this.props.options.newParamValue]); + newParams.sort((a, b) => a[0].localeCompare(b[0])); + const msg = { ...this.props.options, newParamName: '', newParamValue: '', params: newParams }; + this.props.onOptionsChange(msg); + }; + onNewParamNameChanged = ({ target }: any) => { + this.props.onOptionsChange({ ...this.props.options, newParamName: target.value }); + }; + onNewParamValueChanged = ({ target }: any) => { + this.props.onOptionsChange({ ...this.props.options, newParamValue: target.value }); + }; onTextChanged = ({ target }: any) => { this.props.onOptionsChange({ ...this.props.options, text: target.value }); }; onURLChanged = ({ target }: any) => { this.props.onOptionsChange({ ...this.props.options, url: target.value }); }; - onKeyChanged = ({ target }: any) => { - this.props.onOptionsChange({ ...this.props.options, key: target.value }); - }; onMethodChanged = ({ label, value, target }: any) => { this.props.onOptionsChange({ ...this.props.options, @@ -42,6 +56,15 @@ export class ButtonPanelEditor extends PureComponent { + return new MutableDataFrame({ + fields: [ + { name: 'Name', type: FieldType.string, values: Array.from(this.props.options.params.keys()) }, + { name: 'Value', type: FieldType.string, values: Array.from(this.props.options.params.values()) }, + ], + }); + }; + render() { const { options } = this.props; @@ -49,7 +72,7 @@ export class ButtonPanelEditor extends PureComponent
Settings
- + - + + +
+ + + + + + + {Array.from(options.params.entries()).map(entry => ( + + + + + + ))} + +
diff --git a/src/types.ts b/src/types.ts index e956672..6d355a5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,18 +4,23 @@ import { ButtonVariant } from '@grafana/ui'; export interface ButtonPanelOptions { text: string; url: string; - key: string; + params: Array<[string, string]>; + newParamName: string; + newParamValue: string; + method?: SelectableValue; type?: SelectableValue; variant?: SelectableValue; } export const defaults: ButtonPanelOptions = { - key: 'abc1234', text: 'The default button label', url: 'http://api.example.com/', method: undefined, type: undefined, + params: [], + newParamName: '', + newParamValue: '', variant: undefined, };