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.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { CONFERENCE_JOINED, CONFERENCE_LEFT, SET_PASSWORD } from '../base/conference/actionTypes';
  2. import { IParticipant } from '../base/participants/types';
  3. import ReducerRegistry from '../base/redux/ReducerRegistry';
  4. import {
  5. KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED,
  6. KNOCKING_PARTICIPANT_LEFT,
  7. REMOVE_LOBBY_CHAT_WITH_MODERATOR,
  8. SET_KNOCKING_STATE,
  9. SET_LOBBY_MODE_ENABLED,
  10. SET_LOBBY_PARTICIPANT_CHAT_STATE,
  11. SET_LOBBY_VISIBILITY,
  12. SET_PASSWORD_JOIN_FAILED
  13. } from './actionTypes';
  14. const DEFAULT_STATE = {
  15. knocking: false,
  16. knockingParticipants: [],
  17. lobbyEnabled: false,
  18. lobbyVisible: false,
  19. passwordJoinFailed: false
  20. };
  21. interface IKnockingParticipant extends IParticipant {
  22. chattingWithModerator?: string;
  23. }
  24. export interface ILobbyState {
  25. knocking: boolean;
  26. knockingParticipants: IKnockingParticipant[];
  27. lobbyEnabled: boolean;
  28. lobbyVisible: boolean;
  29. passwordJoinFailed: boolean;
  30. }
  31. /**
  32. * Reduces redux actions which affect the display of notifications.
  33. *
  34. * @param {Object} state - The current redux state.
  35. * @param {Object} action - The redux action to reduce.
  36. * @returns {Object} The next redux state which is the result of reducing the
  37. * specified {@code action}.
  38. */
  39. ReducerRegistry.register<ILobbyState>('features/lobby', (state = DEFAULT_STATE, action): ILobbyState => {
  40. switch (action.type) {
  41. case CONFERENCE_JOINED:
  42. case CONFERENCE_LEFT:
  43. return {
  44. ...state,
  45. knocking: false,
  46. passwordJoinFailed: false
  47. };
  48. case KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED:
  49. return _knockingParticipantArrivedOrUpdated(action.participant, state);
  50. case KNOCKING_PARTICIPANT_LEFT:
  51. return {
  52. ...state,
  53. knockingParticipants: state.knockingParticipants.filter(p => p.id !== action.id)
  54. };
  55. case SET_KNOCKING_STATE:
  56. return {
  57. ...state,
  58. knocking: action.knocking,
  59. passwordJoinFailed: false
  60. };
  61. case SET_LOBBY_MODE_ENABLED:
  62. return {
  63. ...state,
  64. lobbyEnabled: action.enabled
  65. };
  66. case SET_LOBBY_VISIBILITY:
  67. return {
  68. ...state,
  69. lobbyVisible: action.visible
  70. };
  71. case SET_PASSWORD:
  72. return {
  73. ...state,
  74. passwordJoinFailed: false
  75. };
  76. case SET_PASSWORD_JOIN_FAILED:
  77. return {
  78. ...state,
  79. passwordJoinFailed: action.failed
  80. };
  81. case SET_LOBBY_PARTICIPANT_CHAT_STATE:
  82. return {
  83. ...state,
  84. knockingParticipants: state.knockingParticipants.map(participant => {
  85. if (participant.id === action.participant.id) {
  86. return {
  87. ...participant,
  88. chattingWithModerator: action.moderator.id
  89. };
  90. }
  91. return participant;
  92. })
  93. };
  94. case REMOVE_LOBBY_CHAT_WITH_MODERATOR:
  95. return {
  96. ...state,
  97. knockingParticipants: state.knockingParticipants.map(participant => {
  98. if (participant.chattingWithModerator === action.moderatorId) {
  99. return {
  100. ...participant,
  101. chattingWithModerator: undefined
  102. };
  103. }
  104. return participant;
  105. })
  106. };
  107. }
  108. return state;
  109. });
  110. /**
  111. * Stores or updates a knocking participant.
  112. *
  113. * @param {Object} participant - The arrived or updated knocking participant.
  114. * @param {Object} state - The current Redux state of the feature.
  115. * @returns {Object}
  116. */
  117. function _knockingParticipantArrivedOrUpdated(participant: IKnockingParticipant, state: ILobbyState) {
  118. let existingParticipant = state.knockingParticipants.find(p => p.id === participant.id);
  119. existingParticipant = {
  120. ...existingParticipant,
  121. ...participant
  122. };
  123. return {
  124. ...state,
  125. knockingParticipants: [
  126. ...state.knockingParticipants.filter(p => p.id !== participant.id),
  127. existingParticipant
  128. ]
  129. };
  130. }