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 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { ReducerRegistry } from '../base/redux';
  2. import { RECORDING_SESSION_UPDATED } from './actionTypes';
  3. const DEFAULT_STATE = {
  4. sessionDatas: []
  5. };
  6. /**
  7. * Reduces the Redux actions of the feature features/recording.
  8. */
  9. ReducerRegistry.register('features/recording',
  10. (state = DEFAULT_STATE, action) => {
  11. switch (action.type) {
  12. case RECORDING_SESSION_UPDATED:
  13. return {
  14. ...state,
  15. sessionDatas:
  16. _updateSessionDatas(state.sessionDatas, action.sessionData)
  17. };
  18. default:
  19. return state;
  20. }
  21. });
  22. /**
  23. * Updates the known information on recording sessions.
  24. *
  25. * @param {Array} sessionDatas - The current sessions in the redux store.
  26. * @param {Object} newSessionData - The updated session data.
  27. * @private
  28. * @returns {Array} The session datas with the updated session data added.
  29. */
  30. function _updateSessionDatas(sessionDatas, newSessionData) {
  31. const hasExistingSessionData = sessionDatas.find(
  32. sessionData => sessionData.id === newSessionData.id);
  33. let newSessionDatas;
  34. if (hasExistingSessionData) {
  35. newSessionDatas = sessionDatas.map(sessionData => {
  36. if (sessionData.id === newSessionData.id) {
  37. return {
  38. ...newSessionData
  39. };
  40. }
  41. // Nothing to update for this session data so pass it back in.
  42. return sessionData;
  43. });
  44. } else {
  45. // If the session data is not present, then there is nothing to update
  46. // and instead it needs to be added to the known session datas.
  47. newSessionDatas = [
  48. ...sessionDatas,
  49. { ...newSessionData }
  50. ];
  51. }
  52. return newSessionDatas;
  53. }