File size: 1,564 Bytes
258fb37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import NotificationSystem from 'react-notification-system';
import * as actions from 'modules/notification/actions';
class Notification extends Component {
componentWillReceiveProps(nextProps) {
const { dispatch } = this.props;
const { notifications } = nextProps;
const notificationIds = notifications.map(notification => notification.uid);
const systemNotifications = this.system.state.notifications || [];
if (notifications.length > 0) {
systemNotifications.forEach(notification => {
if (notificationIds.indexOf(notification.uid) < 0) {
this.system.removeNotification(notification.uid);
}
});
notifications.forEach(notification => {
this.system.addNotification({
...notification,
onRemove: () => {
dispatch(actions.hide(notification.uid));
notification.onRemove && notification.onRemove();
}
});
});
}
if ((this.props.notifications !== notifications) && notifications.length === 0) {
this.system.clearNotifications();
}
}
shouldComponentUpdate(nextProps) {
return this.props !== nextProps;
}
render() {
const { notifications, ...rest} = this.props;
return <NotificationSystem ref={ref => this.system = ref} { ...rest } />
}
}
Notification = connect(
(state) => ({
notifications: state.notifications
})
)(Notification);
export default Notification; |