2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
var ErrorStore = require('../stores/error_store.jsx');
|
|
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
export default class ErrorBar extends React.Component {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
this.onErrorChange = this.onErrorChange.bind(this);
|
|
|
|
|
this.handleClose = this.handleClose.bind(this);
|
2015-07-08 12:51:43 -07:00
|
|
|
|
2015-09-24 15:06:54 -07:00
|
|
|
this.state = ErrorStore.getLastError();
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-06 16:17:27 -07:00
|
|
|
isValidError(s) {
|
|
|
|
|
if (!s) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!s.message) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (s.connErrorCount && s.connErrorCount >= 1 && s.connErrorCount < 7) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isConnectionError(s) {
|
|
|
|
|
if (!s.connErrorCount || s.connErrorCount === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (s.connErrorCount > 7) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
componentDidMount() {
|
|
|
|
|
ErrorStore.addChangeListener(this.onErrorChange);
|
|
|
|
|
}
|
2015-09-24 15:06:54 -07:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
componentWillUnmount() {
|
|
|
|
|
ErrorStore.removeChangeListener(this.onErrorChange);
|
|
|
|
|
}
|
2015-09-24 15:06:54 -07:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
onErrorChange() {
|
2015-09-24 15:06:54 -07:00
|
|
|
var newState = ErrorStore.getLastError();
|
|
|
|
|
|
|
|
|
|
if (newState) {
|
2015-06-14 23:53:32 -08:00
|
|
|
this.setState(newState);
|
2015-09-24 15:06:54 -07:00
|
|
|
} else {
|
|
|
|
|
this.setState({message: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
2015-09-24 15:06:54 -07:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
handleClose(e) {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2015-07-08 12:51:43 -07:00
|
|
|
|
2015-10-06 16:17:27 -07:00
|
|
|
this.setState({message: null});
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|
2015-09-24 15:06:54 -07:00
|
|
|
|
2015-08-31 10:10:22 -07:00
|
|
|
render() {
|
2015-10-06 16:17:27 -07:00
|
|
|
if (!this.isValidError(this.state)) {
|
2015-09-24 15:06:54 -07:00
|
|
|
return <div/>;
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-31 10:10:22 -07:00
|
|
|
|
2015-09-24 15:06:54 -07:00
|
|
|
return (
|
|
|
|
|
<div className='error-bar'>
|
|
|
|
|
<span>{this.state.message}</span>
|
|
|
|
|
<a
|
|
|
|
|
href='#'
|
|
|
|
|
className='error-bar__close'
|
|
|
|
|
onClick={this.handleClose}
|
|
|
|
|
>
|
|
|
|
|
×
|
|
|
|
|
</a>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-31 10:10:22 -07:00
|
|
|
}
|