選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

reducer.ts 4.0KB

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