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

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