2018-04-27 15:42:35 +02:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
2018-11-21 14:45:57 +01:00
|
|
|
import { QueryTransaction, HistoryItem, QueryHint } from 'app/types/explore';
|
2018-10-24 11:08:15 +02:00
|
|
|
|
2018-10-09 19:46:31 +02:00
|
|
|
import DefaultQueryField from './QueryField';
|
|
|
|
import QueryTransactionStatus from './QueryTransactionStatus';
|
2018-11-21 14:45:57 +01:00
|
|
|
import { DataSource, DataQuery } from 'app/types';
|
2018-04-27 15:42:35 +02:00
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint {
|
2018-10-24 11:08:15 +02:00
|
|
|
const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0);
|
|
|
|
if (transaction) {
|
|
|
|
return transaction.hints[0];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
interface QueryRowEventHandlers {
|
|
|
|
onAddQueryRow: (index: number) => void;
|
2018-11-21 14:45:57 +01:00
|
|
|
onChangeQuery: (value: DataQuery, index: number, override?: boolean) => void;
|
2018-10-24 14:55:56 +02:00
|
|
|
onClickHintFix: (action: object, index?: number) => void;
|
|
|
|
onExecuteQuery: () => void;
|
|
|
|
onRemoveQueryRow: (index: number) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface QueryRowCommonProps {
|
|
|
|
className?: string;
|
2018-10-30 14:38:34 +01:00
|
|
|
datasource: DataSource;
|
2018-10-24 14:55:56 +02:00
|
|
|
history: HistoryItem[];
|
|
|
|
transactions: QueryTransaction[];
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryRowProps = QueryRowCommonProps &
|
|
|
|
QueryRowEventHandlers & {
|
|
|
|
index: number;
|
2018-11-21 16:28:30 +01:00
|
|
|
initialQuery: DataQuery;
|
2018-10-24 14:55:56 +02:00
|
|
|
};
|
|
|
|
|
2018-10-30 14:38:34 +01:00
|
|
|
class QueryRow extends PureComponent<QueryRowProps> {
|
2018-11-21 14:45:57 +01:00
|
|
|
onChangeQuery = (value: DataQuery, override?: boolean) => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onChangeQuery } = this.props;
|
|
|
|
if (onChangeQuery) {
|
2018-08-05 23:07:05 +02:00
|
|
|
onChangeQuery(value, index, override);
|
2018-04-27 15:42:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickAddButton = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onAddQueryRow } = this.props;
|
|
|
|
if (onAddQueryRow) {
|
|
|
|
onAddQueryRow(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickClearButton = () => {
|
2018-11-21 14:45:57 +01:00
|
|
|
this.onChangeQuery(null, true);
|
2018-08-04 11:58:54 +02:00
|
|
|
};
|
|
|
|
|
2018-08-08 16:50:30 +02:00
|
|
|
onClickHintFix = action => {
|
|
|
|
const { index, onClickHintFix } = this.props;
|
|
|
|
if (onClickHintFix) {
|
|
|
|
onClickHintFix(action, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onClickRemoveButton = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { index, onRemoveQueryRow } = this.props;
|
|
|
|
if (onRemoveQueryRow) {
|
|
|
|
onRemoveQueryRow(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-04 11:58:54 +02:00
|
|
|
onPressEnter = () => {
|
2018-04-27 15:42:35 +02:00
|
|
|
const { onExecuteQuery } = this.props;
|
|
|
|
if (onExecuteQuery) {
|
|
|
|
onExecuteQuery();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2018-11-21 16:28:30 +01:00
|
|
|
const { datasource, history, initialQuery, transactions } = this.props;
|
2018-10-24 14:55:56 +02:00
|
|
|
const transactionWithError = transactions.find(t => t.error !== undefined);
|
2018-10-24 11:08:15 +02:00
|
|
|
const hint = getFirstHintFromTransactions(transactions);
|
2018-10-22 17:51:42 +02:00
|
|
|
const queryError = transactionWithError ? transactionWithError.error : null;
|
2018-10-30 14:38:34 +01:00
|
|
|
const QueryField = datasource.pluginExports.ExploreQueryField || DefaultQueryField;
|
2018-04-27 15:42:35 +02:00
|
|
|
return (
|
|
|
|
<div className="query-row">
|
2018-10-22 17:51:42 +02:00
|
|
|
<div className="query-row-status">
|
2018-10-09 19:46:31 +02:00
|
|
|
<QueryTransactionStatus transactions={transactions} />
|
2018-10-22 17:51:42 +02:00
|
|
|
</div>
|
2018-08-05 23:07:05 +02:00
|
|
|
<div className="query-row-field">
|
2018-04-27 18:21:20 +02:00
|
|
|
<QueryField
|
2018-10-25 12:24:24 +02:00
|
|
|
datasource={datasource}
|
2018-08-08 16:50:30 +02:00
|
|
|
error={queryError}
|
2018-10-24 11:08:15 +02:00
|
|
|
hint={hint}
|
2018-11-21 16:28:30 +01:00
|
|
|
initialQuery={initialQuery}
|
2018-08-02 16:43:33 +02:00
|
|
|
history={history}
|
2018-08-08 16:50:30 +02:00
|
|
|
onClickHintFix={this.onClickHintFix}
|
2018-08-04 11:58:54 +02:00
|
|
|
onPressEnter={this.onPressEnter}
|
|
|
|
onQueryChange={this.onChangeQuery}
|
2018-04-27 18:21:20 +02:00
|
|
|
/>
|
2018-04-27 15:42:35 +02:00
|
|
|
</div>
|
2018-08-05 23:07:05 +02:00
|
|
|
<div className="query-row-tools">
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
|
|
|
|
<i className="fa fa-times" />
|
|
|
|
</button>
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
|
|
|
|
<i className="fa fa-plus" />
|
|
|
|
</button>
|
|
|
|
<button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
|
|
|
|
<i className="fa fa-minus" />
|
|
|
|
</button>
|
|
|
|
</div>
|
2018-04-27 15:42:35 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 14:55:56 +02:00
|
|
|
type QueryRowsProps = QueryRowCommonProps &
|
|
|
|
QueryRowEventHandlers & {
|
2018-11-21 16:28:30 +01:00
|
|
|
initialQueries: DataQuery[];
|
2018-10-24 14:55:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class QueryRows extends PureComponent<QueryRowsProps> {
|
2018-04-27 15:42:35 +02:00
|
|
|
render() {
|
2018-11-21 16:28:30 +01:00
|
|
|
const { className = '', initialQueries, transactions, ...handlers } = this.props;
|
2018-04-27 15:42:35 +02:00
|
|
|
return (
|
2018-04-27 18:21:20 +02:00
|
|
|
<div className={className}>
|
2018-11-21 16:28:30 +01:00
|
|
|
{initialQueries.map((query, index) => (
|
2018-08-08 16:50:30 +02:00
|
|
|
<QueryRow
|
2018-11-21 16:28:30 +01:00
|
|
|
key={query.key}
|
2018-08-08 16:50:30 +02:00
|
|
|
index={index}
|
2018-11-21 16:28:30 +01:00
|
|
|
initialQuery={query}
|
2018-10-22 17:51:42 +02:00
|
|
|
transactions={transactions.filter(t => t.rowIndex === index)}
|
2018-08-08 16:50:30 +02:00
|
|
|
{...handlers}
|
|
|
|
/>
|
2018-08-03 10:20:13 +02:00
|
|
|
))}
|
2018-04-27 18:21:20 +02:00
|
|
|
</div>
|
2018-04-27 15:42:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|