2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-06 17:51:58 +00:00
|
|
|
import classnames from 'classnames';
|
2016-11-27 21:42:38 +00:00
|
|
|
import {REQUEST_TIME_TO_SHOW_COUNTER} from '../../common/constants';
|
|
|
|
|
2017-02-28 21:32:23 +00:00
|
|
|
class ResponseTimer extends PureComponent {
|
2017-03-06 17:51:58 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this._interval = null;
|
|
|
|
this.state = {
|
|
|
|
elapsedTime: 0
|
|
|
|
};
|
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-06 17:51:58 +00:00
|
|
|
componentWillUnmount () {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-08-10 00:13:59 +00:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
const {loadStartTime} = nextProps;
|
|
|
|
|
|
|
|
if (loadStartTime <= 0) {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearInterval(this._interval); // Just to be sure
|
2017-03-06 17:51:58 +00:00
|
|
|
this._interval = setInterval(() => {
|
2017-08-10 00:13:59 +00:00
|
|
|
const millis = Date.now() - loadStartTime - 200;
|
|
|
|
const elapsedTime = Math.round(millis / 100) / 10;
|
|
|
|
this.setState({elapsedTime});
|
2016-11-27 21:42:38 +00:00
|
|
|
}, 100);
|
2017-03-06 17:51:58 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2017-03-06 17:51:58 +00:00
|
|
|
render () {
|
2017-08-10 00:13:59 +00:00
|
|
|
const {handleCancel, loadStartTime} = this.props;
|
|
|
|
const {elapsedTime} = this.state;
|
|
|
|
|
|
|
|
const show = loadStartTime > 0;
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
|
|
return (
|
2017-06-16 22:31:22 +00:00
|
|
|
<div className={classnames('overlay theme--overlay', {'overlay--hidden': !show})}>
|
2017-03-06 17:51:58 +00:00
|
|
|
{elapsedTime > REQUEST_TIME_TO_SHOW_COUNTER
|
2017-03-28 22:45:23 +00:00
|
|
|
? <h2>{elapsedTime.toFixed(1)} seconds...</h2>
|
2017-03-06 17:51:58 +00:00
|
|
|
: <h2>Loading...</h2>
|
|
|
|
}
|
|
|
|
<div className="pad">
|
|
|
|
<i className="fa fa-refresh fa-spin"/>
|
|
|
|
</div>
|
2016-11-27 21:42:38 +00:00
|
|
|
<div className="pad">
|
|
|
|
<button className="btn btn--clicky" onClick={handleCancel}>
|
|
|
|
Cancel Request
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-27 21:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResponseTimer.propTypes = {
|
|
|
|
handleCancel: PropTypes.func.isRequired,
|
2017-03-03 20:09:08 +00:00
|
|
|
loadStartTime: PropTypes.number.isRequired
|
2016-11-27 21:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResponseTimer;
|