all files / src/SqlLab/components/ RunQueryActionButton.jsx

88.46% Statements 23/26
62.5% Branches 5/8
100% Functions 1/1
88% Lines 22/25
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                                                                                                              
import React from 'react';
import PropTypes from 'prop-types';
import Button from '../../components/Button';
import { t } from '../../locales';
 
const propTypes = {
  allowAsync: PropTypes.bool.isRequired,
  dbId: PropTypes.number,
  queryState: PropTypes.string,
  runQuery: PropTypes.func.isRequired,
  selectedText: PropTypes.string,
  stopQuery: PropTypes.func.isRequired,
  sql: PropTypes.string.isRequired,
};
const defaultProps = {
  allowAsync: false,
  sql: '',
};
 
export default function RunQueryActionButton(props) {
  const runBtnText = props.selectedText ? t('Run Selected Query') : t('Run Query');
  const btnStyle = props.selectedText ? 'warning' : 'primary';
  const shouldShowStopBtn = ['running', 'pending'].indexOf(props.queryState) > -1;
 
  const commonBtnProps = {
    bsSize: 'small',
    bsStyle: btnStyle,
    disabled: !(props.dbId),
  };
 
  const syncBtn = (
    <Button
      {...commonBtnProps}
      onClick={() => props.runQuery(false)}
      key="run-btn"
      tooltip={t('Run query synchronously')}
      disabled={!props.sql.trim()}
    >
      <i className="fa fa-refresh" /> {runBtnText}
    </Button>
  );
 
  const asyncBtn = (
    <Button
      {...commonBtnProps}
      onClick={() => props.runQuery(true)}
      key="run-async-btn"
      tooltip={t('Run query asynchronously')}
      disabled={!props.sql.trim()}
    >
      <i className="fa fa-table" /> {runBtnText}
    </Button>
  );
 
  const stopBtn = (
    <Button
      {...commonBtnProps}
      onClick={props.stopQuery}
    >
      <i className="fa fa-stop" /> {t('Stop')}
    </Button>
  );
 
  let button;
  if (shouldShowStopBtn) {
    button = stopBtn;
  } else Iif (props.allowAsync) {
    button = asyncBtn;
  } else {
    button = syncBtn;
  }
  return button;
}
 
RunQueryActionButton.propTypes = propTypes;
RunQueryActionButton.defaultProps = defaultProps;