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.
2016-03-14 08:50:46 -04:00
import ErrorStore from 'stores/error_store.jsx' ;
import * as Utils from 'utils/utils.jsx' ;
2015-06-14 23:53:32 -08:00
2016-03-14 08:50:46 -04:00
import React from 'react' ;
2016-01-26 22:19:51 -03:00
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 ;
}
return true ;
}
2016-01-25 20:14:27 -05:00
componentWillMount ( ) {
2016-04-21 22:37:01 -07:00
if ( ! ErrorStore . getIgnoreEmailPreview ( ) && global . window . mm _config . SendEmailNotifications === 'false' ) {
ErrorStore . storeLastError ( { email _preview : true , message : Utils . localizeMessage ( 'error_bar.preview_mode' , 'Preview Mode: Email notifications have not been configured' ) } ) ;
2016-01-25 20:14:27 -05:00
this . onErrorChange ( ) ;
}
}
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
2016-01-27 10:07:26 -05:00
ErrorStore . clearLastError ( ) ;
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
2016-04-25 05:38:41 -07:00
var errClass = 'error-bar' ;
if ( this . state . type && this . state . type === 'developer' ) {
errClass = 'error-bar-developer' ;
}
2015-09-24 15:06:54 -07:00
return (
2016-04-25 05:38:41 -07:00
< div className = { errClass } >
2015-09-24 15:06:54 -07:00
< span > { this . state . message } < / span >
< a
href = '#'
className = 'error-bar__close'
onClick = { this . handleClose }
>
& times ;
< / a >
< / div >
) ;
2015-06-14 23:53:32 -08:00
}
2015-08-31 10:10:22 -07:00
}
2016-01-25 20:14:27 -05:00
2016-03-14 08:50:46 -04:00
export default ErrorBar ;