2021-05-12 06:35:00 +00:00
|
|
|
import React, { DOMAttributes, PureComponent } from 'react';
|
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-05-12 06:35:00 +00:00
|
|
|
interface Props {
|
|
|
|
handleCancel: DOMAttributes<HTMLButtonElement>['onClick'],
|
|
|
|
loadStartTime: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
elapsedTime: number;
|
|
|
|
}
|
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class ResponseTimer extends PureComponent<Props, State> {
|
|
|
|
_interval: NodeJS.Timeout | null = null;
|
|
|
|
|
|
|
|
state: State = {
|
|
|
|
elapsedTime: 0,
|
|
|
|
};
|
2016-11-27 21:42:38 +00:00
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._interval === null) {
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._interval !== null) {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
2017-08-10 00:13:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._interval !== null) {
|
|
|
|
// Just to be sure
|
|
|
|
clearInterval(this._interval);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ResponseTimer;
|