mirror of
https://github.com/apache/superset.git
synced 2026-04-10 20:06:13 +00:00
* Fixing PropTypes warning message React recently started warning on the upcoming deprecation of React.PropTypes, the new approach is to use the `prop-types` npm package instead. * Fixing the tests
73 lines
1.5 KiB
JavaScript
73 lines
1.5 KiB
JavaScript
import React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { now, fDuration } from '../modules/dates';
|
|
|
|
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.startTime) {
|
|
const endDttm = this.props.endTime || now();
|
|
if (this.props.startTime < endDttm) {
|
|
const clockStr = fDuration(this.props.startTime, endDttm);
|
|
this.setState({ clockStr });
|
|
}
|
|
if (!this.props.isRunning) {
|
|
this.stopTimer();
|
|
}
|
|
}
|
|
}
|
|
render() {
|
|
if (this.props && this.props.isRunning) {
|
|
this.startTimer();
|
|
}
|
|
let timerSpan = null;
|
|
if (this.props) {
|
|
timerSpan = (
|
|
<span
|
|
className={`inlineBlock m-r-5 label label-${this.props.status}`}
|
|
style={this.props.style}
|
|
>
|
|
{this.state.clockStr}
|
|
</span>
|
|
);
|
|
}
|
|
return timerSpan;
|
|
}
|
|
}
|
|
Timer.propTypes = {
|
|
startTime: PropTypes.number,
|
|
endTime: PropTypes.number,
|
|
isRunning: PropTypes.bool.isRequired,
|
|
status: PropTypes.string,
|
|
style: PropTypes.object,
|
|
};
|
|
|
|
Timer.defaultProps = {
|
|
startTime: null,
|
|
endTime: null,
|
|
status: 'success',
|
|
style: null,
|
|
};
|
|
|
|
export default Timer;
|