All files / javascripts/explore/components URLShortLinkButton.jsx

46.15% Statements 6/13
0% Branches 0/2
0% Functions 0/1
46.15% Lines 6/13
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 581x 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() {
    return (
      <Popover id="shorturl-popover">
        <CopyToClipboard
          text={this.state.shortUrl}
          copyNode={<i className="fa fa-clipboard" title="Copy to clipboard"></i>}
        />
      </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;