2017-12-12 19:53:04 +01:00
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
* OverlayBlocker
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2018-01-12 12:51:39 +01:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
|
import cn from 'classnames';
|
2017-12-12 19:53:04 +01:00
|
|
|
|
|
|
|
|
import styles from './styles.scss';
|
|
|
|
|
|
2019-02-27 17:48:06 +01:00
|
|
|
const DELAY = 1000;
|
|
|
|
|
|
2017-12-12 19:53:04 +01:00
|
|
|
class OverlayBlocker extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2019-01-24 18:24:24 +01:00
|
|
|
|
2019-02-27 17:48:06 +01:00
|
|
|
this.state = { elapsed: 0, start: 0 };
|
2017-12-12 19:53:04 +01:00
|
|
|
this.overlayContainer = document.createElement('div');
|
2019-01-24 18:24:24 +01:00
|
|
|
|
2017-12-12 19:53:04 +01:00
|
|
|
document.body.appendChild(this.overlayContainer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 17:48:06 +01:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
|
const { isOpen } = this.props;
|
|
|
|
|
|
|
|
|
|
if (prevProps.isOpen !== this.props.isOpen && isOpen) {
|
|
|
|
|
this.startTimer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevProps.isOpen !== isOpen && !isOpen) {
|
|
|
|
|
this.stopTimer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 19:53:04 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
document.body.removeChild(this.overlayContainer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 17:48:06 +01:00
|
|
|
tick = () => {
|
|
|
|
|
const { elapsed } = this.state;
|
|
|
|
|
|
|
|
|
|
if (elapsed > 30) {
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState(prevState => ({ elapsed: (Math.round(Date.now() - prevState.start) / 1000) }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startTimer = () => {
|
|
|
|
|
this.setState({ start: Date.now() });
|
|
|
|
|
this.timer = setInterval(this.tick, DELAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopTimer = () => {
|
|
|
|
|
this.setState({ elapsed: 0 });
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-12 19:53:04 +01:00
|
|
|
render() {
|
2019-02-27 17:48:06 +01:00
|
|
|
let { title, description, icon } = this.props;
|
|
|
|
|
const { elapsed } = this.state;
|
|
|
|
|
|
|
|
|
|
if (elapsed > 30) {
|
|
|
|
|
title = 'components.OverlayBlocker.title.serverError';
|
|
|
|
|
description = 'components.OverlayBlocker.description.serverError';
|
|
|
|
|
icon = 'fa fa-clock-o';
|
|
|
|
|
}
|
2019-01-24 18:24:24 +01:00
|
|
|
|
2018-01-12 12:51:39 +01:00
|
|
|
const content = this.props.children ? (
|
|
|
|
|
this.props.children
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.container}>
|
2019-02-27 17:48:06 +01:00
|
|
|
<div className={cn(styles.icoContainer, elapsed < 30 && styles.spin)}>
|
2019-01-24 18:24:24 +01:00
|
|
|
<i className={icon} />
|
2018-01-12 12:51:39 +01:00
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<h4>
|
2019-01-24 18:24:24 +01:00
|
|
|
<FormattedMessage id={title} />
|
2018-01-12 12:51:39 +01:00
|
|
|
</h4>
|
|
|
|
|
<p>
|
2019-01-24 18:24:24 +01:00
|
|
|
<FormattedMessage id={description} />
|
2018-01-12 12:51:39 +01:00
|
|
|
</p>
|
2019-02-27 17:48:06 +01:00
|
|
|
{ elapsed < 30 && (
|
|
|
|
|
<div className={styles.buttonContainer}>
|
|
|
|
|
<a className={cn(styles.primary, 'btn')} href="https://strapi.io/documentation/configurations/configurations.html#server" target="_blank">Read the documentation</a>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2018-01-12 12:51:39 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
2017-12-12 19:53:04 +01:00
|
|
|
if (this.props.isOpen) {
|
|
|
|
|
return ReactDOM.createPortal(
|
|
|
|
|
<div className={styles.overlay}>
|
|
|
|
|
<div>
|
2018-01-12 12:51:39 +01:00
|
|
|
{content}
|
2017-12-12 19:53:04 +01:00
|
|
|
</div>
|
|
|
|
|
</div>,
|
|
|
|
|
this.overlayContainer
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OverlayBlocker.defaultProps = {
|
2019-02-27 17:48:06 +01:00
|
|
|
children: null,
|
2019-01-24 18:24:24 +01:00
|
|
|
description: 'components.OverlayBlocker.description',
|
|
|
|
|
icon: 'fa fa-refresh',
|
2017-12-12 19:53:04 +01:00
|
|
|
isOpen: false,
|
2019-01-24 18:24:24 +01:00
|
|
|
title: 'components.OverlayBlocker.title',
|
2017-12-12 19:53:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
OverlayBlocker.propTypes = {
|
|
|
|
|
children: PropTypes.node,
|
2019-01-24 18:24:24 +01:00
|
|
|
description: PropTypes.string,
|
|
|
|
|
icon: PropTypes.string,
|
2017-12-12 19:53:04 +01:00
|
|
|
isOpen: PropTypes.bool,
|
2019-01-24 18:24:24 +01:00
|
|
|
title: PropTypes.string,
|
2017-12-12 19:53:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default OverlayBlocker;
|