All files / javascripts/components CachedLabel.jsx

0% Statements 0/17
0% Branches 0/6
0% Functions 0/1
0% Lines 0/17
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                                                                                                                                           
import React from 'react';
import PropTypes from 'prop-types';
import { Label } from 'react-bootstrap';
import moment from 'moment';
import TooltipWrapper from './TooltipWrapper';
 
const propTypes = {
  onClick: PropTypes.func,
  cachedTimestamp: PropTypes.string,
  className: PropTypes.string,
};
 
class CacheLabel extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      tooltipContent: '',
      hovered: false,
    };
  }
 
  updateTooltipContent() {
    const cachedText = this.props.cachedTimestamp ? (
      <span>
        Loaded data cached <b>{moment.utc(this.props.cachedTimestamp).fromNow()}</b>
      </span>) :
      'Loaded from cache';
 
    const tooltipContent = (
      <span>
        {cachedText}.
        Click to force-refresh
      </span>
    );
    this.setState({ tooltipContent });
  }
 
  mouseOver() {
    this.updateTooltipContent();
    this.setState({ hovered: true });
  }
 
  mouseOut() {
    this.setState({ hovered: false });
  }
 
  render() {
    const labelStyle = this.state.hovered ? 'primary' : 'default';
    return (
      <TooltipWrapper
        tooltip={this.state.tooltipContent}
        label="cache-desc"
      >
        <Label
          className={this.props.className}
          bsStyle={labelStyle}
          style={{ fontSize: '10px', marginRight: '5px', cursor: 'pointer' }}
          onClick={this.props.onClick}
          onMouseOver={this.mouseOver.bind(this)}
          onMouseOut={this.mouseOut.bind(this)}
        >
          cached <i className="fa fa-refresh" />
        </Label>
      </TooltipWrapper>);
  }
}
CacheLabel.propTypes = propTypes;
 
export default CacheLabel;