insomnia/packages/insomnia-app/app/ui/components/response-timer.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

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';
2017-08-16 00:19:02 +00:00
import autobind from 'autobind-decorator';
import classnames from 'classnames';
2018-06-25 17:42:50 +00:00
import { REQUEST_TIME_TO_SHOW_COUNTER } from '../../common/constants';
2017-08-16 00:19:02 +00:00
@autobind
class ResponseTimer extends PureComponent {
2018-06-25 17:42:50 +00:00
constructor(props) {
super(props);
this._interval = null;
this.state = {
elapsedTime: 0,
};
}
2018-06-25 17:42:50 +00:00
componentWillUnmount() {
clearInterval(this._interval);
}
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;
const elapsedTime = millis / 1000;
2018-06-25 17:42:50 +00:00
this.setState({ elapsedTime });
2017-08-16 00:19:02 +00:00
}
2018-06-25 17:42:50 +00:00
componentWillReceiveProps(nextProps) {
const { loadStartTime } = nextProps;
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();
}
2018-06-25 17:42:50 +00:00
render() {
const { handleCancel, loadStartTime } = this.props;
const { elapsedTime } = this.state;
const show = loadStartTime > 0;
return (
2018-06-25 17:42:50 +00:00
<div
className={classnames('overlay theme--transparent-overlay', {
'overlay--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>
)}
<div className="pad">
2018-06-25 17:42:50 +00:00
<i className="fa fa-refresh fa-spin" />
</div>
<div className="pad">
<button className="btn btn--clicky" onClick={handleCancel}>
Cancel Request
</button>
</div>
</div>
);
}
}
ResponseTimer.propTypes = {
handleCancel: PropTypes.func.isRequired,
loadStartTime: PropTypes.number.isRequired,
};
export default ResponseTimer;