Skip to content

Commit

Permalink
Modify query buttons into header buttons (#10)
Browse files Browse the repository at this point in the history
* Release 0.1.4
  • Loading branch information
idastambuk authored Feb 7, 2023
1 parent 7344230 commit 105fb9c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ cypress/screenshots/actual
cypress/videos/
dist/
compiled/
yarn-error.log
yarn-error.log
.DS_Store
.idea/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## v0.1.4

- Modify query buttons into header buttons

## v0.0.1

- Add `DatasourceWithAsyncBackend` class to handle async query flow on the frontend
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grafana/async-query-data",
"version": "0.0.4",
"version": "0.1.4",
"description": "Async query support for Grafana",
"main": "dist/index.js",
"scripts": {
Expand Down
22 changes: 11 additions & 11 deletions src/RunQueryButtons.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ const getDefaultProps = (overrides?: Partial<RunQueryButtonsProps<DataQuery>>) =
return {
onRunQuery: jest.fn(),
onCancelQuery: jest.fn(),
isQueryValid: jest.fn(),
enableRun: true,
query: { refId: 'refId' },
...overrides,
};
};

describe('RunQueryButtons', () => {
it('disable the run button if the query is invalid', () => {
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(false) });
it('disable the run button if the if the enableRun button is false', () => {
const props = getDefaultProps({ enableRun: false});
render(<RunQueryButtons {...props} />);
const runButton = screen.getByRole('button', { name: 'Run' });
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeDisabled();
});

it('run button should be enabled if the query is valid', () => {
const props = getDefaultProps({ isQueryValid: jest.fn().mockReturnValue(true) });
it('run button should be enabled if the enableRun button is true', () => {
const props = getDefaultProps({ enableRun: true});
render(<RunQueryButtons {...props} />);
const runButton = screen.getByRole('button', { name: 'Run' });
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).not.toBeDisabled();
});

it('only renders the `Run` button if onCancelQuery is undefined', () => {
const props = getDefaultProps({ onCancelQuery: undefined });
render(<RunQueryButtons {...props} />);
const runButton = screen.getByRole('button', { name: 'Run' });
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeInTheDocument();
const stopButton = screen.queryByRole('button', { name: 'Stop' });
const stopButton = screen.queryByRole('button', { name: 'Stop query' });
expect(stopButton).not.toBeInTheDocument();
});

it('renders the `Run` and `Stop` buttons if onCancelQuery defined', () => {
const props = getDefaultProps();
render(<RunQueryButtons {...props} />);
const runButton = screen.getByRole('button', { name: 'Run' });
const runButton = screen.getByRole('button', { name: 'Run query' });
expect(runButton).toBeInTheDocument();
const stopButton = screen.queryByRole('button', { name: 'Stop' });
const stopButton = screen.queryByRole('button', { name: 'Stop query' });
expect(stopButton).toBeInTheDocument();
});
});
48 changes: 23 additions & 25 deletions src/RunQueryButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useEffect, useState } from 'react';
import { HorizontalGroup, Button, Spinner } from '@grafana/ui';
import { Button } from '@grafana/ui';
import { DataQuery, LoadingState } from '@grafana/data';

export interface RunQueryButtonsProps<TQuery extends DataQuery> {
enableRun?: boolean;
onRunQuery: () => void;
onCancelQuery?: (query: TQuery) => void;
isQueryValid: (query: TQuery) => boolean;
query: TQuery;
state?: LoadingState;
}
Expand Down Expand Up @@ -39,30 +39,28 @@ export const RunQueryButtons = <TQuery extends DataQuery>(props: RunQueryButtons
}
: undefined;

const isQueryValid = props.isQueryValid(props.query);

return (
<HorizontalGroup>
<Button icon={running ? undefined : 'play'} disabled={running || !isQueryValid} onClick={onRunQuery}>
{running && !stopping ? (
<HorizontalGroup>
<Spinner /> Running
</HorizontalGroup>
) : (
'Run'
)}
</Button>
{onCancelQuery && (
<Button icon={running ? undefined : 'square-shape'} disabled={!running || stopping} onClick={onCancelQuery}>
{stopping ? (
<HorizontalGroup>
<Spinner /> Stopping
</HorizontalGroup>
) : (
'Stop'
)}
<>
<Button
variant={props.enableRun ? 'primary' : 'secondary'}
size="sm"
onClick={onRunQuery}
icon={running && !stopping ? 'fa fa-spinner' : undefined}
disabled={state === LoadingState.Loading || !props.enableRun}
>
Run query
</Button>
)}
</HorizontalGroup>
{onCancelQuery &&
<Button
variant={running && !stopping ? 'primary' : 'secondary'}
size="sm"
disabled={!running || stopping}
icon={stopping ? 'fa fa-spinner' : undefined}
onClick={onCancelQuery}
>
Stop query
</Button>
}
</>
);
};

0 comments on commit 105fb9c

Please sign in to comment.