Skip to content

Commit 948eac7

Browse files
authored
Disable auto query; Add query button (#35)
1 parent 19307ed commit 948eac7

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/components/QueryEditor.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { ChangeEvent, useRef, useState } from "react";
22
import {
3-
ActionMeta,
43
Button,
54
CodeEditor,
65
Field,
@@ -10,7 +9,8 @@ import {
109
Select,
1110
ControlledCollapse,
1211
InlineSwitch,
13-
RadioButtonGroup
12+
RadioButtonGroup,
13+
Stack
1414
} from "@grafana/ui";
1515
import { QueryEditorProps, SelectableValue } from "@grafana/data";
1616
import { DataSource } from "../datasource";
@@ -40,7 +40,7 @@ const languageOptions: Array<SelectableValue<string>> = [
4040
];
4141

4242

43-
export function QueryEditor({ query, onChange, onRunQuery }: Props) {
43+
export function QueryEditor({ query, onChange, onRunQuery, data }: Props) {
4444

4545
const codeEditorRef = useRef<monacoType.editor.IStandaloneCodeEditor | null>(null);
4646
const [queryTextError, setQueryTextError] = useState<string | null>(null);
@@ -57,20 +57,14 @@ export function QueryEditor({ query, onChange, onRunQuery }: Props) {
5757
// let the same query text as it is
5858
onChange({ ...query, queryText: queryText, ...(collection ? { collection } : {}) });
5959
setQueryTextError(error);
60-
if (!error) {
61-
onRunQuery();
62-
}
6360
} else {
6461
onChange({ ...query, queryText: queryText });
6562
const error = validateJsonQueryText(queryText);
66-
if (!error) {
67-
onRunQuery();
68-
}
6963
setQueryTextError(error);
7064
}
7165
};
7266

73-
const onQueryLanguageChange = (value: SelectableValue<string>, actionMeta: ActionMeta) => {
67+
const onQueryLanguageChange = (value: SelectableValue<string>) => {
7468
onChange({ ...query, queryLanguage: value.value });
7569
};
7670

@@ -185,12 +179,15 @@ export function QueryEditor({ query, onChange, onRunQuery }: Props) {
185179
</InlineField>
186180
</InlineFieldRow>
187181
</ControlledCollapse>
188-
<Field label="Query Text" description={`Enter the Mongo Aggregation Pipeline (${query.queryLanguage})`}
182+
<Field label="Query Text" description={`Enter the Mongo Aggregation Pipeline (${query.queryLanguage === QueryLanguage.JSON ? "JSON" : "JavaScript"})`}
189183
error={queryTextError} invalid={queryTextError != null}>
190184
<CodeEditor onEditorDidMount={onCodeEditorDidMount} width="100%" height={300} language={query.queryLanguage === QueryLanguage.JAVASCRIPT || query.queryLanguage === QueryLanguage.JAVASCRIPT_SHADOW ? "javascript" : "json"}
191185
onBlur={onQueryTextChange} value={query.queryText || ""} showMiniMap={false} showLineNumbers={true} monacoOptions={{ fontSize: 14 }} />
192186
</Field>
193-
<Button onClick={onFormatQueryText}>Format</Button>
187+
<Stack direction="row" wrap alignItems="flex-start" justifyContent="start" gap={1}>
188+
<Button onClick={onFormatQueryText} variant="secondary">Format</Button>
189+
<Button onClick={onRunQuery} variant="primary" disabled={data?.state === "Loading"} icon={data?.state === "Loading" ? "spinner" : undefined}>Query</Button>
190+
</Stack>
194191
</>
195192
);
196193
};

tests/queryEditor.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test.beforeAll(async ({ createDataSource, readProvisionedDataSource }) => {
7272
await client.close();
7373
});
7474

75-
test("data query should return correct temperature data", async ({ panelEditPage, readProvisionedDataSource, selectors, page, createDataSource }) => {
75+
test("data query should return correct temperature data", async ({ panelEditPage, readProvisionedDataSource, selectors }) => {
7676
const query = `
7777
[
7878
{
@@ -123,7 +123,7 @@ test("data query should return correct temperature data", async ({ panelEditPage
123123
await expect(panelEditPage.panel.data).toContainText(["2", "1", "3", "1"]);
124124
});
125125

126-
test("data query should return correct temperature data with Javascript query", async ({ panelEditPage, readProvisionedDataSource, selectors, page, createDataSource }) => {
126+
test("data query should return correct temperature data with Javascript query", async ({ panelEditPage, readProvisionedDataSource, selectors, page }) => {
127127
const query = `
128128
db.test_temperatureData.aggregate([
129129
{
@@ -177,7 +177,7 @@ test("data query should return correct temperature data with Javascript query",
177177
await expect(panelEditPage.panel.data).toContainText(["2", "1", "3", "1"]);
178178
});
179179

180-
test("data query should return correct temperature data with javascript function", async ({ panelEditPage, readProvisionedDataSource, selectors, page, createDataSource, dashboardPage }) => {
180+
test("data query should return correct temperature data with javascript function", async ({ panelEditPage, readProvisionedDataSource, selectors, page }) => {
181181
const query = `
182182
function query() {
183183
return [

0 commit comments

Comments
 (0)