badguardhome/client/src/components/Toasts/Toast.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-09-14 12:37:35 +00:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2018-10-29 03:26:19 +00:00
import { Trans, withNamespaces } from 'react-i18next';
2018-09-14 12:37:35 +00:00
class Toast extends Component {
componentDidMount() {
2019-05-17 15:17:17 +00:00
const timeout = this.props.type === 'success' ? 5000 : 30000;
2018-09-14 13:41:34 +00:00
2018-09-14 12:37:35 +00:00
setTimeout(() => {
this.props.removeToast(this.props.id);
2018-09-14 13:43:27 +00:00
}, timeout);
2018-09-14 12:37:35 +00:00
}
shouldComponentUpdate() {
return false;
}
2019-05-17 15:17:17 +00:00
showMessage(t, type, message) {
if (type === 'notice') {
return <span dangerouslySetInnerHTML={{ __html: t(message) }} />;
}
return <Trans>{message}</Trans>;
}
2018-09-14 12:37:35 +00:00
render() {
2019-05-17 15:17:17 +00:00
const {
type, id, t, message,
} = this.props;
2018-09-14 12:37:35 +00:00
return (
2019-05-17 15:17:17 +00:00
<div className={`toast toast--${type}`}>
2018-09-14 12:37:35 +00:00
<p className="toast__content">
2019-05-17 15:17:17 +00:00
{this.showMessage(t, type, message)}
2018-09-14 12:37:35 +00:00
</p>
2019-05-17 15:17:17 +00:00
<button className="toast__dismiss" onClick={() => this.props.removeToast(id)}>
2018-09-14 12:37:35 +00:00
<svg stroke="#fff" fill="none" width="20" height="20" strokeWidth="2" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="m18 6-12 12"/><path d="m6 6 12 12"/></svg>
</button>
</div>
);
}
}
Toast.propTypes = {
2019-05-17 15:17:17 +00:00
t: PropTypes.func.isRequired,
2018-09-14 12:37:35 +00:00
id: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
removeToast: PropTypes.func.isRequired,
};
2018-10-29 03:26:19 +00:00
export default withNamespaces()(Toast);