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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // @flow
  2. import _ from 'lodash';
  3. import { ReducerRegistry } from '../base/redux';
  4. import {
  5. INIT_SEARCH,
  6. UPDATE_STATS,
  7. INIT_REORDER_STATS,
  8. RESET_SEARCH_CRITERIA
  9. } from './actionTypes';
  10. /**
  11. * The initial state of the feature speaker-stats.
  12. *
  13. * @type {Object}
  14. */
  15. const INITIAL_STATE = {
  16. stats: {},
  17. isOpen: false,
  18. pendingReorder: true,
  19. criteria: null
  20. };
  21. ReducerRegistry.register('features/speaker-stats', (state = _getInitialState(), action) => {
  22. switch (action.type) {
  23. case INIT_SEARCH:
  24. return _updateCriteria(state, action);
  25. case UPDATE_STATS:
  26. return _updateStats(state, action);
  27. case INIT_REORDER_STATS:
  28. return _initReorderStats(state);
  29. case RESET_SEARCH_CRITERIA:
  30. return _updateCriteria(state, { criteria: null });
  31. }
  32. return state;
  33. });
  34. /**
  35. * Gets the initial state of the feature speaker-stats.
  36. *
  37. * @returns {Object}
  38. */
  39. function _getInitialState() {
  40. return INITIAL_STATE;
  41. }
  42. /**
  43. * Reduces a specific Redux action INIT_SEARCH of the feature
  44. * speaker-stats.
  45. *
  46. * @param {Object} state - The Redux state of the feature speaker-stats.
  47. * @param {Action} action - The Redux action INIT_SEARCH to reduce.
  48. * @private
  49. * @returns {Object} The new state after the reduction of the specified action.
  50. */
  51. function _updateCriteria(state, { criteria }) {
  52. return _.assign(
  53. {},
  54. state,
  55. { criteria }
  56. );
  57. }
  58. /**
  59. * Reduces a specific Redux action UPDATE_STATS of the feature
  60. * speaker-stats.
  61. * The speaker stats order is based on the stats object properties.
  62. * When updating without reordering, the new stats object properties are reordered
  63. * as the last in state, otherwise the order would be lost on each update.
  64. * If there was already a pending reorder, the stats object properties already have
  65. * the correct order, so the property order is not changing.
  66. *
  67. * @param {Object} state - The Redux state of the feature speaker-stats.
  68. * @param {Action} action - The Redux action UPDATE_STATS to reduce.
  69. * @private
  70. * @returns {Object} - The new state after the reduction of the specified action.
  71. */
  72. function _updateStats(state, { stats }) {
  73. const finalStats = state.pendingReorder ? stats : state.stats;
  74. if (!state.pendingReorder) {
  75. // Avoid reordering the speaker stats object properties
  76. const finalKeys = Object.keys(stats);
  77. finalKeys.forEach(newStatId => {
  78. finalStats[newStatId] = _.clone(stats[newStatId]);
  79. });
  80. Object.keys(finalStats).forEach(key => {
  81. if (!finalKeys.includes(key)) {
  82. delete finalStats[key];
  83. }
  84. });
  85. }
  86. return _.assign(
  87. {},
  88. state,
  89. {
  90. stats: { ...finalStats },
  91. pendingReorder: false
  92. }
  93. );
  94. }
  95. /**
  96. * Reduces a specific Redux action INIT_REORDER_STATS of the feature
  97. * speaker-stats.
  98. *
  99. * @param {Object} state - The Redux state of the feature speaker-stats.
  100. * @private
  101. * @returns {Object} The new state after the reduction of the specified action.
  102. */
  103. function _initReorderStats(state) {
  104. return _.assign(
  105. {},
  106. state,
  107. { pendingReorder: true }
  108. );
  109. }