All files / javascripts/SqlLab/components Timer.jsx

0% Statements 0/26
0% Branches 0/20
0% Functions 0/1
0% Lines 0/26
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                                                                                                                           
import React from 'react';
import { now, fDuration } from '../../modules/dates';
 
import { STATE_BSSTYLE_MAP } from '../constants.js';
 
class Timer extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      clockStr: '',
    };
  }
  componentWillMount() {
    this.startTimer();
  }
  componentWillUnmount() {
    this.stopTimer();
  }
  startTimer() {
    if (!(this.timer)) {
      this.timer = setInterval(this.stopwatch.bind(this), 30);
    }
  }
  stopTimer() {
    clearInterval(this.timer);
    this.timer = null;
  }
  stopwatch() {
    if (this.props && this.props.query) {
      const endDttm = this.props.query.endDttm || now();
      const clockStr = fDuration(this.props.query.startDttm, endDttm);
      this.setState({ clockStr });
      if (this.props.query.state !== 'running') {
        this.stopTimer();
      }
    }
  }
  render() {
    if (this.props.query && this.props.query.state === 'running') {
      this.startTimer();
    }
    let timerSpan = null;
    if (this.props && this.props.query) {
      const bsStyle = STATE_BSSTYLE_MAP[this.props.query.state];
      timerSpan = (
        <span className={'inlineBlock m-r-5 label label-' + bsStyle}>
          {this.state.clockStr}
        </span>
      );
    }
    return timerSpan;
  }
}
Timer.propTypes = {
  query: React.PropTypes.object,
};
Timer.defaultProps = {
  query: null,
};
 
export default Timer;