UNPKG

@bigfishtv/cockpit

Version:

120 lines (89 loc) 4.26 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _dec, _class; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactRedux = require('react-redux'); var _notifications = require('../../actions/notifications'); var _Alert = require('../notification/Alert'); var _Alert2 = _interopRequireDefault(_Alert); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var AUTO_DISMISS = 5000; /** * NotificationsHost handles the display of notifications as determined by the notification store */ var NotificationsHost = (_dec = (0, _reactRedux.connect)(function (_ref) { var notifications = _ref.notifications; return { notifications: notifications }; }), _dec(_class = function (_Component) { _inherits(NotificationsHost, _Component); function NotificationsHost() { _classCallCheck(this, NotificationsHost); var _this = _possibleConstructorReturn(this, _Component.call(this)); _this.state = { ids: [] }; return _this; } NotificationsHost.prototype.componentDidMount = function componentDidMount() { this.checkForNewNotifications(); }; NotificationsHost.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { this.checkForNewNotifications(); }; /** * Animate new notificatons in. Timeout required so CSS transitions work as expected. */ NotificationsHost.prototype.checkForNewNotifications = function checkForNewNotifications() { var _this2 = this; var newNotifications = this.props.notifications.filter(function (notification) { return _this2.isNew(notification); }); if (newNotifications.length) { setTimeout(function () { return _this2.setState({ ids: _this2.props.notifications.map(function (notification) { return notification.id; }) }); }, 10); // automatically dismiss items newNotifications.map(function (notification) { return setTimeout(function () { return _this2.props.dispatch((0, _notifications.dismissNotification)(notification.id)); }, notification.autoDismiss || AUTO_DISMISS); }); } }; /** * determines if notification is newly added * @param {Object} notification * @param {Integer} notification.id * @return {Boolean} */ NotificationsHost.prototype.isNew = function isNew(notification) { return this.state.ids.indexOf(notification.id) === -1; }; NotificationsHost.prototype.render = function render() { var _this3 = this; var index = 0; return _react2.default.createElement( 'div', { className: 'flash-message' }, this.props.notifications.map(function (notification) { return _react2.default.createElement(_Alert2.default, _extends({ key: notification.id, index: _this3.isNew(notification) || notification.dismissed ? null : index++, onClick: function onClick() { return _this3.props.dispatch((0, _notifications.dismissNotification)(notification.id)); } }, notification)); }) ); }; return NotificationsHost; }(_react.Component)) || _class); exports.default = NotificationsHost;