您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 3.2KB

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