-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
ButtonPanel.tsx
196 lines (173 loc) · 5.38 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React, { PureComponent } from 'react';
import { Button, IconName, ButtonVariant } 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.state = {
api_call: 'READY',
response: '',
};
}
resetState() {
this.setState({
api_call: 'READY',
response: '',
});
}
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 this.props.options.icon;
}
}
apiStateClassName() {
switch (this.state.api_call) {
case 'IN_PROGRESS':
return 'spinning';
default:
return '';
}
}
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';
}
}
customStyle() {
if (this.props.options.variant === 'custom') {
return {
// Resaet Grafana defaults
background: 'none',
border: 'none',
// Set custom styles
backgroundColor: this.props.options.backgroundColor,
color: this.props.options.foregroundColor,
};
} else {
return {};
}
}
variant(): ButtonVariant | undefined {
if (this.props.options.variant === 'custom') {
return undefined;
} else {
return this.props.options.variant as ButtonVariant;
}
}
buttonText() {
return this.interpolateVariables(this.props.options.text);
}
interpolateVariables(text: string) {
return getTemplateSrv().replace(text, this.props.data.request?.scopedVars);
}
prepareFetchOpts(url: URL): RequestInit {
const { options } = this.props;
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
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 = this.interpolateVariables(options.payload);
}
}
if (options.isAuth) {
fetchOpts.credentials = 'include';
requestHeaders.set('Authorization', 'Basic ' + btoa(options.username + ':' + options.password));
}
if (options.params) {
if (options.type === 'header') {
options.params.forEach((e) => {
requestHeaders.set(this.interpolateVariables(e[0]), this.interpolateVariables(e[1]));
});
} else if (options.type === 'query') {
options.params.forEach((e) => {
url.searchParams.append(this.interpolateVariables(e[0]), this.interpolateVariables(e[1]));
});
} else {
console.error('Unknown params type', options.type);
}
}
return fetchOpts;
}
render() {
const { options } = this.props;
const exeucte = () => {
this.setState({ api_call: 'IN_PROGRESS' });
const url = new URL(this.interpolateVariables(options.url));
console.log(options.method, ' to ', url, ' with params as ', options.type);
let fetchOpts = this.prepareFetchOpts(url);
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);
})
.finally(() => {
setTimeout(this.resetState.bind(this), 2500);
});
};
return (
<div className={this.getOrientation()}>
<Button
variant={this.variant()}
title={this.state.response}
size="lg"
className={this.apiStateClassName()}
icon={this.apiStateIcon()}
onClick={exeucte}
style={this.customStyle()}
>
{this.buttonText()}
</Button>
</div>
);
}
}