2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-08-10 01:56:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
|
|
|
import { REQUEST_TIME_TO_SHOW_COUNTER, AUTOBIND_CFG } from '../../common/constants';
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2017-02-28 21:32:23 +00:00
|
|
|
class ResponseTimer extends PureComponent {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-03-06 17:51:58 +00:00
|
|
|
super(props);
|
|
|
|
this._interval = null;
|
|
|
|
this.state = {
|
2018-12-12 17:36:11 +00:00
|
|
|
elapsedTime: 0,
|
2017-03-06 17:51:58 +00:00
|
|
|
};
|
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2017-03-06 17:51:58 +00:00
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleUpdateElapsedTime() {
|
|
|
|
const { loadStartTime } = this.props;
|
2017-08-16 00:19:02 +00:00
|
|
|
const millis = Date.now() - loadStartTime - 200;
|
2017-11-22 13:30:36 +00:00
|
|
|
const elapsedTime = millis / 1000;
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ elapsedTime });
|
2017-08-16 00:19:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
const { loadStartTime } = this.props;
|
2017-08-10 00:13:59 +00:00
|
|
|
|
|
|
|
if (loadStartTime <= 0) {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearInterval(this._interval); // Just to be sure
|
2017-08-16 21:31:14 +00:00
|
|
|
this._interval = setInterval(this._handleUpdateElapsedTime, 100);
|
2017-08-16 00:19:02 +00:00
|
|
|
this._handleUpdateElapsedTime();
|
2017-03-06 17:51:58 +00:00
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { handleCancel, loadStartTime } = this.props;
|
|
|
|
const { elapsedTime } = this.state;
|
2017-08-10 00:13:59 +00:00
|
|
|
|
|
|
|
const show = loadStartTime > 0;
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
if (!show) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-27 21:42:38 +00:00
|
|
|
return (
|
2019-04-18 00:50:03 +00:00
|
|
|
<div className="overlay theme--transparent-overlay" aria-hidden={!show}>
|
2018-06-25 17:42:50 +00:00
|
|
|
{elapsedTime >= REQUEST_TIME_TO_SHOW_COUNTER ? (
|
|
|
|
<h2>{elapsedTime.toFixed(1)} seconds...</h2>
|
|
|
|
) : (
|
|
|
|
<h2>Loading...</h2>
|
|
|
|
)}
|
2017-03-06 17:51:58 +00:00
|
|
|
<div className="pad">
|
2018-06-25 17:42:50 +00:00
|
|
|
<i className="fa fa-refresh fa-spin" />
|
2017-03-06 17:51:58 +00:00
|
|
|
</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,
|
2018-12-12 17:36:11 +00:00
|
|
|
loadStartTime: PropTypes.number.isRequired,
|
2016-11-27 21:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ResponseTimer;
|