All files / javascripts/explore/components URLShortLinkButton.jsx

42.86% Statements 6/14
0% Branches 0/2
0% Functions 0/1
42.86% Lines 6/14
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 631x 1x 1x 1x   1x                                                                                                               1x  
import React, { PropTypes } from 'react';
import { Popover, OverlayTrigger } from 'react-bootstrap';
import CopyToClipboard from './../../components/CopyToClipboard';
import { getShortUrl } from '../../../utils/common';
 
const propTypes = {
  slice: PropTypes.object.isRequired,
};
 
export default class URLShortLinkButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      shortUrl: '',
    };
  }
 
  onShortUrlSuccess(data) {
    this.setState({
      shortUrl: data,
    });
  }
 
  getCopyUrl() {
    const longUrl = window.location.pathname + window.location.search;
    getShortUrl(longUrl, this.onShortUrlSuccess.bind(this));
  }
 
  renderPopover() {
    const emailBody = `Check out this slice: ${this.state.shortUrl}`;
    return (
      <Popover id="shorturl-popover">
        <CopyToClipboard
          text={this.state.shortUrl}
          copyNode={<i className="fa fa-clipboard" title="Copy to clipboard"></i>}
        />
        &nbsp;&nbsp;
        <a href={`mailto:?Subject=Superset%20Slice%20&Body=${emailBody}`}>
          <i className="fa fa-envelope"></i>
        </a>
      </Popover>
    );
  }
 
  render() {
    return (
      <OverlayTrigger
        trigger="click"
        rootClose
        placement="left"
        onEnter={this.getCopyUrl.bind(this)}
        overlay={this.renderPopover()}
      >
        <span className="btn btn-default btn-sm">
          <i className="fa fa-link"></i>&nbsp;
        </span>
      </OverlayTrigger>
    );
  }
}
 
URLShortLinkButton.propTypes = propTypes;