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.3KB

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