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