All files / javascripts/SqlLab/components CopyQueryTabUrl.jsx

20.83% Statements 5/24
0% Branches 0/12
0% Functions 0/1
26.32% Lines 5/19
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 511x 1x 1x   1x                                                                                         1x  
import React from 'react';
import CopyToClipboard from '../../components/CopyToClipboard';
import { getShortUrl } from '../../../utils/common';
 
const propTypes = {
  queryEditor: React.PropTypes.object.isRequired,
};
 
export default class CopyQueryTabUrl extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      shortUrl: '',
    };
  }
 
  componentWillMount() {
    const qe = this.props.queryEditor;
    const params = [];
    if (qe.dbId) params.push('dbid=' + qe.dbId);
    if (qe.title) params.push('title=' + encodeURIComponent(qe.title));
    if (qe.schema) params.push('schema=' + encodeURIComponent(qe.schema));
    if (qe.autorun) params.push('autorun=' + qe.autorun);
    if (qe.sql) params.push('sql=' + encodeURIComponent(qe.sql));
 
    const queryString = params.join('&');
    const queryLink = window.location.pathname + '?' + queryString;
    getShortUrl(queryLink, this.onShortUrlSuccess.bind(this));
  }
 
  onShortUrlSuccess(data) {
    this.setState({
      shortUrl: data,
    });
  }
 
  render() {
    return (
      <CopyToClipboard
        inMenu
        text={this.state.shortUrl}
        copyNode={<span>share query</span>}
        tooltipText="copy URL to clipboard"
        shouldShowText={false}
      />
    );
  }
}
 
CopyQueryTabUrl.propTypes = propTypes;