Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

reducer.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // @flow
  2. import { PersistencyRegistry, ReducerRegistry } from '../base/redux';
  3. import {
  4. STORE_CURRENT_CONFERENCE,
  5. UPDATE_CONFERENCE_DURATION
  6. } from './actionTypes';
  7. import { LIST_SIZE } from './constants';
  8. /**
  9. * The initial state of this feature.
  10. */
  11. const DEFAULT_STATE = {
  12. list: []
  13. };
  14. /**
  15. * The Redux subtree of this feature.
  16. */
  17. const STORE_NAME = 'features/recent-list';
  18. /**
  19. * Registers the redux store subtree of this feature for persistency.
  20. */
  21. PersistencyRegistry.register(STORE_NAME, {
  22. list: true
  23. });
  24. /**
  25. * Reduces the Redux actions of the feature features/recent-list.
  26. */
  27. ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  28. switch (action.type) {
  29. case STORE_CURRENT_CONFERENCE:
  30. return _storeCurrentConference(state, action);
  31. case UPDATE_CONFERENCE_DURATION:
  32. return _updateConferenceDuration(state, action);
  33. default:
  34. return state;
  35. }
  36. });
  37. /**
  38. * Adds a new list entry to the redux store.
  39. *
  40. * @param {Object} state - The redux state.
  41. * @param {Object} action - The redux action.
  42. * @returns {Object}
  43. */
  44. function _storeCurrentConference(state, action) {
  45. const { locationURL } = action;
  46. const conference = locationURL.href;
  47. // If the current conference is already in the list, we remove it to re-add
  48. // it to the top.
  49. const list = state.list.filter(e => e.conference !== conference);
  50. // This is a reverse sorted array (i.e. newer elements at the end).
  51. list.push({
  52. conference,
  53. conferenceDuration: 0, // we don't have this data yet
  54. date: Date.now()
  55. });
  56. // maximising the size
  57. list.splice(0, list.length - LIST_SIZE);
  58. return {
  59. list
  60. };
  61. }
  62. /**
  63. * Updates the conference length when left.
  64. *
  65. * @param {Object} state - The redux state.
  66. * @param {Object} action - The redux action.
  67. * @returns {Object}
  68. */
  69. function _updateConferenceDuration(state, action) {
  70. const { locationURL } = action;
  71. if (locationURL && locationURL.href) {
  72. const list = state.list;
  73. if (list.length > 0) {
  74. const mostRecentURL = list[list.length - 1];
  75. if (mostRecentURL.conference === locationURL.href) {
  76. // The last conference start was stored so we need to update the
  77. // length.
  78. mostRecentURL.conferenceDuration
  79. = Date.now() - mostRecentURL.date;
  80. return {
  81. list
  82. };
  83. }
  84. }
  85. }
  86. return state;
  87. }