2017-01-13 12:47:21 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-05-04 11:05:55 +02:00
|
|
|
import { h, Component } from 'preact';
|
2018-05-01 19:45:52 +02:00
|
|
|
import { bind } from 'decko';
|
2017-01-13 12:47:21 +01:00
|
|
|
|
|
|
|
class Notification extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
message: props.message,
|
|
|
|
kind: props.kind || 'error'
|
|
|
|
}
|
2017-01-13 13:41:30 +01:00
|
|
|
this.timeout = 0
|
2017-01-13 12:47:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 10:54:22 +02:00
|
|
|
componentWillReceiveProps(newProps, prevState) {
|
|
|
|
if(newProps.message === this.state.message) {
|
|
|
|
return;
|
2017-01-13 12:47:21 +01:00
|
|
|
}
|
2018-05-03 10:54:22 +02:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
message: newProps.message,
|
|
|
|
kind: newProps.kind || 'error'
|
|
|
|
})
|
|
|
|
|
|
|
|
window.clearTimeout(this.timeout)
|
|
|
|
this.timeout = window.setTimeout(this.clearMessage, 5000)
|
2017-01-13 12:47:21 +01:00
|
|
|
}
|
|
|
|
|
2018-05-01 19:45:52 +02:00
|
|
|
@bind
|
|
|
|
clearMessage() {
|
|
|
|
this.setState({ message: '' })
|
|
|
|
}
|
|
|
|
|
|
|
|
render(props, state) {
|
|
|
|
if(state.message === '') {
|
2017-01-13 12:47:21 +01:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={`notification`}>
|
2018-05-01 19:45:52 +02:00
|
|
|
<div class={`notification-${state.kind}`}>
|
|
|
|
{state.message}
|
2017-01-13 12:47:21 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notification
|