Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import ReducerRegistry from '../base/redux/ReducerRegistry';
  2. import {
  3. CHANGE_VOTE,
  4. CLEAR_POLLS,
  5. RECEIVE_ANSWER,
  6. RECEIVE_POLL,
  7. REGISTER_VOTE,
  8. RESET_NB_UNREAD_POLLS,
  9. RETRACT_VOTE
  10. } from './actionTypes';
  11. import { IAnswer, IPoll } from './types';
  12. const INITIAL_STATE = {
  13. polls: {},
  14. // Number of not read message
  15. nbUnreadPolls: 0
  16. };
  17. export interface IPollsState {
  18. nbUnreadPolls: number;
  19. polls: {
  20. [pollId: string]: IPoll;
  21. };
  22. }
  23. ReducerRegistry.register<IPollsState>('features/polls', (state = INITIAL_STATE, action): IPollsState => {
  24. switch (action.type) {
  25. case CHANGE_VOTE: {
  26. const { pollId, value } = action;
  27. return {
  28. ...state,
  29. polls: {
  30. ...state.polls,
  31. [pollId]: {
  32. ...state.polls[pollId],
  33. changingVote: value,
  34. showResults: !value
  35. }
  36. }
  37. };
  38. }
  39. case CLEAR_POLLS: {
  40. return {
  41. ...state,
  42. ...INITIAL_STATE
  43. };
  44. }
  45. // Reducer triggered when a poll is received
  46. case RECEIVE_POLL: {
  47. const newState = {
  48. ...state,
  49. polls: {
  50. ...state.polls,
  51. // The poll is added to the dictionary of received polls
  52. [action.pollId]: action.poll
  53. },
  54. nbUnreadPolls: state.nbUnreadPolls + 1
  55. };
  56. return newState;
  57. }
  58. // Reducer triggered when an answer is received
  59. // The answer is added to an existing poll
  60. case RECEIVE_ANSWER: {
  61. const { pollId, answer }: { answer: IAnswer; pollId: string; } = action;
  62. // if the poll doesn't exist
  63. if (!(pollId in state.polls)) {
  64. console.warn('requested poll does not exist: pollId ', pollId);
  65. return state;
  66. }
  67. // if the poll exists, we update it with the incoming answer
  68. const newAnswers = state.polls[pollId].answers
  69. .map(_answer => {
  70. return {
  71. name: _answer.name,
  72. voters: new Map(_answer.voters)
  73. };
  74. });
  75. for (let i = 0; i < newAnswers.length; i++) {
  76. // if the answer was chosen, we add the sender to the set of voters of this answer
  77. const voters = newAnswers[i].voters;
  78. if (answer.answers[i]) {
  79. voters.set(answer.voterId, answer.voterName);
  80. } else {
  81. voters.delete(answer.voterId);
  82. }
  83. }
  84. // finally we update the state by returning the updated poll
  85. return {
  86. ...state,
  87. polls: {
  88. ...state.polls,
  89. [pollId]: {
  90. ...state.polls[pollId],
  91. answers: newAnswers
  92. }
  93. }
  94. };
  95. }
  96. case REGISTER_VOTE: {
  97. const { answers, pollId }: { answers: Array<boolean> | null; pollId: string; } = action;
  98. return {
  99. ...state,
  100. polls: {
  101. ...state.polls,
  102. [pollId]: {
  103. ...state.polls[pollId],
  104. changingVote: false,
  105. lastVote: answers,
  106. showResults: true
  107. }
  108. }
  109. };
  110. }
  111. case RETRACT_VOTE: {
  112. const { pollId }: { pollId: string; } = action;
  113. return {
  114. ...state,
  115. polls: {
  116. ...state.polls,
  117. [pollId]: {
  118. ...state.polls[pollId],
  119. showResults: false
  120. }
  121. }
  122. };
  123. }
  124. case RESET_NB_UNREAD_POLLS: {
  125. return {
  126. ...state,
  127. nbUnreadPolls: 0
  128. };
  129. }
  130. default:
  131. return state;
  132. }
  133. });