You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationsContainer.web.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { FlagGroup } from '@atlaskit/flag';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { hideNotification } from '../actions';
  5. /**
  6. * Implements a React {@link Component} which displays notifications and handles
  7. * automatic dismissmal after a notification is shown for a defined timeout
  8. * period.
  9. *
  10. * @extends {Component}
  11. */
  12. class NotificationsContainer extends Component {
  13. /**
  14. * {@code NotificationsContainer} component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. /**
  20. * The notifications to be displayed, with the first index being the
  21. * notification at the top and the rest shown below it in order.
  22. */
  23. _notifications: React.PropTypes.array,
  24. /**
  25. * Invoked to update the redux store in order to remove notifications.
  26. */
  27. dispatch: React.PropTypes.func
  28. };
  29. /**
  30. * Initializes a new {@code NotificationsContainer} instance.
  31. *
  32. * @param {Object} props - The read-only React Component props with which
  33. * the new instance is to be initialized.
  34. */
  35. constructor(props) {
  36. super(props);
  37. /**
  38. * The timeout set for automatically dismissing a displayed
  39. * notification. This value is set on the instance and not state to
  40. * avoid additional re-renders.
  41. *
  42. * @type {number|null}
  43. */
  44. this._notificationDismissTimeout = null;
  45. // Bind event handlers so they are only bound once for every instance.
  46. this._onDismissed = this._onDismissed.bind(this);
  47. }
  48. /**
  49. * Sets a timeout if the currently displayed notification has changed.
  50. *
  51. * @inheritdoc
  52. * returns {void}
  53. */
  54. componentDidUpdate() {
  55. const { _notifications } = this.props;
  56. if (_notifications.length && !this._notificationDismissTimeout) {
  57. const notification = _notifications[0];
  58. const { timeout, uid } = notification;
  59. this._notificationDismissTimeout = setTimeout(() => {
  60. // Perform a no-op if a timeout is not specified.
  61. if (Number.isInteger(timeout)) {
  62. this._onDismissed(uid);
  63. }
  64. }, timeout);
  65. }
  66. }
  67. /**
  68. * Clear any dismissal timeout that is still active.
  69. *
  70. * @inheritdoc
  71. * returns {void}
  72. */
  73. componentWillUnmount() {
  74. clearTimeout(this._notificationDismissTimeout);
  75. }
  76. /**
  77. * Implements React's {@link Component#render()}.
  78. *
  79. * @inheritdoc
  80. * @returns {ReactElement}
  81. */
  82. render() {
  83. const { _notifications } = this.props;
  84. const flags = _notifications.map(notification => {
  85. const Notification = notification.component;
  86. const { props, uid } = notification;
  87. // The id attribute is necessary as {@code FlagGroup} looks for
  88. // either id or key to set a key on notifications, but accessing
  89. // props.key will cause React to print an error.
  90. return (
  91. <Notification
  92. { ...props }
  93. id = { uid }
  94. key = { uid }
  95. uid = { uid } />
  96. );
  97. });
  98. return (
  99. <FlagGroup onDismissed = { this._onDismissed }>
  100. { flags }
  101. </FlagGroup>
  102. );
  103. }
  104. /**
  105. * Emits an action to remove the notification from the redux store so it
  106. * stops displaying.
  107. *
  108. * @param {number} flagUid - The id of the notification to be removed.
  109. * @private
  110. * @returns {void}
  111. */
  112. _onDismissed(flagUid) {
  113. clearTimeout(this._notificationDismissTimeout);
  114. this._notificationDismissTimeout = null;
  115. this.props.dispatch(hideNotification(flagUid));
  116. }
  117. }
  118. /**
  119. * Maps (parts of) the Redux state to the associated NotificationsContainer's
  120. * props.
  121. *
  122. * @param {Object} state - The Redux state.
  123. * @private
  124. * @returns {{
  125. * _notifications: React.PropTypes.array
  126. * }}
  127. */
  128. function _mapStateToProps(state) {
  129. return {
  130. _notifications: state['features/notifications']
  131. };
  132. }
  133. export default connect(_mapStateToProps)(NotificationsContainer);