/
ButtonPanel.tsx
171 lines (155 loc) · 5.06 KB
/
ButtonPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { PureComponent } from 'react';
import { Button, IconName } from '@grafana/ui';
import { PanelProps } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
import { ButtonPanelOptions, ButtonPanelState } from 'types';
interface Props extends PanelProps<ButtonPanelOptions> {}
export class ButtonPanel extends PureComponent<Props, ButtonPanelState> {
constructor(props: any) {
super(props);
this.init();
}
init() {
this.state = {
api_call: 'READY',
response: '',
};
}
render() {
const { options } = this.props;
const exeucte = () => {
this.setState({ api_call: 'IN_PROGRESS' });
const url = new URL(interpolateVariables(options.url));
console.log(options.method, ' to ', url, ' with params as ', options.type);
const requestHeaders: HeadersInit = new Headers();
requestHeaders.set('Accept', 'application/json');
let fetchOpts: RequestInit = {
method: options.method, // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
credentials: 'include', // include, *same-origin, omit
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
headers: requestHeaders,
redirect: 'follow', // manual, *follow, error
//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
};
if (options.method === 'POST') {
requestHeaders.set('Content-Type', options.contentType);
if (options.payload) {
fetchOpts.body = interpolateVariables(options.payload);
}
}
if (options.isAuth) {
requestHeaders.set('Authorization', btoa(options.username + ':' + options.password));
}
if (options.params) {
if (options.type === 'header') {
options.params.forEach(e => {
requestHeaders.set(interpolateVariables(e[0]), interpolateVariables(e[1]));
});
} else if (options.type === 'query') {
options.params.forEach(e => {
url.searchParams.append(interpolateVariables(e[0]), interpolateVariables(e[1]));
});
} else {
console.error('Unknown params type', options.type);
}
}
fetch(url.toString(), fetchOpts)
.then(response => {
if (response.type === 'opaque') {
// CORS prevents us from knowing what's up - so be it
this.setState({
api_call: 'READY',
response: 'CORS prevents access to the response',
});
} else if (response.ok) {
this.setState({
api_call: 'SUCCESS',
response: response.statusText,
});
console.log('Requeste successful: ', response);
} else {
console.log('Requeste failed: ', response);
throw new Error(response.status + response.statusText);
}
})
.catch(e => {
this.setState({
api_call: 'ERROR',
response: e.message,
});
console.error('Request error: ', e);
});
};
const apiStateIcon = (): IconName | undefined => {
switch (this.state.api_call) {
case 'IN_PROGRESS':
return 'fa fa-spinner';
case 'SUCCESS':
return 'check';
case 'ERROR':
return 'exclamation-triangle';
case 'READY':
default:
return options.icon;
}
};
const apiStateClassName = () => {
switch (this.state.api_call) {
case 'IN_PROGRESS':
return 'spinning';
default:
return '';
}
};
const getOrientation = () => {
if (!this.props.options.orientation) {
return 'center';
}
switch (this.props.options.orientation) {
case 'left':
return 'left';
case 'right':
return 'right';
case 'center':
default:
return 'center';
}
};
const customStyle = () => {
if (this.props.options.variant === undefined) {
return {
// Resaet Grafana defaults
background: 'none',
border: 'none',
// Set custom styles
backgroundColor: this.props.options.backgroundColor,
color: this.props.options.foregroundColor,
};
} else {
return {};
}
};
const buttonText = () => {
return interpolateVariables(this.props.options.text);
};
const interpolateVariables = (text: string) => {
return getTemplateSrv().replace(text, this.props.data.request?.scopedVars);
};
return (
<div className={getOrientation()}>
<Button
variant={this.props.options.variant}
title={this.state.response}
size="lg"
className={apiStateClassName()}
icon={apiStateIcon()}
onClick={exeucte}
style={customStyle()}
>
{buttonText()}
</Button>
</div>
);
}
}