All files / javascripts/SqlLab/components SaveQuery.jsx

75% Statements 18/24
50% Branches 1/2
66.67% Functions 2/3
75% Lines 18/24
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132  1x 1x 1x   1x               1x     1x       12x   4x         4x 4x 4x 4x 4x                                             6x                                                                                                       2x     6x                                   1x 1x      
/* global notify */
import React from 'react';
import { FormControl, FormGroup, Overlay, Popover, Row, Col } from 'react-bootstrap';
import Button from '../../components/Button';
 
const propTypes = {
  defaultLabel: React.PropTypes.string,
  sql: React.PropTypes.string,
  schema: React.PropTypes.string,
  dbId: React.PropTypes.number,
  animation: React.PropTypes.bool,
  onSave: React.PropTypes.func,
};
const defaultProps = {
  defaultLabel: 'Undefined',
  animation: true,
  onSave: () => {},
};
 
class SaveQuery extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      description: '',
      label: props.defaultLabel,
      showSave: false,
    };
    this.toggleSave = this.toggleSave.bind(this);
    this.onSave = this.onSave.bind(this);
    this.onCancel = this.onCancel.bind(this);
    this.onLabelChange = this.onLabelChange.bind(this);
    this.onDescriptionChange = this.onDescriptionChange.bind(this);
  }
  onSave() {
    const query = {
      label: this.state.label,
      description: this.state.description,
      db_id: this.props.dbId,
      schema: this.props.schema,
      sql: this.props.sql,
    };
    this.props.onSave(query);
    this.setState({ showSave: false });
  }
  onCancel() {
    this.setState({ showSave: false });
  }
  onLabelChange(e) {
    this.setState({ label: e.target.value });
  }
  onDescriptionChange(e) {
    this.setState({ description: e.target.value });
  }
  renderPopover() {
    return (
      <Popover id="embed-code-popover">
        <FormGroup bsSize="small" style={{ width: '350px' }}>
          <Row>
            <Col md={12}>
              <small>
                <label className="control-label" htmlFor="embed-height">
                  Label
                </label>
              </small>
              <FormControl
                type="text"
                placeholder="Label for your query"
                value={this.state.label}
                onChange={this.onLabelChange}
              />
            </Col>
          </Row>
          <br />
          <Row>
            <Col md={12}>
              <small>
                <label className="control-label" htmlFor="embed-height">Description</label>
              </small>
              <FormControl
                componentClass="textarea"
                placeholder="Write a description for your query"
                value={this.state.description}
                onChange={this.onDescriptionChange}
              />
            </Col>
          </Row>
          <br />
          <Row>
            <Col md={12}>
              <Button
                bsStyle="primary"
                onClick={this.onSave}
                className="m-r-3"
              >
                Save
              </Button>
              <Button onClick={this.onCancel} className="cancelQuery">
                Cancel
              </Button>
            </Col>
          </Row>
        </FormGroup>
      </Popover>
    );
  }
  toggleSave(e) {
    this.setState({ target: e.target, showSave: !this.state.showSave });
  }
  render() {
    return (
      <span className="SaveQuery">
        <Overlay
          trigger="click"
          target={this.state.target}
          show={this.state.showSave}
          placement="bottom"
          animation={this.props.animation}
        >
          {this.renderPopover()}
        </Overlay>
        <Button bsSize="small" className="toggleSave" onClick={this.toggleSave}>
          <i className="fa fa-save" /> Save Query
        </Button>
      </span>
    );
  }
}
SaveQuery.propTypes = propTypes;
SaveQuery.defaultProps = defaultProps;
 
export default SaveQuery;