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.

reducer.js 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { ReducerRegistry } from '../base/redux';
  2. import {
  3. HIDE_NOTIFICATION,
  4. SET_NOTIFICATIONS_ENABLED,
  5. SHOW_NOTIFICATION
  6. } from './actionTypes';
  7. import { NOTIFICATION_TYPE_PRIORITIES } from './constants';
  8. /**
  9. * The initial state of the feature notifications.
  10. *
  11. * @type {array}
  12. */
  13. const DEFAULT_STATE = {
  14. enabled: true,
  15. notifications: []
  16. };
  17. /**
  18. * Reduces redux actions which affect the display of notifications.
  19. *
  20. * @param {Object} state - The current redux state.
  21. * @param {Object} action - The redux action to reduce.
  22. * @returns {Object} The next redux state which is the result of reducing the
  23. * specified {@code action}.
  24. */
  25. ReducerRegistry.register('features/notifications',
  26. (state = DEFAULT_STATE, action) => {
  27. switch (action.type) {
  28. case HIDE_NOTIFICATION:
  29. return {
  30. ...state,
  31. notifications: state.notifications.filter(
  32. notification => notification.uid !== action.uid)
  33. };
  34. case SET_NOTIFICATIONS_ENABLED:
  35. return {
  36. ...state,
  37. enabled: action.enabled
  38. };
  39. case SHOW_NOTIFICATION:
  40. return {
  41. ...state,
  42. notifications:
  43. _insertNotificationByPriority(state.notifications, {
  44. component: action.component,
  45. props: action.props,
  46. timeout: action.timeout,
  47. uid: action.uid
  48. })
  49. };
  50. }
  51. return state;
  52. });
  53. /**
  54. * Creates a new notification queue with the passed in notification placed at
  55. * the end of other notifications with higher or the same priority.
  56. *
  57. * @param {Object[]} notifications - The queue of notifications to be displayed.
  58. * @param {Object} notification - The new notification to add to the queue.
  59. * @private
  60. * @returns {Object[]} A new array with an updated order of the notification
  61. * queue.
  62. */
  63. function _insertNotificationByPriority(notifications, notification) {
  64. const newNotificationPriority
  65. = NOTIFICATION_TYPE_PRIORITIES[notification.props.appearance] || 0;
  66. // Default to putting the new notification at the end of the queue.
  67. let insertAtLocation = notifications.length;
  68. // Find where to insert the new notification based on priority. Do not
  69. // insert at the front of the queue so that the user can finish acting on
  70. // any notification currently being read.
  71. for (let i = 1; i < notifications.length; i++) {
  72. const queuedNotification = notifications[i];
  73. const queuedNotificationPriority
  74. = NOTIFICATION_TYPE_PRIORITIES[queuedNotification.props.appearance]
  75. || 0;
  76. if (queuedNotificationPriority < newNotificationPriority) {
  77. insertAtLocation = i;
  78. break;
  79. }
  80. }
  81. // Create a copy to avoid mutation and insert the notification.
  82. const copyOfNotifications = notifications.slice();
  83. copyOfNotifications.splice(insertAtLocation, 0, notification);
  84. return copyOfNotifications;
  85. }